def find_matches(img, template_list):
# Make a copy of the image to draw on
# Define an empty list to take bbox coords
bbox_list = []
# Iterate through template list
# Read in templates one by one
# Use cv2.matchTemplate() to search the image
# using whichever of the OpenCV search methods you prefer
# Use cv2.minMaxLoc() to extract the location of the best match
# Determine bounding box corners for the match
# Return the list of bounding boxes
method = cv2.TM_CCOEFF_NORMED
for temp in templist:
tmp = mpimg.imread(temp)
# Apply template Matching
res = cv2.matchTemplate(img,tmp,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
w, h = (tmp.shape[1], tmp.shape[0])
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
bbox_list.append((top_left, bottom_right))
return bbox_list
评论列表
文章目录