Skip to content

Instantly share code, notes, and snippets.

@zawhtutwin
Created November 24, 2018 09:41
Show Gist options
  • Save zawhtutwin/7d3f03b7426e264e443aefdebc1efc24 to your computer and use it in GitHub Desktop.
Save zawhtutwin/7d3f03b7426e264e443aefdebc1efc24 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
img = cv2.imread('original12.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread('tal.jpg',0)
# run template matching, get minimum val
res = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# create threshold from min val, find where sqdiff is less than thresh
min_thresh = 0.9
match_locations = np.where(res>=min_thresh)
print(match_locations)
# draw template match boxes
w, h = template.shape[::-1]
for (x, y) in zip(match_locations[1], match_locations[0]):
cv2.rectangle(img, (x, y), (x+w, y+h), [0,0,255], 2)
cv2.imwrite('output.jpg',img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment