Skip to content

Instantly share code, notes, and snippets.

@zhunhung
Created October 17, 2019 16:26
Show Gist options
  • Save zhunhung/bc5c785d717c74f7158e4b238fc9d5e8 to your computer and use it in GitHub Desktop.
Save zhunhung/bc5c785d717c74f7158e4b238fc9d5e8 to your computer and use it in GitHub Desktop.
Match template to unit bounding box at different orientation
# load base image and template
img = cv2.imread(sample_img_path, 0)
template = cv2.imread(template_path, 0)
# canny edge detection to get edges of base image
edged = cv2.Canny(img, 50, 200)
# define the orientations
methods_arr = [('original',0), ('original',90), ('original',180), ('original',270),
('flipped',0), ('flipped',90), ('flipped',180), ('flipped',270)]
for method in methods_arr:
template_copy = template.copy()
if method[0] == 'flipped':
# flips image
template_copy = cv2.flip(template, 1)
# rotates image
template_copy = rotate_image(template_copy, method[1])
# perform template matching
result = cv2.matchTemplate(edged, template_copy, cv2.TM_SQDIFF)
# get maximum and minimum distance => we focus on minimum distance
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment