P
P
pepeh2014-09-25 19:24:13
Java
pepeh, 2014-09-25 19:24:13

How to compare 2 OpenCV images?

Hello.
Using Opencv, I find the coordinates of the picture in the screenshot.

Mat screen = Highgui.imread(image1, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat image = Highgui.imread(image2, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat result = new Mat(screen.cols()- image.cols() + 1, screen.rows()- screen.rows() + 1, CvType.CV_32FC1);

Imgproc.matchTemplate(screen, image, result, Imgproc.TM_SQDIFF_NORMED);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc = mmr.minLoc;

The problem is that if there is no picture in the screenshot, Opencv finds something similar and still gives out the coordinates.
I can take and cut out from the screenshot at those coordinates that part of the image that should have matched the template
Mat search_result = new Mat(screen, new Rect((int) matchLoc.x, (int) matchLoc.y, image.cols(), image.rows()));

How can I now compare search_result and image , for example, in percentage terms for a match? If you can get a different percentage of coincidence, please tell me the code.
Thanks for the answer

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir, 2014-09-26
@noonv

It can be like this , but it can be that way .

R
rusianvodka, 2016-01-14
@rusianvodka

Hello, I have a similar problem and question.
If you found the answer, I will be grateful for its description.
There is an image - a screenshot, and a small piece of this image, which must be found in the screenshot.
With the help of ROI, I limit the area where it is necessary to search, after that I use the same functions as you, but how to determine whether my pattern is found in this ROI or not? Especially if the area has different dimensions in relation to the template.

R
Roman Mirilaczvili, 2019-06-05
@2ord

As far as I understand, it was also necessary to filter the comparison result above a certain threshold.

Imgproc.matchTemplate(screen, image, result, Imgproc.TM_SQDIFF_NORMED);
The result of matchTemplate must be placed in a variable and then filtered.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question