emgu finding image a in image b [duplicate]
Asked Answered
F

2

12

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

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:

Cannot get SURF example in EMGU.CV to work?

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.

Image B

image b

And

Image A

image a

Feasible answered 6/5, 2013 at 20:43 Comment(3)
thanks for helping me format my post.Feasible
:( no ideas? or what area of EMGU would be best for this?Feasible
Yes, take a look at this question.Qktp
D
25

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;
Dilapidated answered 7/5, 2013 at 11:58 Comment(5)
A quick question, in your code it says "source.Draw" Is this drawing directly onto an image (called source) in the resources folder? or onto a picturebox (called source)? I don't understand how to set source and template.Feasible
It's drawing onto an image. source and template are Image<Bgr, byte>. See updated answer.Dilapidated
Thanks! I nearly had it when i tried :P After this i had another problem saying: Cannot implicitly convert type 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>' to 'System.Drawing.Image' So i changed ImageBox1.Image = imageToShow.Bitmap; But now i have a 'Emgu.CV.CvInvoke' threw an exception. :(Feasible
Btw, All the code above works perfectly! i just copied my exe into the example folder that comes with emgu and it works! It was my Dll's and now i added them using : emgu.com/wiki/index.php/Download_And_Installation and it works fine :) thanks !!Feasible
is there any way to make this work if the smaller image is resized or rotated?Neiman
R
3

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.

Romilly answered 7/5, 2013 at 9:42 Comment(1)
thanks for this great page! Lots of useful info :)Feasible

© 2022 - 2024 — McMap. All rights reserved.