这篇文章的重点在于 模板匹配 的使用。模板匹配是计算机视觉中的一项基本技术,它通过比对输入图像与模板图像的相似度,来进行目标识别。对于数字识别,特别是标准数字的识别,模板匹配非常有效。
请看效果:
文章结构
- 模板匹配的概念
- 如何裁剪图像以提高匹配精度
- 代码实现:数字识别
- 代码解析
- 总结与建议
模板匹配的概念
模板匹配是一种在图像中查找特定模板区域的方法。通过计算输入图像与多个模板的相似度,找到最匹配的区域。在数字识别中,这项技术通过将待识别的数字与一组模板数字进行比对,识别出最接近的数字。
如何裁剪图像以提高匹配精度
在使用模板匹配时,图像的预处理是至关重要的。裁剪掉不需要的部分,尤其是图像中可能干扰匹配的区域,可以大大提高匹配的精度。确保图像中只包含目标数字区域,从而提高识别准确率。
代码实现:数字识别
接下来,我会分享一段我用OpenCV实现的数字识别代码。这段代码利用了模板匹配的方式来识别标准数字。
import cv2
import numpy as np
import os
# 数字模板匹配
def img_match(input_img, template_dict):
"""返回 (最佳匹配数字, 最大相似度)"""
resized_img = cv2.resize(input_img, (32, 48), interpolation=cv2.INTER_LINEAR)
max_val = 0.0
best_num = -1
for i in range(10):
template = template_dict[i]
result = cv2.matchTemplate(resized_img, template, cv2.TM_CCOEFF_NORMED)
_, current_max, _, _ = cv2.minMaxLoc(result)
if current_max > max_val:
max_val = current_max
best_num = i
return best_num, max_val
# 加载数字模板
def load_templates(template_dir):
template_dict = {}
for i in range(10):
template_path = os.path.join(template_dir, f"{i}.png")
template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)
if template is not None:
_, binary_template = cv2.threshold(template, 100, 255, cv2.THRESH_BINARY_INV)
resized_template = cv2.resize(binary_template, (32, 48), interpolation=cv2.INTER_LINEAR)
template_dict[i] = resized_template
return template_dict
# 主函数
def main():
# 加载数字模板
template_dir = "G:/pycharm/projects/opencv/num/" # 修改为你的模板路径
template_dict = load_templates(template_dir)
# 打开摄像头
cap = cv2.VideoCapture(1)
if not cap.isOpened():
print("无法打开摄像头")
return
while True:
ret, frame = cap.read()
if not ret:
break
imgContour = frame.copy()
imgGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 1)
_, imgThresh = cv2.threshold(imgBlur, 95, 255, cv2.THRESH_BINARY_INV)
edges = cv2.Canny(imgBlur, 30, 150)
imgCanny = cv2.bitwise_or(edges, imgThresh)
kernel = np.ones((3, 3), np.uint8)
imgCanny = cv2.morphologyEx(imgCanny, cv2.MORPH_CLOSE, kernel)
contours, hierarchy = cv2.findContours(imgCanny, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area < 4500: # 像素面积小,可认为是数字
x, y, w, h = cv2.boundingRect(cnt)
roi = imgCanny[y:y + h, x:x + w]
if roi.size == 0:
continue
num, score = img_match(roi, template_dict)
if score > 0.6:
cv2.putText(imgContour, f"{num}", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
cv2.imshow("Processed", imgContour)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
代码解析
1. 数字模板匹配
img_match
函数负责执行模板匹配操作。它将输入的图像与预先加载的数字模板进行比对,计算出匹配度,并返回最佳匹配的数字和相似度。
2. 加载模板
load_templates
函数从指定路径加载数字模板,模板图像会经过二值化处理并调整到统一的尺寸,确保模板能够适应各种输入图像。
3. 图像预处理
在主函数中,我们首先通过摄像头读取图像,并进行一些常见的图像预处理操作,包括灰度化、模糊化和边缘检测。然后,我们使用 cv2.findContours
函数提取图像中的数字区域。
4. 匹配与识别
通过 img_match
函数对每一个数字区域进行模板匹配,若匹配的相似度大于设定的阈值(例如0.6),则将识别的数字显示在图像上。
总结与建议
通过模板匹配,我们能够快速、准确地识别标准数字,适用于数字识别的基础场景。对于更复杂的场景,如手写数字或不同字体的数字,可能需要更先进的算法,如深度学习模型。
希望这篇文章能够帮助你理解OpenCV中模板匹配的使用方法。如果你有任何问题或改进建议,欢迎在评论区留言讨论。