OpenCV-Python 图像的模板匹配和霍夫变换

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)]

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)]

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)
# 根据左上角点和右上角点,绘制宽度为2的绿色矩形
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)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PcqFKOLO-1637755286455)(attachment:image.png)]

2.霍夫变换

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hu8nPD6f-1637755286455)(attachment:image.png)]

2.1霍夫线检测

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7krQq91B-1637755286457)(attachment:image.png)]

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)]

# 霍夫线检测 cv.HoughLines(要检测的图片,精度,每次检测的角度精度,阈值(只有大于阈值才认为是直线))
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)]

2.2霍夫圆检测

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yNjC6N2a-1637755286459)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nQWb8hod-1637755286460)(attachment:image.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)]

# 进行霍夫圆检测
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)]

总结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CkopBbcn-1637755286463)(attachment:image.png)]

猜你喜欢

转载自blog.csdn.net/weixin_51545953/article/details/121523620