-2

There are two PNG files.

One (A file) is a 10x10 size file with a red circle.

The other file (B file) has many white circles and one red circle.

At this time, I am looking for a way to find the x and y positions of the red circles in the B file.

--

The reason I need these is in some program I want to find the x,y position that matches a specific image I have.

This can be done using pyautogui.locateAllOnScreen, but I want to apply it to inactive windows. (pyautogui is only used active windows)

So I was looking for a way to take a screenshot of an inactive window and compare it to an image I have.

Please let me know if there is a better way

Thank you

(This was written with Google Translate. There may be errors.)


added I got what I want with the code below.

Check if a template exists using match_template from scikit-image

  • Hi and welcome to SO. Your translation seems fine, however it is important to demonstrate that you are *also* working to solve your problem. The best way to do that is to include the **text** based version of the code you have so far. You say that you are using `pyautogui.locateAllOnScreen`, I bet that if you showed us how that people would join in and help you. – JonSG Dec 04 '21 at 16:11

2 Answers2

0

I am not so sure but I think your problem can be solved using template matching. If you are looking for an exact match, in your case the 10x10 image exactly as it is, then this should work fine. It wont work however for cases where the template in the image is oriented, skewed or has undergone any transformations. You could refer SIFT for that.

Basically template matching is just trying to find a smaller matrix inside a larger matrix(Images can be considered as 2d matrices). So you can yoursefl write the code for this using nested for loops and for each new top left position in the bigger image( image B in your case) match the smaller image i.e. the template(image A). Youl''l have to decide the metric for your 'match', as in if its an exact match, or the pixels can vary some percentage you defined, or some other threshold. Since this will give you the top left coordinates of the image, adding width and height would give you the bottom right one and you have all 4 of them. This is the naive approach.

Or you can use OpenCVs template matching https://docs.opencv.org/4.x/d4/dc6/tutorial_py_template_matching.html

It does the same thing I explained, albeit in a much optimized manner and you can even see how the matching function has different algorithms.

Rinkesh P
  • 588
  • 4
  • 13
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 04 '21 at 17:32
  • 'using template matching' Thanks for teaching me this. – Love_Python Dec 05 '21 at 01:51
0

I would use opencv. For your application, you could use templates, but they're dodgy. Instead, I'd just go straight to image B, get the contours and return the coordinates of the one containing the color you're seeking.

As an example, consider this image:

Demo

import cv2
import os

FILE = os.path.expanduser(r"~\Downloads\demo.png")

RED = (0, 0, 255)  # BGR

if __name__ == "__main__":
    img = cv2.imread(FILE, -1)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    centroids = cv2.connectedComponentsWithStats(gray, 8, cv2.CV_32S)[3]

    for center in centroids:
        x, y = int(center[0]), int(center[1])
        if (tuple(img[y, x, :]) == RED):
            print(x, y)

The following outputs:

596 411
adam.hendry
  • 4,458
  • 5
  • 24
  • 51