def multiple_template_match(self, feature, scene, roi=None, scale=None, min_scale=0.5, max_scale=1.0, max_distance=14, min_corr=0.8, debug=False, threshold_min=50, threshold_max=200):
if roi is not None:
scene = scene[roi.top:(roi.top + roi.height), roi.left:(roi.left + roi.width)]
if not scale:
scale = self.find_best_scale(feature, scene, min_scale=min_scale, max_scale=max_scale, min_corr=min_corr)
peaks = []
if scale:
scaled_feature = cv2.resize(feature, (0, 0), fx=scale, fy=scale)
canny_scene = cv2.Canny(scene, threshold_min, threshold_max)
canny_feature = cv2.Canny(scaled_feature, threshold_min, threshold_max)
# Threshold for peaks.
corr_map = cv2.matchTemplate(canny_scene, canny_feature, cv2.TM_CCOEFF_NORMED)
_, max_corr, _, max_loc = cv2.minMaxLoc(corr_map)
good_points = list(zip(*np.where(corr_map >= max_corr - self.tolerance)))
if debug:
print(max_corr, good_points)
clusters = self.get_clusters(good_points, max_distance=max_distance)
peaks = [max([(pt, corr_map[pt]) for pt in cluster], key=lambda pt: pt[1]) for cluster in clusters]
return (scale, peaks)
评论列表
文章目录