python中霍夫直线提取的函数
时间: 2023-08-07 20:21:39 浏览: 109
Python中OpenCV库提供了霍夫直线变换的函数`cv2.HoughLines()`和`cv2.HoughLinesP()`。
`cv2.HoughLines()`可以用于标准霍夫直线变换,其参数为二值化图像、距离分辨率rho、角度分辨率theta、阈值threshold,返回值为一个包含rho和theta值的数组。
`cv2.HoughLinesP()`可以用于概率霍夫直线变换,其参数为二值化图像、距离分辨率rho、角度分辨率theta、阈值threshold、最小直线长度minLineLength和最大直线间隔maxLineGap,返回值为直线的起点和终点坐标。
示例代码:
```
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# 标准霍夫直线变换
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)
# 概率霍夫直线变换
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文
相关推荐


















