Python Forum

Full Version: Compare values in a for loop.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey!

Im working on this code that get a list o templates and compare(OpenCV template matching) to a image searching for templates.

The precision is set to 0.70 so search_image_in_image get THE FIRST template that have this match precision (a float value).

def search_image_in_image(small_image, large_image, precision):
    try:
        template = small_image.astype(np.float32)
        img_rgb = large_image.astype(np.float32)

        template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
        img_rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

        res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
        loc = (_, max_val, _, max_loc) = cv2.minMaxLoc(res)
        if loc[1] >= precision:
            print('parameter:', loc[1])
            found_positions = loc[1]
            return found_positions
    except:
        print('Not working')
What Im trying to do is not stop at the first 0.70 match, I want to get the best value among all >= 0.70. So if the best match is 0.70 return it index, but if have a higer match like 0.90 return it.

Right now the code stop right the way it find a 0.70 match. I want to compare the results and get the best match (higer value).

Apreciate any help to do it in the pythonic and fastest results.


def detect_current_item(itens_list):
    image_to_check = get_screen_area_as_image(itens_list[1]["check_area"])
    for index, item in enumerate(itens_list):
        if item["image"] is not None:
            found_xy = search_image_in_image(item["image"], image_to_check, precision)
            if found_xy:
                return index
    return None
Please, don't remove content. If you fund solution yourself, consider sharing it for benefit of others.