在另一个图像中查找图像

mar*_*vin 10 c# search image aforge

我正在尝试构建一个解决谜题的应用程序(尝试开发图形算法),我不想一直手工输入样本输入.

编辑: 我不是想建立一个游戏.我正在尝试建立一个扮演游戏"SpellSeeker"的经纪人

假设我在屏幕上有一个图像(见附件),上面有数字,我知道这些框的位置,我有这些数字的确切图像.我想要做的只是告诉相应的盒子上有哪个图像(数字).

数字http://i46.tinypic.com/3089vyt.jpg

所以我想我需要实施

bool isImageInsideImage(Bitmap numberImage,Bitmap Portion_Of_ScreenCap) 或类似的东西.

我试过的是(使用AForge库)

public static bool Contains(this Bitmap template, Bitmap bmp)
{
    const Int32 divisor = 4;
    const Int32 epsilon = 10;

    ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.9f);

    TemplateMatch[] tm = etm.ProcessImage(
        new ResizeNearestNeighbor(template.Width / divisor, template.Height / divisor).Apply(template),
        new ResizeNearestNeighbor(bmp.Width / divisor, bmp.Height / divisor).Apply(bmp)
        );

    if (tm.Length == 1)
    {
        Rectangle tempRect = tm[0].Rectangle;

        if (Math.Abs(bmp.Width / divisor - tempRect.Width) < epsilon
            &&
            Math.Abs(bmp.Height / divisor - tempRect.Height) < epsilon)
        {
            return true;
        }
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

但是在此图像中搜索黑点时返回false.

我该如何实现呢?

mar*_*vin 5

我找到解决方案后,我正在回答我的问题:

对我有用:

System.Drawing.Bitmap sourceImage = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\1.jpg");
            System.Drawing.Bitmap template = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\2.jpg");
            // create template matching algorithm's instance
            // (set similarity threshold to 92.5%)

           ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.921f);
                // 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)
            {

                    Drawing.Rectangle(data, m.Rectangle, Color.White);

                MessageBox.Show(m.Rectangle.Location.ToString());
                // do something else with matching
            }
            sourceImage.UnlockBits(data);
Run Code Online (Sandbox Code Playgroud)

唯一的问题是它为所述游戏找到了所有(58)盒子.但是将值0.921f更改为0.98使其完美,即它只找到指定数字的图像(模板)

编辑:我实际上必须为不同的图片输入不同的相似度阈值.我通过尝试找到了优化的值,最后我有一个类似的函数

float getSimilarityThreshold(int number)
Run Code Online (Sandbox Code Playgroud)