如何从所有比赛中选择一个? C#AForge

时间:2017-10-08 13:36:35

标签: c# .net image-recognition aforge

我正在使用c#和AForge进行图像识别,我得到的几个匹配很棒,因为图片中几乎没有相同的东西。但我如何选择其中一个呢?我需要以某种方式更改foreach(){}这是我的代码:

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 );

1 个答案:

答案 0 :(得分: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)]);