#-*- coding: utf-8 -*-
import os
import cv2 as cv
import numpy as np
tpl = cv.imdecode(np.fromfile("static/ym.jpg",dtype=np.uint8),1)
cv.imshow("ym", tpl)
target = cv.imdecode(np.fromfile("static/3.jpg",dtype=np.uint8),1)
# methods = [cv.TM_SQDIFF_NORMED, cv.TM_CCORR_NORMED, cv.TM_CCOEFF_NORMED] #3种模板匹配方法
methods = cv.TM_SQDIFF_NORMED
th, tw = tpl.shape[:2]
#模板匹配
result = cv.matchTemplate(target, tpl, methods)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
if methods == cv.TM_SQDIFF_NORMED:
tl = min_loc
else:
tl = max_loc
br = (tl[0]+tw, tl[1]+th) #br是矩形右下角的点的坐标
cv.putText(target,str([tl[0]+(tw/2),br[1]]), (tl), cv.FONT_HERSHEY_SIMPLEX, 2, (98, 170, 255), 2)
cv.rectangle(target, tl, br, (0, 255, 0), 2)
img = target[:tl[1]+th,:]
cv.namedWindow("match" ,cv.WINDOW_NORMAL)
cv.imshow("match" , target)
cv.waitKey(0)
cv.destroyAllWindows()
显示结果如下:


本文深入探讨了Python中图像与模型匹配的技术,包括关键点检测、特征描述符和匹配算法。通过实例展示了如何使用OpenCV库进行图像处理,实现图像之间的精确匹配,这对于图像识别、图像拼接等应用至关重要。
最低0.47元/天 解锁文章
1898

被折叠的 条评论
为什么被折叠?



