1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import cv2 import numpy as np import time
image = cv2.imread('smiley.png', cv2.IMREAD_COLOR ) template = cv2.imread('template.png', cv2.IMREAD_COLOR)
h, w = template.shape[:2] method = cv2.TM_CCOEFF_NORMED res = cv2.matchTemplate(image, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
threshold = 0.90 max_val = 1 while max_val > threshold: min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val > threshold: res[max_loc[1]-h//2:max_loc[1]+h//2+1, max_loc[0]-w//2:max_loc[0]+w//2+1] = 0 image = cv2.rectangle(image,(max_loc[0],max_loc[1]), (max_loc[0]+w+1, max_loc[1]+h+1), (0,0,0) )
cv2.imwrite('output.png', image)
|