代码拉取完成,页面将自动刷新
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('lena.jpg',0)
img2 = img.copy()
template = cv.imread('eyes.jpg',0)
w, h = template.shape[::-1]
# 6种比较方法
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
i = 'a'
for meth in methods:
img = img2.copy()
method = eval(meth)
# 模板匹配
res = cv.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
# 如果是 TM_SQDIFF 或 TM_SQDIFF_NORMED,则取最小值;反之,最大值
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
# 目标右下角坐标
bottom_right = (top_left[0] + w, top_left[1] + h)
# 绘制目标矩形边界
cv.rectangle(img,top_left, bottom_right, 255, 2)
# 结果显示
plt.subplot(121),plt.imshow(res,cmap = 'gray'),plt.title('Matching Result')
plt.subplot(122),plt.imshow(img,cmap = 'gray'),plt.title('Detected Point')
# plt.suptitle(meth)
plt.savefig('fig8-13%s.png'%i,bbox_inches='tight')
i=chr(ord(i)+1)
plt.show()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。