V
V
Vadim)))2021-09-19 14:40:12
Python
Vadim))), 2021-09-19 14:40:12

How to find one image in another image?

I need the program to find a specific object in the screenshot and return its coordinates. An object is another image that is in the folder with the program. The program should find the object, even if it is rotated at an angle and a different color. How to implement this in Python? Ready to use third-party libraries and ready-made solutions.
!!! THERE WILL BE A LOT OF OBJECTS!!!

6147215a778a2582220472.pnghere is a screenshot.
614721756f7ad919391640.pngand this program should find.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-09-19
@VadimCoder

Template matching
61472e47a4566878455706.jpeg

V
Vindicar, 2021-09-19
@Vindicar

Terminology for googling: "Localization of an object using computer vision methods."
You can use opencv as a toolkit. For python, this is the pip package python-opencv (imported under the name cv2).
To get around the color problem, convert the input image to grayscale, and then, if necessary, add a thresholding to leave only black and white.
There are different approaches. If wildcard performance is not required, then a SIFT detector can be used.
Then you will have an algorithm like this:
0. You create a detector (function cv2.SIFT.create()) and a matcher (you can cv2.BFMatcher(), it is slow but easy to use).
1. Load a sample of the object you are looking for and transform the images (grayscale, etc.)
2. You call the detector's .detectAndCompute() method to get the found singular points (corners, etc.) and their description.
3. You repeat steps 1,2 for the analyzed image (scene).
4. Call the .knnMatch() cn=2 method on the matcher. This will give you the top 2 matches between the sample and the scene for each feature point.
5. You can simply take the best match, or you can filter out those special points for which the two closest matches are too similar - they are eliminated.
One way or another, you should end up with a list of pairs of points in the form "point on the sample - found similar point on the stage."
6. Use the cv2.findHomography() function or one of its relatives to find the transformation matrix. Roughly speaking, it will allow you to recalculate the pixel position on the sample image into the pixel position on the scene image.
Something like this, if you imagine it visually (to get such a picture, you need a few more steps, but it conveys the idea).
homography_findobj.jpg
7. Then you can use this matrix as you like. For example, take the coordinates of the object's center in the reference image, and use it in conjunction with cv2.perspectiveTransform() to find where in the scene the coordinates of the object's center point are. By taking another point (in the corner or on the edge), you can find the orientation.
The problem is that this approach ONLY works if there is no more than one similar object in the scene.
I do not provide the code, sketch a draft yourself, then it will be possible to discuss and correct it. If anything, you can find a lot of information on the net on using opencv to solve this problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question