Logo
Section 1 / 11

模板匹配

学习使用模板匹配检测图像特征。

导入

模板匹配

学习使用模板匹配检测图像特征。

...
需要精确定位的电子板
...
基准环的测量
1import cv2 2import numpy as np 3 4ring_outer_diameter_in_pixels = 26 5ring_inner_diameter_in_pixels = 18 6 7original_img = cv2.imread('./images/electronics/beaglebone.jpg') 8print(f"original_img.shape = {original_img.shape}") 9 10# 创建模板 11template_shapeHWC = [round(1.5 * ring_outer_diameter_in_pixels), 12 round(1.5 * ring_outer_diameter_in_pixels), 3] 13if template_shapeHWC[0] % 2 == 0: 14 template_shapeHWC[0] += 1 15if template_shapeHWC[1] % 2 == 0: 16 template_shapeHWC[1] += 1 17print(f"template_shapeHWC = {template_shapeHWC}") 18template_img = np.zeros(template_shapeHWC, dtype=np.uint8) 19 20# 绘制环形:先画一个黄色圆盘,再在其上覆盖一个较小的黑色圆盘 21cv2.circle(template_img, center=(template_shapeHWC[1]//2, template_shapeHWC[0]//2), radius=ring_outer_diameter_in_pixels//2, 22 color=(128, 210, 255), thickness=-1) 23cv2.circle(template_img, center=(template_shapeHWC[1] // 2, template_shapeHWC[0] // 2), radius=ring_inner_diameter_in_pixels//2, 24 color=0, thickness=-1) 25 26cv2.imwrite('./output/0_original.png', original_img) 27cv2.imwrite('./output/1_template.png', template_img)
...
cv2.matchTemplate() 的示例结果
1# ... 接上文 2 3# 执行模板匹配 4match_img = cv2.matchTemplate(original_img, template_img, method=cv2.TM_CCOEFF_NORMED) 5print(f"match_img.dtype = {match_img.dtype}") 6print(f"match_img.shape = {match_img.shape}") 7 8# 输出匹配值的范围 9minimum_match_value = np.min(match_img) 10maximum_match_value = np.max(match_img) 11print(f"minimum_match_value = {minimum_match_value}; maximum_match_value = {maximum_match_value}") 12 13# 将匹配结果转换为8位图像 14match_8b_img = (127.0 + 128.0 * match_img).astype(np.uint8) 15 16# 添加标注并保存 17annotated_match_8b_img = cv2.cvtColor(match_8b_img, cv2.COLOR_GRAY2BGR) 18cv2.putText(annotated_match_8b_img, 'match', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2) 19cv2.imwrite('./output/1_match.png', annotated_match_8b_img)

dimensions_{dst} = (height_{source} - height_{template} + 1, width_{source} - width_{template} + 1)

1# ... 接上文 2 3# 对匹配结果进行零填充,使其尺寸与原始图像一致 4zero_padded_match_img = np.zeros((original_img.shape[0], original_img.shape[1]), dtype=float) 5# 将匹配图像复制到零填充图像的中心区域 6zero_padded_match_img[template_shapeHWC[0]//2: template_shapeHWC[0]//2 + match_img.shape[0], 7 template_shapeHWC[1]//2: template_shapeHWC[1]//2 + match_img.shape[1]] = match_img 8 9# 对匹配结果进行阈值处理 10match_threshold = 0.43 11retval, thresholded_match_img = cv2.threshold(zero_padded_match_img, match_threshold, 255, cv2.THRESH_BINARY) 12 13# 在原始图像上做标注 14annotated_img = copy.deepcopy(original_img) 15for y in range(thresholded_match_img.shape[0]): 16 for x in range(thresholded_match_img.shape[1]): 17 if thresholded_match_img[y, x] > 0: 18 annotated_img[y, x] = (0, 0, 255) 19 20# 标注并保存阈值化后的匹配图 21annotated_thresholded_match_img = cv2.cvtColor(thresholded_match_img.astype(np.uint8), cv2.COLOR_GRAY2BGR) 22cv2.putText(annotated_thresholded_match_img, 'thresholded match', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2) 23cv2.imwrite('./output/2_thresholded_match.png', annotated_thresholded_match_img) 24 25# 标注叠加在原图上的掩膜区域 26cv2.putText(annotated_img, 'active mask (red) over the original image', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2) 27cv2.imwrite('./output/3_annotated.png', annotated_img)

平滑与遮罩

形态学