版权声明: https://blog.csdn.net/Wang_Jiankun/article/details/82355344
Python-opencv 笔记2 – 霍夫变换(Hough)
1、标准霍夫变换 HoughLinesP
函数原型:
HoughLines(image, rho, theta, threshold, lines=None, srn=None, stn=None, min_theta=None, max_theta=None)
- lines :存储检测结果,直线用半径和角度表示
2、概率霍夫变换 HoughLinesP
函数原型:
HoughLinesP(image, rho, theta, threshold, lines=None, minLineLength=None, maxLineGap=None)
- image:待检测的图像
- rho:直线半径的搜索步长,单位为像素
- theta:直线角度使得搜索步长,单位为弧度
- threshold:累加阈值,只有属于同一直线的点数超过该阈值才会被检测为直线
- lines :存储检测结果1x1x4的矩阵,直线用两点坐标表示(x1, y1, x2, y2)
- minLineLength :检测为直线的最短长度,单位为像素
- maxLineGap:直线断点的最大距离
import cv2
import numpy as np
# 读取图像>高斯模糊>边缘检测
img = cv2.imread("1.jpg")
img = cv2.GaussianBlur(img, (3,3), 0)
edges = cv2.Canny(img, 50, 150, apertureSize = 3)
# 概率霍夫变换
minLineLength = 200
maxLineGap = 15
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 80, minLineLength, maxLineGap)
lines = np.squeeze(lines)
for x1,y1,x2,y2 in lines:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()