python车道线检测
时间: 2023-12-14 14:34:42 浏览: 93
以下是Python实现车道线检测的步骤:
1. 图像加载和预处理:使用OpenCV库中的cv2.imread()函数加载图像,并将其转换为灰度图像。然后使用高斯滤波器平滑图像,以减少噪声。
```python
import cv2
import numpy as np
# 加载图像
img = cv2.imread('test.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 高斯滤波
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
```
2. Canny边缘检测:使用Canny算法检测图像中的边缘。
```python
# Canny边缘检测
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
```
3. 感兴趣区域检测:提取感兴趣区域(ROI),以便在后续步骤中检测车道线。
```python
# 定义感兴趣区域的四个顶点坐标
imshape = img.shape
vertices = np.array([[(0, imshape[0]), (450, 320), (490, 320), (imshape[1], imshape[0])]], dtype=np.int32)
# 创建一个掩膜,只保留感兴趣区域内的像素
mask = np.zeros_like(edges)
if len(img.shape) > 2:
channel_count = img.shape[2]
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
cv2.fillPoly(mask, vertices, ignore_mask_color)
masked_edges = cv2.bitwise_and(edges, mask)
```
4. 霍夫直线检测:使用霍夫变换检测图像中的直线。
```python
# 霍夫直线检测
rho = 2
theta = np.pi / 180
threshold = 15
min_line_length = 40
max_line_gap = 20
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
```
5. 直线拟合:将检测到的直线拟合成车道线。
```python
# 直线拟合
left_lines = []
right_lines = []
for line in lines:
for x1, y1, x2, y2 in line:
k = (y2 - y1) / (x2 - x1)
if k < 0:
left_lines.append(line)
else:
right_lines.append(line)
left_points = np.array([[(x1, y1) for x1, y1, x2, y2 in left_lines]]).reshape(-1, 2)
right_points = np.array([[(x2, y2) for x1, y1, x2, y2 in right_lines]]).reshape(-1, 2)
left_fit = np.polyfit(left_points[:, 0], left_points[:, 1], 1)
right_fit = np.polyfit(right_points[:, 0], right_points[:, 1], 1)
```
6. 车道线叠加:将检测到的车道线叠加到原始图像上。
```python
# 车道线叠加
line_img = np.zeros_like(img)
y1 = img.shape[0]
y2 = int(y1 * 0.6)
left_x1 = int((y1 - left_fit[1]) / left_fit[0])
left_x2 = int((y2 - left_fit[1]) / left_fit[0])
right_x1 = int((y1 - right_fit[1]) / right_fit[0])
right_x2 = int((y2 - right_fit[1]) / right_fit[0])
cv2.line(line_img, (left_x1, y1), (left_x2, y2), (255, 0, 0), 10)
cv2.line(line_img, (right_x1, y1), (right_x2, y2), (255, 0, 0), 10)
result = cv2.addWeighted(img, 0.8, line_img, 1, 0)
```
7. 图片和视频测试:使用上述步骤处理单张图片或视频。
```python
# 图片测试
cv2.imwrite('result.jpg', result)
# 视频测试
cap = cv2.VideoCapture('test.mp4')
while cap.isOpened():
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
mask = np.zeros_like(edges)
cv2.fillPoly(mask, vertices, ignore_mask_color)
masked_edges = cv2.bitwise_and(edges, mask)
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)
left_lines = []
right_lines = []
for line in lines:
for x1, y1, x2, y2 in line:
k = (y2 - y1) / (x2 - x1)
if k < 0:
left_lines.append(line)
else:
right_lines.append(line)
left_points = np.array([[(x1, y1) for x1, y1, x2, y2 in left_lines]]).reshape(-1, 2)
right_points = np.array([[(x2, y2) for x1, y1, x2, y2 in right_lines]]).reshape(-1, 2)
left_fit = np.polyfit(left_points[:, 0], left_points[:, 1], 1)
right_fit = np.polyfit(right_points[:, 0], right_points[:, 1], 1)
line_img = np.zeros_like(frame)
y1 = frame.shape[0]
y2 = int(y1 * 0.6)
left_x1 = int((y1 - left_fit[1]) / left_fit[0])
left_x2 = int((y2 - left_fit[1]) / left_fit[0])
right_x1 = int((y1 - right_fit[1]) / right_fit[0])
right_x2 = int((y2 - right_fit[1]) / right_fit[0])
cv2.line(line_img, (left_x1, y1), (left_x2, y2), (255, 0, 0), 10)
cv2.line(line_img, (right_x1, y1), (right_x2, y2), (255, 0, 0), 10)
result = cv2.addWeighted(frame, 0.8, line_img, 1, 0)
cv2.imshow('result', result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
```
阅读全文