2017-10-08 3 views
0

나는 C#와 AForge로 이미지 인식을하고 있는데, 거의 일치하는 부분이 거의 없다. 그러나 나는 그들 중 단 하나를 어떻게 선택합니까? 어떻게 든 여기 foreach(){}을 변경해야 할 것은 내 코드입니다 :모든 경기에서 하나를 선택하는 방법? C# AForge

ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.925f); 
// find all matchings with specified above similarity 
TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template); 
// highlight found matchings 
BitmapData data = sourceImage.LockBits(
    new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 
    ImageLockMode.ReadWrite, sourceImage.PixelFormat); 
foreach (TemplateMatch m in matchings) // <-----how to change this to select only one random thing? 
{ 
    Drawing.Rectangle(data, m.Rectangle, Color.White); 
    // do something else with matching 
} 
sourceImage.UnlockBits(data); 

답변

0

당신이 일치하는 구체적인 배열이 아니라, 당신이이 라인을 따라 뭔가를해야 할 몇 가지 장황한 평가를 실행할 수 열거 보인다 linq 처리되기 때문에 :

Random rnd = new Random(); 
var rndPick = matches[rnd.Next(matches.Length)]; 

예 - linqpad을 냈다

string[] matches = new string[] {"A","B","C","D","E","F","G"}; 

Random rnd = new Random(); 

for (int i=0; i<10; i++) 
    matches[rnd.Next(matches.Length)].Dump(); 

비 LinqPad, 그냥 내가 당신의 예제를 사용하려고 콘솔

for (int i=0; i<10; i++) 
     Console.Writeline(matches[rnd.Next(matches.Length)]); 
+0

선택한 항목을 작성하지만 난'()'를 덤프하는 것이 무엇인지 프로그래밍에 새로운 오전? – greyb3ast

+0

[LinqPad] (https://www.linqpad.net/)는 멋진 C# 도구입니다. 'Dump'는 LHS 아이템의 내용을 콘솔에 내보내는 확장 메소드입니다. 콘솔에 대한 답을 직접 작성할 수 있습니다 (업데이트 된 예) –

관련 문제