python实现Hough直线检测与曲线检测代码
时间: 2024-05-12 22:17:49 浏览: 174
当然可以,你需要使用opencv库中的HoughLines和HoughLinesP函数。以下是一个示例代码:
```python
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# Hough直线检测
lines = cv2.HoughLines(edges, 1, 0.01, 150)
for line in lines:
rho, theta = line[0]
a, b = np.cos(theta), np.sin(theta)
x0, y0 = a * rho, b * rho
x1, y1 = int(x0 + 1000 * (-b)), int(y0 + 1000 * a)
x2, y2 = int(x0 - 1000 * (-b)), int(y0 - 1000 * a)
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# Hough曲线检测
lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi/180, threshold=50, minLineLength=50, maxLineGap=10)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow('result', img)
cv2.waitKey(0)
```
这段代码会读取名为image.jpg的图片,并在其上进行直线和曲线检测,并将结果显示出来。
阅读全文