[Python] How does one compare the result of multiple CV2 match templates

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    Hello guys just a quick question with regards to CV2 Template matching.

    I'm developing a bot program for an old online MMORPG game and as part of the calibration system I have 4 functions with the purpose of finding each cardinal point . All works well , however I want to add a 5th function that gets the current position.

    So what it's supposed to do is simply find the current cardinal point orientation on a compass image by comparing the current position on that image with a list of templates for each cardinal point .

    self.all_cardinals = [self.east_img,self.north_img,self.west_img,self.south_img]

    def findcurrent(self):
    for image in self.all_cardinals:
    test_img = pyautogui.screenshot(region=(1249, 102, 22, 22))
    test_img2 = cv2.cvtColor(np.array(test_img), cv2.COLOR_RGB2BGR)
    result = cv2.matchTemplate(test_img2, image, method=cv2.TM_CCORR_NORMED, mask=image)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
    threshold = 0.945
    if max_val >= threshold:
    break
    else:
    continue


    Compass mage to search over

    Template image to search for

    Now all this works fine in the sense that if the character in-game is facing any one of the 4 cardinal points it breaks out of the loop, however I am struggling with outputting that result and adding that in a 'if' condition statement.

    For example if the result was a match for the W cardinal point then do something like another function.

    Or if the result was a match for the S cardinal point do something else.

    I have been going back and forth with this and tried a few different methods like adding a return image to the function which gives me back a 3d array and tried to compare that to the original cv2.imread of that image.

    east_img = cv2.imread('resources/findxy/facingeast.png')
    east_test = Calibrate.findcurrent()
    print(east_img == east_test)


    I'm getting a ValueError: operands could not be broadcast together with shapes .

    MRE. We have a white background image (bgcolor is irrelevant) with 1 red square on it. . We then template-match 4 different color squares (one being red) by using a for loop and tell it to break when it finds a match . How do we then obtain that output (red match) and use it. e.g if the match was the red square then do something

    Continue reading...

Compartilhe esta Página