基于Hough变换和角点检测的停车位精准检测的Python代码
时间: 2024-04-18 18:17:22 浏览: 82
基于Hough变换和圆形模板的棋盘格角点检测
以下是基于Hough变换和角点检测的停车位精准检测的Python代码:
```python
import cv2
import numpy as np
# 读取图片
img = cv2.imread('parking.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 边缘检测
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# 霍夫变换检测直线
lines = cv2.HoughLines(edges, 1, np.pi / 180, 50)
# 绘制直线
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# 角点检测
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
corners = np.int0(corners)
# 绘制角点
for corner in corners:
x, y = corner.ravel()
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
# 显示结果
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码首先读取图片并将其转换为灰度图像。然后使用Canny边缘检测算法进行边缘检测。接下来使用霍夫变换检测直线,并将检测到的直线绘制在原图上。最后使用goodFeaturesToTrack函数进行角点检测,并将检测到的角点绘制在原图上。
阅读全文