1 Star 0 Fork 0

念古/opencv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
模板匹配.py 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
念古 提交于 2019-12-30 17:05 . Add files via upload
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('/home/niangu/桌面/答题卡识别/webwxgetmsgimg (16).jpeg', 0)
img2 = img.copy()
template = cv2.imread('/home/niangu/桌面/答题卡识别/test2/裁剪2.jpg', 0)
w, h = template.shape[::-1]
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR', 'cv2.TM_CCORR_NORMED',
'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
for meth in methods:
img = img2.copy()
method = eval(meth)
res = cv2.matchTemplate(img, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
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)
cv2.rectangle(img, top_left, bottom_right, 255, 2)
"""
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.imshow('img', res)
cv2.namedWindow('edges', cv2.WINDOW_NORMAL)
cv2.imshow('edges', img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()
"""
plt.subplot(121)
plt.imshow(res, cmap='gray')
plt.title('Matching Result')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(img, cmap='gray')
plt.title('Detected Point')
plt.xticks([])
plt.yticks([])
plt.suptitle(meth)
plt.show()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/niangu/opencv.git
[email protected]:niangu/opencv.git
niangu
opencv
opencv
master

搜索帮助