cv2 matching template on a black screenshot

60 views Asked by At

I'm trying to wait until I detect an image on a screenshot. The problem is that when there's a fade to black, the program finds the image, which isn't black at all (see images below).

screenshot: screenshot

template: template

result: result

Here is my code:

def find_image_in_screenshot(template_name, *, screenshot=None, threshhold=0.99, coords=None):
    if screenshot is None: 
        if coords is None:
            coords = (None, None, None, None)
        screenshot = take_screenshot(*coords)
    
    # read bananas image template
    template = cv2.imread(f'imgs/{template_name}.png', cv2.IMREAD_UNCHANGED)
    hh, ww = template.shape[:2]

    # extract bananas base image and alpha channel and make alpha 3 channels
    base = template[:,:,0:3]
    alpha = template[:,:,3]
    alpha = cv2.merge([alpha,alpha,alpha])

    # do masked template matching and save correlation image
    correlation = cv2.matchTemplate(screenshot, base, cv2.TM_CCORR_NORMED, mask=alpha)

    # set threshold and get all matches
    loc = np.where(correlation > threshhold)
        
    cv2.imwrite('screenshot.png', screenshot)
        
    # draw matches 
    coords = []        
    for pt in zip(*loc[::-1]):
        coords.append([pt[0], pt[1], pt[0]+ww, pt[1]+hh])
        cv2.rectangle(screenshot, pt, (pt[0] + ww, pt[1] + hh), (0, 0, 255), 2)
        break

    cv2.imwrite('result.png', screenshot)
    cv2.imwrite('template.png', template)

    return coords

I tried with different methods, different threshold, added some transparency in the image (cause I need transparency for other images), but nothing seems to work.

0

There are 0 answers