python 模板匹配对比
【摘要】 对于形变,以下两种 相关系数的效果比较好:
cv2.TM_CCOEFF cv2.TM_CCOEFF_NORMED
其余四种会误检。
平方差匹配CV_TM_SQDIFF:用两者的平方差来匹配,最好的匹配值为0归一化平方差匹配CV_TM_SQDIFF_NORMED相关匹配CV_TM_CCORR:用两者的乘积匹配,数值越大表明匹配程度越好归一化相关匹配CV_T...
对于形变,以下两种 相关系数的效果比较好:
cv2.TM_CCOEFF
cv2.TM_CCOEFF_NORMED
其余四种会误检。
- 平方差匹配CV_TM_SQDIFF:用两者的平方差来匹配,最好的匹配值为0
- 归一化平方差匹配CV_TM_SQDIFF_NORMED
- 相关匹配CV_TM_CCORR:用两者的乘积匹配,数值越大表明匹配程度越好
- 归一化相关匹配CV_TM_CCORR_NORMED
- 相关系数匹配CV_TM_CCOEFF:用两者的相关系数匹配,1表示完美的匹配,-1表示最差的匹配
- 归一化相关系数匹配CV_TM_CCOEFF_NORMED
-
# -*- coding:utf-8 -*-
-
__author__ = 'Microcosm'
-
-
import cv2
-
import numpy as np
-
from matplotlib import pyplot as plt
-
-
-
img = cv2.imread(r"e:/new/n3.jpg",0)
-
-
template = cv2.imread(r"e:/new/muban3.jpg",0)
-
-
img2 = img.copy()
-
w,h = template.shape[::-1]
-
-
# 6 中匹配效果对比算法
-
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.imshow("Point",img)
-
cv2.imshow("Matching Result",res)
-
cv2.waitKeyEx()
-
print(meth)
-
# plt.subplot(221), plt.imshow(img2,cmap= "gray")
-
# plt.title('Original Image'), plt.xticks([]),plt.yticks([])
-
# plt.subplot(222), plt.imshow(template,cmap= "gray")
-
# plt.title('template Image'),plt.xticks([]),plt.yticks([])
-
# 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.show()
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/95319446
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)