在Emgu中找到图像A在图像B中的位置

12

我刚接触Emgu,想要一些关于从哪里开始学习的建议。

我已经看过形状检测了,但对我的需求来说太过复杂了。我认为我的surf示例也不起作用。我得到了这个错误:

Cannot get SURF example in EMGU.CV to work?

无论如何,这是我想做的事情:在图像B中查找图像A。 图像A是一个简单的正方形,始终具有相同的灰色1像素边框和相同的大小(我相信),但内部颜色可能是黑色或约7种其他颜色之一(仅为纯色)。 当我按下按钮时,我需要找到图像A在图像B中的坐标。请参见以下图片。

图像B

image b

图像A

image a


感谢您帮助我格式化我的帖子。 - user2078674
你没有想法吗?或者哪个EMGU领域最适合这个问题? - user2078674
是的,请看一下这个问题 - Jesse
2个回答

25

Goosebumps的答案是正确的,但我认为加上一些代码可能也会有所帮助。这是使用MatchTemplate来检测图像A模板在源图像B中出现的代码。正如Goosebumps所指出的,您可能希望在模板周围包含一些灰色。

Image<Bgr, byte> source = new Image<Bgr, byte>(filepathB); // Image B
Image<Bgr, byte> template = new Image<Bgr, byte>(filepathA); // Image A
Image<Bgr, byte> imageToShow = source.Copy();

using (Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED))
{
    double[] minValues, maxValues;
    Point[] minLocations, maxLocations;
    result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);

    // You can try different values of the threshold. I guess somewhere between 0.75 and 0.95 would be good.
    if (maxValues[0] > 0.9)
    {
        // This is a match. Do something with it, for example draw a rectangle around it.
        Rectangle match = new Rectangle(maxLocations[0], template.Size);
        imageToShow.Draw(match, new Bgr(Color.Red), 3);
    }
}

// Show imageToShow in an ImageBox (here assumed to be called imageBox1)
imageBox1.Image = imageToShow;

一个快速的问题,在你的代码中它说“source.Draw”,这是直接在资源文件夹中的图像(称为source)上绘制,还是在picturebox(称为source)上绘制?我不明白如何设置source和template。 - user2078674
它正在图像上绘制。sourcetemplateImage<Bgr, byte>类型。请参见更新的答案。 - Oskar Birkne
1
谢谢!我差点就成功了,当时我试过:P。之后我又遇到了另一个问题,提示:无法隐式转换类型“Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>”为“System.Drawing.Image”。所以我改成了ImageBox1.Image = imageToShow.Bitmap; 但现在我又遇到了“Emgu.CV.CvInvoke”引发异常的问题。:( - user2078674
顺便说一下,上面的所有代码都完美地运行了!我只是将我的exe复制到emgu附带的示例文件夹中,它就可以工作了!这是我的Dll,现在我使用http://www.emgu.com/wiki/index.php/Download_And_Installation添加了它们,它可以正常工作:)谢谢!! - user2078674
如果较小的图像被调整大小或旋转,有没有办法使其正常工作? - mrid

3

感谢这个很棒的页面!有很多有用的信息 :) - user2078674

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接