目录
- 模版匹配
- 原理
- 实现
- 霍夫变换
- 霍夫线检测
模版匹配
原理
实现
res=cv.matchTemplate(img,template,method)
import numpy as np
import cv2 as cv
import matplotlib.pyplot as pltimg=cv.imread('./汪学长的随堂资料/6/模板匹配/lena.jpg')
template=cv.imread('./汪学长的随堂资料/6/模板匹配/face.jpg')
res=cv.matchTemplate(img,template,cv.TM_CCORR)
min_val,max_val,min_loc,max_loc=cv.minMaxLoc(res)
top_left=max_loc
h,w=template.shape[:2]
bottom_right=(top_left[0]+w,top_left[1]+h)
cv.rectangle(img,top_left,bottom_right,[0,255,0],2)
plt.imshow(img[:,:,::-1])
注:
霍夫变换
霍夫线检测
cv.HoughLines(img,rho,theta,threshold)
img=cv.imread('./image/rili,jpg',0)
edges=cv.Canny(img,50,150)
lines=cv.HoughLines(edges,0.8,np.pi/180,150)
for line in lines:rho,theta=line[0]a=np.cos(theta)b=np.sin(theta)x0=rho*ay0=rho*bx1=int(x0+1000*(-b))y1=int(y0+1000*a)x2=int(x0-1000*(-b))y2=int(y0-1000*a)cv.line(img,(x1,y1),(x2,y2),(0,255,0))
plt.imshow(img[:,:,::-1])