opencv调取摄像头进行车道线检测
时间: 2023-08-02 16:05:58 浏览: 166
首先需要安装OpenCV库,然后使用以下代码调取摄像头进行车道线检测:
```
import cv2
import numpy as np
cap = cv2.VideoCapture(0) # 0表示调取电脑自带摄像头
while True:
_, frame = cap.read() # 读取摄像头画面
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 将画面转为灰度图像
blur = cv2.GaussianBlur(gray, (5, 5), 0) # 高斯模糊
edges = cv2.Canny(blur, 50, 150) # 边缘检测
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=50) # 霍夫直线检测
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) # 绘制检测到的车道线
cv2.imshow("Lane Detection", frame) # 显示结果
if cv2.waitKey(1) == ord('q'): # 按q键退出
break
cap.release()
cv2.destroyAllWindows()
```
以上代码会实时调取摄像头画面并进行车道线检测,检测到的车道线会标记在画面上。可以根据实际需求调整参数,如高斯模糊的核大小、Canny边缘检测的阈值等。
阅读全文
相关推荐

















