代码拉取完成,页面将自动刷新
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。