0

I'm trying to use template matching to detect when the input from my screen no longer matches the template. The code below puts a rectangle around the area I want to detect a change in. Is there any way I can, lets say, print some text when that rectangle is no longer there?

import numpy as np
import cv2
from PIL import ImageGrab as ig
import pyautogui as gui
import glob

template_data=[]
files1 = glob.glob("C:\\Users\\aaaa\\template images\\template*.jpg")

for myfile in files1:
        image = cv2.imread(myfile,0)
        template_data.append(image)

while(True):
    source = ig.grab(bbox=(700,400,1200,700))
    screen_colour = cv2.cvtColor(np.array(source), cv2.COLOR_BGR2RGB)
    screen = cv2.cvtColor(np.array(screen_colour), cv2.COLOR_RGB2GRAY)

    for tmp in template_data:
        (h, w) = tmp.shape[:2]
        result = cv2.matchTemplate(np.array(screen), tmp, cv2.TM_CCOEFF_NORMED)
        threshhold = 0.92
        loc = np.where(result >= threshhold)

        for pt in zip(*loc[::-1]):
            cv2.rectangle(screen, pt, (pt[0]+w, pt[1]+h), (0,255,255),2)
            print("yes")


    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

    cv2.imshow('Result',np.array(screen))

1 Answers1

0

If you have an offset, take the absolute difference of the template and the image and compare this to a threshold

def still_has_rect(template,image,pt):
    min_x = pt[0]
    max_x = min_x + template.shape[0]
    min_y = pt[1]
    max_y = min_y + template.shape[1]
    sub_image = image[min_x:max_x,min_y:max_y]

    difference = sum(abs(sub_image-template))
    return difference<threshold
Ted Brownlow
  • 1,103
  • 9
  • 15