emgu finding image a in image b [duplicate]

删除回忆录丶 提交于 2019-11-30 05:09:16

Goosebumps answer is correct, but I thought that a bit of code might be helpful also. This is my code using MatchTemplate to detect a template (image A) inside a source image (image B). As Goosebumps noted, you probably want to include some grey around the template.

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;

You could have a look at http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html This is probably what you are looking for. Your Black square would be the template. You may try to also include a little bit of grey around it. This will keep the detector from fireing on large black areas.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!