0
点赞
收藏
分享

微信扫一扫

【opencv】模板匹配

闲云困兽 2022-01-20 阅读 90
from email.mime import image
import imghdr
from turtle import title
import cv2
import matplotlib.pyplot as plt
import numpy as np

def cv_show(img,name):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


#模板匹配
def template_match(img,template):
    res=cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED) # 参数为输入图像  模板  参数为匹配方法
    min_val,max_val,min_loc,max_loc=cv2.minMaxLoc(res) # 参数为输入图像  参数为输出最小值  参数为输出最大值  参数为输出最小值的位置  参数为输出最大值的位置
    return max_val,max_loc

img=cv2.imread('lena.jpg')  # 读取图片
template=cv2.imread('lena_part.png') # 读取图片 0表示灰度图 1表示彩色图

max_val,max_loc=template_match(img,template)
top_left=max_loc
bottom_right=(top_left[0]+template.shape[1],top_left[1]+template.shape[0])
cv2.rectangle(img,top_left,bottom_right,(0,0,255),2)
cv_show(img,'after_template_match')
#保存
cv2.imwrite('lena_template_match.jpg',img)

 

 

 

举报

相关推荐

0 条评论