drov0 / python-imagesearch

A wrapper around opencv2 and pyautogui to do image searching easily.
MIT License
279 stars 98 forks source link

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

Open Cranbaerry opened 5 years ago

Cranbaerry commented 5 years ago

Is there a way to do this?

drov0 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@eagleEggs awesome !

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

Cranbaerry commented 4 years ago

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 commented 4 years ago

@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]