python-imagesearch icon indicating copy to clipboard operation
python-imagesearch copied to clipboard

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

Open Cranbaerry opened this issue 5 years ago • 5 comments

Is there a way to do this?

Cranbaerry avatar Aug 30 '19 00:08 Cranbaerry

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.

drov0 avatar Jan 04 '20 22:01 drov0

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.

eagleEggs avatar Jan 08 '20 22:01 eagleEggs

@eagleEggs awesome !

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

drov0 avatar Jan 08 '20 22:01 drov0

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.

Cranbaerry avatar Jan 09 '20 02:01 Cranbaerry

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

Mglt-b avatar Jul 23 '20 07:07 Mglt-b