OpenCV 车道线提取
生活随笔
收集整理的這篇文章主要介紹了
OpenCV 车道线提取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在車道線檢測中,使用的是HSL顏色空間,其中H表示色相,即顏色,S表示飽和度,即顏色的純度,L表示顏色的明亮程度。
本案例基于圖像的梯度和顏色特征,定位車道線的位置。
在這里選用Sobel邊緣提取算法,Sobel相比于Canny的優秀之處在于,它可以選擇橫向或縱向的邊緣進行提取。從車道的拍攝圖像可以看出,車道線在橫向上的邊緣突變是需要關心的問題。OpenCV提供的cv2.Sobel()函數,將進行邊緣提取后的圖像做二進制圖的轉化,即提取到邊緣的像素點顯示為白色(值為1),未提取到邊緣的像素點顯示為黑色(值為0)。由于只使用邊緣檢測,在有樹木陰影覆蓋的區域時,雖然能提取出車道線的大致輪廓,但會同時引入的噪聲,給后續處理帶來麻煩。所以在這里引入顏色閾值來解決這個問題。
import cv2 import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimgdef pipeline(img, s_thresh=(170, 255), sx_thresh=(50, 300)):img = np.copy(img)#1.將圖像轉換為HLS色彩空間,并分離各個通道hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(np.float64)h_channel = hls[:, :, 0]l_channel = hls[:, :, 1]s_channel = hls[:, :, 2]#2.利用sobel計算x方向的梯度sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0)abs_sobelx = np.absolute(sobelx)# 將導數轉換為8bit整數scaled_sobel = np.uint8(255 * abs_sobelx / np.max(abs_sobelx))sxbinary = np.zeros_like(scaled_sobel)sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1# 3.對s通道進行閾值處理s_binary = np.zeros_like(s_channel)s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1# 4. 將邊緣檢測的結果和顏色空間閾值的結果合并,并結合l通道的取值,確定車道提取的二值圖結果color_binary = np.dstack((np.zeros_like(sxbinary), sxbinary, s_binary))return color_binarydef draw_images(img, undistorted, title, cmap):f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))f.tight_layout()ax1.imshow(img)ax1.set_title('Original Image', fontsize=50)if cmap is not None:ax2.imshow(undistorted, cmap=cmap)else:ax2.imshow(undistorted)ax2.set_title(title, fontsize=50)plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)plt.show()if __name__ == '__main__':image = mpimg.imread('./img.png')pipeline_img = pipeline(image)draw_images(image, pipeline_img, 'Pipeline Result', None)代碼運行結果:
總結:
顏色空間:
HLS:色相,飽和度,明亮程度
車道線提取
顏色空間轉換 → 邊緣檢測 → 顏色閾值 → 合并得到檢測結果。
注:NumPy 1.20.0 后的版本 棄用了 np.float,需要改為 np.float64
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
| numpy.bool | bool | numpy.bool_ |
| numpy.int | int | numpy.int_(默認)numpy.int64,或numpy.int32 |
| numpy.float | float | numpy.float64,numpy.float_,numpy.double |
| numpy.complex | complex | numpy.complex128,numpy.complex_,numpy.cdouble |
| numpy.object | object | numpy.object_ |
| numpy.str | str | numpy.str_ |
| numpy.long | int | numpy.int_,numpy.longlong |
| numpy.unicode | str | numpy.unicode_ |
總結
以上是生活随笔為你收集整理的OpenCV 车道线提取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据特征预处理
- 下一篇: OpenCV 相机校正