import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
1.模板匹配
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mKnZEjAv-1637755286449)(attachment:image.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9948800396bf48d6a206187bdf8f1f6a~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
wulin = cv.imread('img/wulin.jpeg')
template = cv.imread('img/bai.jpeg')
res = cv.matchTemplate(wulin,template,cv.TM_CCORR)
plt.imshow(res,cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x24d7c137af0>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vKnNj3aH-1637755286451)(output_5_1.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f3d85a07345942819d56d3990eb624f7~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
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)
res = cv.rectangle(wulin,top_left,bottom_right,(0,255,0),2)
plt.imshow(res[:,:,::-1])
<matplotlib.image.AxesImage at 0x24d7cc2ccd0>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rdVCmsKD-1637755286453)(output_8_1.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c0c8e5095f2649e782063e83b73cb605~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PcqFKOLO-1637755286455)(attachment:image.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3949525a287944278aaa4043c56bd919~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
2.霍夫变换
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hu8nPD6f-1637755286455)(attachment:image.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/69e91eca614849c08598a93136d5ae90~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
2.1霍夫线检测
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7krQq91B-1637755286457)(attachment:image.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2131c5abae20477e8a27bb5745d1cc89~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
rili = cv.imread('img/rili.jpg')
edges = cv.Canny(rili,50,150)
plt.imshow(edges,cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x24d7c177640>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5YPYbxDV-1637755286458)(output_16_1.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a470e898d50548b69e4caf1051bbcdad~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
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 * a
y0 = rho * b
x1 = int(x0+1000*(-b))
y1 = int(y0+1000*a)
x2 = int(x0-1000*(-b))
y2 = int(y0-1000*a)
cv.line(rili,(x1,y1),(x2,y2),(0,255,0))
plt.imshow(rili[:,:,::-1])
<matplotlib.image.AxesImage at 0x24d7f021c70>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YIdiBic5-1637755286459)(output_19_1.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c85513106d3947dc861a9653c9139b6c~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
2.2霍夫圆检测
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yNjC6N2a-1637755286459)(attachment:image.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d5c7121db789418b88b06630afc76f50~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nQWb8hod-1637755286460)(attachment:image.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d917c2eb8ee84cf1aea7aca73dca130e~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
star = cv.imread('img/star.jpeg')
gray_star = cv.cvtColor(star,cv.COLOR_BGR2GRAY)
img = cv.medianBlur(gray_star,7)
plt.imshow(img,cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x24d7efc20d0>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5ciKJzIe-1637755286461)(output_26_1.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9c20d544343f40b0af06bcfbe315cd99~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
circles = cv.HoughCircles(img,cv.HOUGH_GRADIENT,1,200,param1=100,param2=50,minRadius=0,maxRadius=200)
circles
array([[[494.5, 820.5, 71.7],
[494.5, 596.5, 71.3],
[221.5, 370.5, 72. ],
[221.5, 596.5, 71.2],
[773.5, 820.5, 71.6],
[494.5, 371.5, 71.6],
[773.5, 369.5, 70.7],
[220.5, 820.5, 71.5],
[774.5, 594.5, 70.6],
[362.5, 145.5, 64.5],
[632.5, 138.5, 63.3]]], dtype=float32)
for c in circles[0,:]:
cv.circle(star,(int(c[0]),int(c[1])),int(c[2]),(0,255,0),2)
cv.circle(star,(int(c[0]),int(c[1])),2,(255,0,0),-1)
plt.figure(dpi=400)
plt.imshow(star[:,:,::-1])
<matplotlib.image.AxesImage at 0x24d7f0361f0>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6EmjUYrT-1637755286462)(output_29_1.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4a851a1081db40b490e085da93aa4cc8~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)
总结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CkopBbcn-1637755286463)(attachment:image.png)]](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8b9066b080414dc4b1fde64df2b3c1f1~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp)