python opencv 模板匹配

注意判断读取文件是否成功用 img is None 不是 img == None:
 



import cv2


def displayOneImage() :
    srcFile = "F:\\deep\data\\dataForStudyOpencv\\1_869307B00644_20190312_052816_410.bmp"
    img = cv2.imread( srcFile )
    if img is None :
        print("Fail to load pic")
        return 
    winName = 'displayOneImage'
    cv2.namedWindow( winName , cv2.WINDOW_NORMAL ) 
    cv2.imshow( winName , img )

def findTemplate() :
    srcFile = "F:\\deep\data\\dataForStudyOpencv\\1_869307B00644_20190312_052816_410.bmp"
    dstFile = "F:\\deep\data\\dataForStudyOpencv\\1_869307B00644_20190312_052816_410_templateMarked.png"
    templateFile =  "F:\\deep\data\\dataForStudyOpencv\\template1.png"
    img = cv2.imread( srcFile )
    if img is None :
        return ;
    imgTempl = cv2.imread( templateFile ) ;
    w,h = imgTempl.shape[:-1]
    print( "imgTempl.shape:" , imgTempl.shape ) 
    print("w,h:" , w,h ) 
    find = cv2.matchTemplate( img , imgTempl , cv2.TM_CCORR_NORMED )
    minVal , maxVal , minLoc , maxLoc = cv2.minMaxLoc( find )
    bottom_right = (maxLoc[0] + w , maxLoc[1]+h)
    print('maxVal:', maxVal , 'maxLoc:' , maxLoc )
    print('bottom_right:', bottom_right ) 
    cv2.rectangle( img , maxLoc , bottom_right , (255,255,255) , 20 )
    winName = 'findTemplate'
    cv2.namedWindow( winName , cv2.WINDOW_NORMAL ) 
    cv2.imshow( winName , img )
    cv2.imwrite( dstFile , img )
    

def loop() :
    k = cv2.waitKey(0)
    if ( k== 27 ) :
        cv2.destroyAllWindows()
    

def main() :
    displayOneImage()
    findTemplate()
    loop()

main() 



import cv2


def displayOneImage() :
    srcFile = "F:\\deep\data\\dataForStudyOpencv\\1_869307B00644_20190312_052816_410.bmp"
    img = cv2.imread( srcFile )
    if img is None :
        print("Fail to load pic")
        return 
    winName = 'displayOneImage'
    cv2.namedWindow( winName , cv2.WINDOW_NORMAL ) 
    cv2.imshow( winName , img )

def findTemplate() :
    srcFile = "F:\\deep\data\\dataForStudyOpencv\\1_869307B00644_20190312_052816_410.bmp"
    dstFile = "F:\\deep\data\\dataForStudyOpencv\\1_869307B00644_20190312_052816_410_templateMarked.png"
    templateFile =  "F:\\deep\data\\dataForStudyOpencv\\template1.png"
    img = cv2.imread( srcFile )
    if img is None :
        return ;
    imgTempl = cv2.imread( templateFile ) ;
    w,h = imgTempl.shape[:-1]
    print( "imgTempl.shape:" , imgTempl.shape ) 
    print("w,h:" , w,h ) 
    find = cv2.matchTemplate( img , imgTempl , cv2.TM_CCORR_NORMED )
    minVal , maxVal , minLoc , maxLoc = cv2.minMaxLoc( find )
    bottom_right = (maxLoc[0] + w , maxLoc[1]+h)
    print('maxVal:', maxVal , 'maxLoc:' , maxLoc )
    print('bottom_right:', bottom_right ) 
    cv2.rectangle( img , maxLoc , bottom_right , (255,255,255) , 20 )
    winName = 'findTemplate'
    cv2.namedWindow( winName , cv2.WINDOW_NORMAL ) 
    cv2.imshow( winName , img )
    cv2.imwrite( dstFile , img )
    

def loop() :
    k = cv2.waitKey(0)
    if ( k== 27 ) :
        cv2.destroyAllWindows()
    

def main() :
    displayOneImage()
    findTemplate()
    loop()

main() 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值