提升网站访问量,如何做网站解析,做公众号用什么网站吗,广告公司的名字怎么起好【1】引言
前序学习进程中#xff0c;已经探索了使用cv.matchTemplate()函数实现最佳图像匹配的技巧#xff0c;并且成功对两个目标进行了匹配。
相关文章链接为#xff1a;python学opencv|读取图像#xff08;五十二#xff09;使用cv.matchTemplate()函数实现最佳图像…【1】引言
前序学习进程中已经探索了使用cv.matchTemplate()函数实现最佳图像匹配的技巧并且成功对两个目标进行了匹配。
相关文章链接为python学opencv|读取图像五十二使用cv.matchTemplate()函数实现最佳图像匹配-CSDN博客
实际上我们在这篇文章中重点体会了匹配效果却没有真正剖析代码背后的运行逻辑。今天这篇文章的目标就是对代码背后逻辑稍微追溯一下。
【2】官网教程
【2.1】cv2.matchTemplate()函数
点击下方链接直达cv2.matchTemplate()函数官网链接 图1 cv2.matchTemplate()函数官网说明
图1所示的cv2.matchTemplate()函数官网说明中有三处做了标记它们彼此交织在一起。需要解读
a.待匹配的大图像I大小为W X H使用的模板T像素大小为w x h获得的匹配效果R对应的的矩阵大小为(W-w1,H-h1)
b.使用不同的匹配方法后再用minMaxLoc函数读取最佳匹配效果对应的左上角坐标时有时候取最小值如TM_SQDIFF有时候取最大值如TM_CCORR和TM_CCOEFF。
c.解读匹配方法请看第2.2节。
【2.2】cv2.matchTemplate()函数
点击链接直达函数对匹配方法的解读OpenCV: Object Detection
在这个页面会看到不同的函数说明 图2 匹配方法的数学公式
由图2可见TM_SQDIFF采用的是减法计算而TM_CCORR和TM_CCOEFF采用的乘法计算所以相似度高的时候TM_SQDIFF方法的计算值往往会接近0而TM_CCORR和TM_CCOEFF方法就会在因为平方而取得更大的值。
所以“用minMaxLoc函数读取最佳匹配效果对应的左上角坐标时有时候取最小值如TM_SQDIFF有时候取最大值如TM_CCORR和TM_CCOEFF”就获得了解释。
【3】代码测试
【3.1】代码回顾
首先直接引用前一篇文章的完整代码
import cv2 as cv # 引入CV模块
import numpy as np #引入numpy模块# 读取图片
srcm cv.imread(srcm.png) #读取图像srcx.png
srcg cv.imread(srcg.png) #读取图像srcp.png
srcc cv.imread(srcc.png) #读取图像srcp.png
rows,cols,canssrcg.shape #读取图像属性
rowsc,colsc,canscsrcc.shape #读取图像属性#匹配结果
resultscv.matchTemplate(srcm,srcg,cv.TM_CCORR_NORMED)
results1cv.matchTemplate(srcm,srcc,cv.TM_CCORR_NORMED)#取值
minValue,maxValue,minLoc,maxLoccv.minMaxLoc(results)
minValuec,maxValuec,minLocc,maxLocccv.minMaxLoc(results1)#取最大坐标
resultPoint1maxLoc
print(resultPoint1,resultPoint1)#取最大坐标
resultPoint2maxLocc
print(resultPoint2,resultPoint2)#定义新坐标
resultPoint3(resultPoint1[0]cols,resultPoint1[1]rows)
print(resultPoint3,resultPoint3)#定义新坐标
resultPoint4(resultPoint2[0]colsc,resultPoint2[1]rowsc)
print(resultPoint4,resultPoint4)#作标记
cv.circle(srcm,(250,250),30,(0,255,0))
cv.rectangle(srcm,resultPoint1,resultPoint3,(0,255,0),2)
cv.rectangle(srcm,resultPoint2,resultPoint4,(200,180,55),2)# 显示结果
cv.imshow(srcm , srcm)
cv.imshow(srcg , srcg)
cv.imshow(srcc , srcc)
cv.imwrite(srcgc.png,srcm)#窗口控制
cv.waitKey() # 图像不关闭
cv.destroyAllWindows() # 释放所有窗口
待匹配的图像I为 图3 待匹配图像Isrcm.png 图4 模板T1 srcg.png 图5 模板T2 srcc.png 图6 匹配效果 srcgc.png
上述代码全部使用了cv2.TM_CCORR_NORMED方法所以需要调用最大值来代表最佳匹配效果的左上角坐标。
未验证不用方法对应最佳匹配效果的左上角坐标现在应增加匹配方法。
【3.2】代码扩展
在直接引用前一篇文章的完整代码的基础上不仅要增加匹配方法还要显示出匹配结果。
#匹配计算
resultscv.matchTemplate(srcm,srcg,cv.TM_SQDIFF_NORMED) #TM_SQDIFF匹配方法
results1cv.matchTemplate(srcm,srcc,cv.TM_CCORR_NORMED) #TM_CCORR匹配方法
print(result,results) #输出匹配结果
print(result1,results1) #输出匹配结果
代码先后使用了TM_SQDIFF和TM_CCORR两种方法并且要求输出了匹配结果。
然后读取了调用minMaxLoc()函数对结果渠道的各个参数值
#取值
minValue,maxValue,minLoc,maxLoccv.minMaxLoc(results)
minValuec,maxValuec,minLocc,maxLocccv.minMaxLoc(results1)
print(result.minValue,minValue)
print(result1.minValuec,minValuec)
print(result.maxValue,maxValue)
print(result1.maxValuec,maxValuec)
print(result.minLoc,minLoc)
print(result1.minLocc,minLocc)
print(result.maxLoc,maxLoc)
print(result1.maxLocc,maxLocc)
然后根据先前的分析思路取最佳匹配矩阵的左上角坐标。
这时候TM_SQDIFF取最小值TM_CCORR方法取最大值之后还要叠加模板的大小来画出整个匹配区域
#取最小坐标
resultPoint1minLoc
print(resultPoint1,resultPoint1)#取最大坐标
resultPoint2maxLocc
print(resultPoint2,resultPoint2)#定义新坐标
resultPoint3(resultPoint1[0]cols,resultPoint1[1]rows)
print(resultPoint3,resultPoint3)#定义新坐标
resultPoint4(resultPoint2[0]colsc,resultPoint2[1]rowsc)
print(resultPoint4,resultPoint4)
之后为了突出匹配点以最小和最大坐标Wie圆心分别绘制半径为10和20的圆形
#作标记
cv.circle(srcm,(minLoc),10,(255,255,0))
cv.circle(srcm,(maxLoc),20,(255,255,0))
cv.circle(srcm,(minLocc),10,(0,255,255))
cv.circle(srcm,(maxLocc),20,(0,255,255))
cv.circle(srcm,(250,250),30,(0,255,0))
cv.rectangle(srcm,resultPoint1,resultPoint3,(0,255,0),2)
cv.rectangle(srcm,resultPoint2,resultPoint4,(200,180,55),2)
然后输出所有图像
# 显示结果
cv.imshow(srcm , srcm)
cv.imwrite(srcgcw.png,srcm)
#窗口控制
cv.waitKey() # 图像不关闭
cv.destroyAllWindows() # 释放所有窗口
代码运行后获得的匹配效果为 图7 匹配效果srcgcw.png
由图7可见TM_SQDIFF取最小值TM_CCORR方法取最大值获得的最佳匹配图像实现了预期效果。
【4】细节说明
上述3.2节读取到的部分匹配结果矩阵为 图8 匹配结果矩阵
由图8可见每个矩阵内部给出了很多值这表明在矩阵内部图像和模板是按照像素点逐个进行比对匹配。
【5】总结
掌握了pythonopencv调用使用cv.matchTemplate()函数实现最佳图像匹配的执行原理和过程。 文章转载自: http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.gwzfj.cn.gov.cn.gwzfj.cn http://www.morning.zmwzg.cn.gov.cn.zmwzg.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.ktskc.cn.gov.cn.ktskc.cn http://www.morning.mprtj.cn.gov.cn.mprtj.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.kpgms.cn.gov.cn.kpgms.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.bnpn.cn.gov.cn.bnpn.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.lwzpp.cn.gov.cn.lwzpp.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.cykqb.cn.gov.cn.cykqb.cn http://www.morning.wnhml.cn.gov.cn.wnhml.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn