Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to get x and y coordinates of each match that is found in ImageSearchCount? #11

Open
Cranbaerry opened this issue Aug 30, 2019 · 5 comments

Comments

@Cranbaerry
Copy link

Is there a way to do this?

@drov0
Copy link
Owner

drov0 commented Jan 4, 2020

There isn't for now, but you can easily edit the imagesearch_count function to do it (save the coordinates in a list instead of increasing the counter).

A new function would be welcome.

@eagleEggs
Copy link

Yea typically you would just build your own function and pipe out the x,y's. Do you want this functionality within imagesearchcount? Seems like it's overkill if for most the function isn't meant to do it that's just wasted overhead in the background. I'd say a new function ImageSearchCountLoc for returning the values as well as the count. I'm down to craft it up.

@drov0
Copy link
Owner

drov0 commented Jan 8, 2020

@eagleEggs awesome !

I think this deserves it's own function as well. And I am down for the name ImageSearchCountLoc

@Cranbaerry
Copy link
Author

Hi, I made this:

def imageSearchCount(image, precision=0.9, screenshot=False):
    img_rgb = pyautogui.screenshot()
    img_rgb = array(img_rgb)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

    if screenshot:
        template = cv2.cvtColor(array(image), cv2.COLOR_BGR2GRAY)
    else:
        template = cv2.imread(image, 0)

    w, h = template.shape[::-1]
    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    loc = where(res >= precision)
    count = []
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
        count.append((pt[0], pt[1]))
    # cv2.imwrite('result.png', img_rgb)
    return count

It returns list of each coordinates found in a list.

@Mglt-b
Copy link

Mglt-b commented Jul 23, 2020

@Mikami382 Thank you !

Its working for me !

edit : this file "......./Python38/site-packages/python_imagesearch/imagesearch.py" like this :

def imagesearch_count(image, precision=0.9):
    img_rgb = pyautogui.screenshot()
    if is_retina:
        img_rgb.thumbnail((round(img_rgb.size[0] * 0.5), round(img_rgb.size[1] * 0.5)))
    img_rgb = np.array(img_rgb)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(image, 0)
    w, h = template.shape[::-1]
    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    loc = np.where(res >= precision)
    count = []
    for pt in zip(*loc[::-1]):  # Swap columns and rows
        # cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2) // Uncomment to draw boxes around found occurrences
        count .append((pt[0], pt[1]))
    # cv2.imwrite('result.png', img_rgb) // Uncomment to write output image with boxes drawn around occurrences
    return count

And in your code :

from python_imagesearch.imagesearch import imagesearch_count

image_counted = imagesearch_count("name_of_image.png")

if len(image_counted) > 0:
    x_count_list = []
    y_count_list = []

    for (x_count, y_count) in image_counted : 
        x_count_list.append(x_count)
        y_count_list.append(y_count)

#coords of first x, x_count_list[0]
#coords of twice x, x_count_list[1]
#coords of first y, y_count_list[0]
#coords of twice y, y_count_list[1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants