C# emgu 在图像 b 中找到图像 a

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16406958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:57:16  来源:igfitidea点击:

emgu finding image a in image b

c#emgucvsurfmatchtemplate

提问by user2078674

I'm new to emgu and would like some advice on where to start.

我是 emgu 的新手,想要一些关于从哪里开始的建议。

I've looked through the shape detection but its far too complex for what i need .. i think.. and my surfexample isn't working. I get this error:

我已经查看了形状检测,但它对于我需要的东西来说太复杂了..我认为..我的 surfexample 不起作用。我收到此错误:

Cannot get SURF example in EMGU.CV to work?

无法让 EMGU.CV 中的 SURF 示例正常工作?

Anyway, this is what i would like to do: Find image A in image B. Image A is a simple square which always has the same grey 1 pixel border and always the same size (i believe) but the inner colour could be black or one of about 7 other colours (only ever a solid colour). i need to find the coordinates of image A in image b when i press a button. see the below images.

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

Image B

image b

图片B

图片 b

And

Image A

image a

图片A

图像a

采纳答案by Oskar Birkne

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

Goosebumps答案是正确的,但我认为一些代码也可能有帮助。这是我MatchTemplate用来检测源图像(图像 B)中的模板(图像 A)的代码。如前所述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;

回答by Goosebumps

You could have a look at http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.htmlThis 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.

你可以看看http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html这可能是你正在寻找的。你的黑色方块将是模板。您也可以尝试在其周围添加一点灰色。这将防止探测器在大面积黑色区域上发射。