Python Forum
Compare values in a for loop.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare values in a for loop.
#1
Exclamation 
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
Reply
#2
Please, don't remove content. If you fund solution yourself, consider sharing it for benefit of others.
nilamo likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 855 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Loop through values and compare edroche3rd 6 684 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Trying to compare string values in an if statement israelsattleen 1 541 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,097 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,482 Jul-27-2022, 08:50 PM
Last Post: rob101
  How do loop over curl and 'put' different values in API call? onenessboy 0 1,219 Jun-05-2022, 05:24 AM
Last Post: onenessboy
  Compare variable with values in a file paulo79 1 1,104 Apr-22-2022, 03:16 AM
Last Post: Pedroski55
  Loop through values in dictrionary and find the same as in previous row Paqqno 5 1,901 Mar-27-2022, 07:58 PM
Last Post: deanhystad
  How to add for loop values in variable paulo79 1 1,441 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,843 Apr-01-2021, 07:57 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020