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://img-blog.csdnimg.cn/d1a20946d6c84677a46b892d4e97b4d9.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16)
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://img-blog.csdnimg.cn/54b4d72016f540419da8162ac5a6873d.png)
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://img-blog.csdnimg.cn/dd41a4a1396b4fc99b5923ed2c347877.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_12,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PcqFKOLO-1637755286455)(attachment:image.png)]](https://img-blog.csdnimg.cn/859b5ed64c0b4875b2c8814470f53915.png)
2.霍夫变换
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hu8nPD6f-1637755286455)(attachment:image.png)]](https://img-blog.csdnimg.cn/b4628ba631d543dba66be5dbc12e6183.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16)
2.1霍夫线检测
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7krQq91B-1637755286457)(attachment:image.png)]](https://img-blog.csdnimg.cn/21605b12f1fe4902afe0868e05e10d91.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16)
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://img-blog.csdnimg.cn/25e0c118cacc4508b2e88e963d371abd.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_11,color_FFFFFF,t_70,g_se,x_16)
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://img-blog.csdnimg.cn/0760f9da8a674276bd15c2b26278be49.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_11,color_FFFFFF,t_70,g_se,x_16)
2.2霍夫圆检测
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yNjC6N2a-1637755286459)(attachment:image.png)]](https://img-blog.csdnimg.cn/9d84e123cc694a9b86faef89ce5e5572.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nQWb8hod-1637755286460)(attachment:image.png)]](https://img-blog.csdnimg.cn/7aa90670cf1d4befa4af41e1db25ae07.png)
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://img-blog.csdnimg.cn/f9158d99c4fe4e81bc9f5587af8db717.png)
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://img-blog.csdnimg.cn/5f2de66fcab54aa497d0e597152e8739.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16)
总结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CkopBbcn-1637755286463)(attachment:image.png)]](https://img-blog.csdnimg.cn/39b2294d783a4e53a6fa03e00fab5ee5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16)
|