车道线检测-从单车道到多车道的车道线检测(二)

车道线检测

····这里是车道线检测第二篇,睿智小编,在线码字。
····车道线检测,从易到难:单直线车道检测–>单弯道车道检测–>多直线车道检测–>多弯道车道检测
····我更希望用例子去说明,多按图说话。本篇分三个内容:

···· 1.讲解Udacity的CarND-LaneLines-P1-master项目
···· 2.讲解Udacity的CarND-Advanced-Lane-Lines-master项目
···· 3.讲解我在这基础上改进的multi-lane-lines-detection项目

CarND-Advanced-Lane-Lines-master

····透视变换,在鸟瞰视角拟合车道线,效果很好但实时性较差的单车道线检测方法。
详细代码见 https://github.com/wisdom-bob/CarND-Advanced-Lane-Lines-master
车道线检测-从单车道到多车道的车道线检测(二)_第1张图片
····如图所示,为项目结果图,边缘拟合度高,并且车道两边几乎对称,符合弯道车道线检测结果,效果不错,但实时性较差,在无gpu下10.2帧/s,不过还是来看看这是怎么做的,如何改进,或者有哪些值得借鉴的地方。
····主要思路:对输入图像通过灰度化、去畸变、sobel算子在s-channel,gray图像上进行边缘检测、图像二值化;再框选出感兴趣区域,对感兴趣区域进行透视变换,基于滑动窗口法和统计直方图法得到车道线节点,拟合车道线节点,得到车道线,得到稳定输出结果。

摄像头标定

····透视变换是图像数据处理的一个大招了,想想最让你头疼的不就是图像是正视吗,你硬是要基于二维图像去考虑三维空间的事情,透视变换一弄-------->二维下考虑二维的事情,应该会轻松很多吧!
····但是要使用透视变换,也不是那么容易的,我们需要摄像头内参才可以,这里就来说一下如何标定摄像头拿到摄像头内参。
····说到摄像头标定,就一定要提到张正友老师了,这里对于张正友标定法并不详细展开说,简而言之,基于实际点坐标和对标像素点位置,基于极大似然估计计算图像的内参,数据点越多结果越准。这里推荐几篇比较好的标定blog,见转载12,在1中简洁明了的对张正友方法进行推导说明,2中有一点错误,仔细读下来也能帮助你理解。

在这里插入图片描述
如上图所示为标定的图片,通过以下代码,基于cv2.findChessboardCorners找到对标像素点,这里的像素坐标精确到小数点后3位(ps,事实上这里并不是简单的图像捕捉点,也是统计得到的结果,精确度算不得准,只是当单张表格点较多时结果更可信),对标点对应的ground_truth point,即objp点集,相当于标定板不动,相机移动,由于标定板间隔相同,这里直接简单设定间距为单位1.

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6 * 9, 3), np.float32)
objp[:,:2] = np.mgrid[0:9, 0:6].T.reshape(-1, 2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
img_with_corners = []
# Make a list of calibration images
images = glob.glob('./camera_cal/calibration*.jpg')

# Step through the list and search for chessboard corners
for i in range(len(images)):
    img = cv2.imread(images[i])
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
    ret, corners = cv2.findChessboardCorners(gray, (9,6), None)
    
    # If found, add object points, image points
    if ret == True:
        objpoints.append(objp)
        imgpoints.append(corners)

        # Draw and display the corners
        img = cv2.drawChessboardCorners(img, (9,6), corners, ret)
        img_with_corners.append(img)

车道线检测-从单车道到多车道的车道线检测(二)_第2张图片
如果算法能够准确检测到所有对标图像点,每张图像应都能画上如右图所示若干标记点,直线代表点位检测顺序。基于对标点集,就可以运用cv2.calibrateCamera计算摄像头内参矩阵和畸变矩阵等,当然也可以得到摄像头的外参(rvecs和tvecs),旋转向量和平移向量。

# mtx is Camera Matrix, dist is the distort arameter Vector
global mtx,dist

# calculate the mtx and dist by cv2.calibrateCamera
img = cv2.imread('camera_cal/calibration1.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_size = gray.shape[::-1]
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None)

/##################那些天趟过的坑##################/
这里想说明一下标定的一些坑点,关于cv2.findChessboardCorners和cv2.calibrateCamera,与整体内容无关,不感兴趣的可以直接跳过偶。
cv2.findchessboard,经过多次测试,对于仰视视角的图像检测结果较差如上图4,边界不完整检测效果也有影响如图5;这里的标定板是10x7,单张图片的标定点数54(9x6)个,最好不少于12个点,否则对标点精度会有影响;函数对于块格的间隔也有明确要求,检测点间最小距离>25像素;另外,如果明确感觉对标点精度不行,可以自行修改对标像素坐标,再跑cv2.calibrateCamera标定内参。
cv2.calibrateCamera,基于objpoints和imgpoints计算结果,当然数据是越多越好,它会逐渐趋向稳定,这里推荐cv2.projectPoints配合筛选元图像数据。另外说一下cv2.calibrateCamera最末尾参数 criteria,用于设定迭代终止条件,算法依据最小二乘法,利用输入的对应点集进行循环计算,不断修缮结果,但偶尔也会碰到无法计算的情况,即使findchessboard没有剔除图像,但是对于标定计算的过程中,也有一些图像缺陷会被暴露出来需要剔除,这些都需要在调试中慢慢进行。
总之,数据集是越大越好,至少有60张图像去筛选,最后可能得到20-30张有效图片,另外记得拍摄图像时要尽可能概括相机视野的每个地方,不要过于集中,防止权重失衡,导致内参标定差异。
/##################那些天趟过的坑##################/

以上只是个人见解,不喜勿喷,感谢

基于标定内参,对图像进行畸变矫正,如下图所示。

def cal_undistort(img):
    # convert image into gray scale
    undist = cv2.undistort(img, mtx, dist, None, mtx)
    return undist

undist = cal_undistort(img)
    
# Visualize undistortion
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))
ax1.imshow(img)
ax1.set_title('Original Image', fontsize=30)
ax2.imshow(undist)
ax2.set_title('Undistorted Image', fontsize=30)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)

车道线检测-从单车道到多车道的车道线检测(二)_第3张图片
到这里我们就完成了摄像头的标定,以上我们只用了摄像头畸变矫正,那要如何实现透视变换呢?让我们往下走吧!
车道线检测-从单车道到多车道的车道线检测(二)_第4张图片

思路讲解

····我们再次通过一帧图片来说明处理过程。
车道线检测-从单车道到多车道的车道线检测(二)_第5张图片
····在畸变矫正后,为了减少处理的数据量和提高算法鲁棒性,二值化处理是少不了的。
····如上图所示为图像的RGB,HLS通道成像及在各自方向上做二值化特征提取的结果图,二值化的目的是希望尽可能保留道路特征,而其他干扰特征尽可能少,那么对比以上各托我们发现[X sobel on hls],[Y sobel on hls],[X sobel]都是不错的结果,于是这里考虑取[X sobel on hls]和[X sobel]交集,再与[R_channel]作为背景进行取交,得到一个更稳定的结果,从而完成二值化,这里使用的是cv2.Sobel,详细可见上篇文章中的转载[4],本文使用sobel算子(ps.canny算子中也同样通到了sobel算子)。结果如图所示。

def rgb_select(img, thresh=(0, 255)):
	#	get the binary image of r-channel
    R = img[:,:,0]
    binary = np.zeros_like(R)
    binary[(R > thresh[0]) & (R <= thresh[1])] = 1
    return binary
    
def abs_sobel_thresh(img, orient='x', thresh=(0, 255)):
	#	calculate the binary image by sobel operator with orient and thresh from grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    if orient == 'x':
        sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
    else:
        sobel = cv2.Sobel(gray, cv2.CV_64F, 0, 1)
    abs_sobel = np.absolute(sobel)
    scaled_sobel = np.uint8(255 * abs_sobel / np.max(abs_sobel))
    binary_output = np.zeros_like(scaled_sobel)
    binary_output[(scaled_sobel > thresh[0]) & (scaled_sobel <= thresh[1])] = 1
    return binary_output

def abs_sobel_thresh_hls(img, orient='x', thresh=(0, 255)):
	#	calculate the binary image by sobel operator with orient and thresh from s-channel
    hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)[:,:,2]
    if orient == 'x':
        sobel = cv2.Sobel(hls, cv2.CV_64F, 1, 0)
    else:
        sobel = cv2.Sobel(hls, cv2.CV_64F, 0, 1)
    abs_sobel = np.absolute(sobel)
    scaled_sobel = np.uint8(255 * abs_sobel / np.max(abs_sobel))
    binary_output = np.zeros_like(scaled_sobel)
    binary_output[(scaled_sobel > thresh[0]) & (scaled_sobel <= thresh[1])] = 1
    return binary_output
    
def make_binary(img):
    # Threshold color channel
    r_binary = rgb_select(img, (220, 255))
    
    # Threshold based on sobel edge detection  
    sobel = abs_sobel_thresh(img, 'x', (40, 255))
    
    # Complex threshold
    c_binary = abs_sobel_thresh_hls(img, 'x', (50, 255))
    
    # Stack each channel
    color_binary = np.dstack((r_binary, sobel, c_binary)) * 255
    
    # Combine the two binary thresholds
    combined_binary = np.zeros_like(sobel)
    combined_binary[(c_binary == 1) | (sobel == 1) | (r_binary == 1)] = 1
    
    return (combined_binary, color_binary)

undist = cal_undistort(img)
(comb_bin, col_bin) = make_binary(undist)

车道线检测-从单车道到多车道的车道线检测(二)_第6张图片
在得到二值化图像后,此时图像特征明显,可以考虑进行透视变换了。
车道线检测-从单车道到多车道的车道线检测(二)_第7张图片
A3x3即我们需要求的变换矩阵M,这里我们需要把图像从透视视角转化为鸟瞰视角,首先需要计算出透视变换矩阵,那么可以直观的认为**(x,y,w)对应于(u,v,1)**,即w为单位1,那么我们只需要知道若干个对应点,即可求出A3x3,见下方代码,src为实际图像中的车道线选框点dst为对标矩形框坐标点,结果如图所示,关于透视变换的知识可见转载3,里面详细讲解的矩阵变换细节。

def perspective_transform(img, M):  
	# calculate the wrap image by perspective matrix
    warped = cv2.warpPerspective(img,M,(img_size),flags=cv2.INTER_LINEAR)
    return warped

src = np.float32([[185, img_size[1]],[580, 460], [705, 460], [1200, img_size[1]]])
dst = np.float32([[280, img_size[1]], [280, 0], [1000, 0], [1000, img_size[1]]])

# calculate the perspective matrix
M = cv2.getPerspectiveTransform(src, dst)
M_inv = cv2.getPerspectiveTransform(dst, src)
warped = perspective_transform(comb_bin,M)

# Plot the result
colored_comb_bin = np.dstack((comb_bin, comb_bin, comb_bin)) * 255
cv2.polylines(colored_comb_bin, [np.array(src,dtype=np.int32).reshape((-1, 1, 2))], True, (255,255,0), thickness = 2)

colored_warped = np.dstack((warped, warped, warped)) * 255
cv2.polylines(colored_warped, [np.array(dst, dtype=np.int32).reshape((-1, 1, 2))], True, (255,255,0), thickness=2)

车道线检测-从单车道到多车道的车道线检测(二)_第8张图片
在此基础之上,通过直方图统计和滑动窗口法,采集车道线节点,再对节点进行拟合,得到平滑车道线。在图[warped]中,我们可以清晰看到,车道线特征为白色(色值1),其他为黑色(色值0),我们知道图像也是数据矩阵,大小为(720x1280x1),如果把关于横轴(0~1280)统计,那么我们可以得到一个(1x1280)数组向量,如下图所示,那么就可以确定车道线起点大致位置。
车道线检测-从单车道到多车道的车道线检测(二)_第9张图片
再基于滑动窗口法,每个窗口聚合所有(色值1)有效点,通过取平均值,得到特征点作为该窗口的车道线节点,再基于节点坐标向上滑移窗口,…,得到两条车道线的若干节点,基于节点拟合车道线如图所示。到这里,完成车道线检测。
车道线检测-从单车道到多车道的车道线检测(二)_第10张图片

def find_lane_pixels(binary_warped):
    # Take a histogram of the bottom half of the image
    histogram = np.sum(binary_warped[binary_warped.shape[0]//2:,:], axis=0)
    
    # Create an output image to draw on and visualize the result
    out_img = np.dstack((binary_warped, binary_warped, binary_warped))

    # Find the peak of the left and right halves of the histogram
    # These will be the starting point for the left and right lines
    midpoint = np.int(histogram.shape[0]//2)
    leftx_base = np.argmax(histogram[:midpoint])
    rightx_base = np.argmax(histogram[midpoint:]) + midpoint

    # HYPERPARAMETERS
    nwindows = 10 # Choose the number of sliding windows
    margin = 80 # Set the width of the windows +/- margin
    minpix = 40 # Set minimum number of pixels found to recenter window

    # Set height of windows - based on nwindows above and image shape
    window_height = np.int(binary_warped.shape[0]//nwindows)
    # Identify the x and y positions of all nonzero pixels in the image
    nonzero = binary_warped.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
 
    # Current positions to be updated later for each window in nwindows
    leftx_current = leftx_base
    rightx_current = rightx_base

    # Create empty lists to receive left and right lane pixel indices
    left_lane_inds = []
    right_lane_inds = []

    # Step through the windows one by one
    for window in range(nwindows):
        # Identify window boundaries in x and y (and right and left)
        win_y_low = binary_warped.shape[0] - ( window + 1) * window_height
        win_y_high = binary_warped.shape[0] - window * window_height
        
        ##Find the four below boundaries of the window
        win_xleft_low = leftx_current  - margin
        win_xleft_high = leftx_current + margin
        win_xright_low = rightx_current - margin
        win_xright_high = rightx_current + margin
        
        # Draw the windows on the visualization image
        cv2.rectangle(out_img,(win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2) 
        cv2.rectangle(out_img,(win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2) 
        
        # Identify the nonzero pixels in x and y within the window
        good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & 
                          (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0]
        
        good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & 
                           (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0]

        # Append these indices to the lists
        left_lane_inds.append(good_left_inds)
        right_lane_inds.append(good_right_inds)
        
        # If found > minpix pixels, recenter next window
        # (`right` or `leftx_current`) on their mean position
        if len(good_left_inds) > minpix:
            leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
            
        if len(good_right_inds) > minpix:        
            rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

    # Concatenate the arrays of indices (previously was a list of lists of pixels)
    try:
        left_lane_inds = np.concatenate(left_lane_inds)
        right_lane_inds = np.concatenate(right_lane_inds)
    except ValueError:
        # Avoids an error if the above is not implemented fully
        pass

    # Extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds] 
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]
    
    return leftx, lefty, rightx, righty, out_img, left_lane_inds, right_lane_inds
def fit_polynomial(binary_warped, vis, chose=1):
    # Find our lane pixels first
    leftx, lefty, rightx, righty, out_img, left_lane_inds, right_lane_inds = find_lane_pixels(binary_warped)

    left_fit, right_fit = (None, None)
    # Fit a second order polynomial to each
    if len(leftx) != 0:
        left_fit = np.polyfit(lefty, leftx, 2)
    if len(rightx) != 0:
        right_fit = np.polyfit(righty, rightx, 2)

    # Generate x and y values for plotting
    ploty = np.linspace(0, binary_warped.shape[0]-1, binary_warped.shape[0] )
    try:
        left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2]
        right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2]
    except TypeError:
        # Avoids an error if `left` and `right_fit` are still none or incorrect
        left_fitx = 1*ploty**2 + 1*ploty
        right_fitx = 1*ploty**2 + 1*ploty

    ## Visualization ##
    if vis:
        # Colors in the left and right lane regions
        out_img[lefty, leftx] = [255, 0, 0]
        out_img[righty, rightx] = [0, 0, 255]

        # Plots the left and right polynomials on the lane lines
        plt.figure(figsize=(15, 15))
        plt.plot(left_fitx, ploty, color='yellow')
        plt.plot(right_fitx, ploty, color='yellow')
        plt.imshow(out_img)

    if chose == 1:
        return (ploty, left_fit, right_fit, left_fitx, right_fitx)
    else:
        return left_fit, right_fit, left_lane_inds, right_lane_inds

(ploty, left_fit, right_fit, left_fitx, right_fitx) = fit_polynomial(warped, True)

总结和拓展

····该方法的泛化能力更强,可以针对直道、弯道检测,若不作为实时车道线检测,那么用于作为辅助车道偏移,巡航都是不错的选择。
····从优化角度考虑,效果还不够稳定,偶尔由于光影影响,容易出现错帧,可考虑加上平滑器,效果可以提高一些;此外图像上可考虑加上黑白阶调节,减少光影影响,此外可考虑左右两车道线相互联系,再与平滑器比对,提高车道线检测稳定性,但关键缺陷还是在帧率上。
····通过测试,在时间占比上:二值化41%,去畸变22.7%,绘制19.7%,拟合车道线13%,透视变换5%…。可以考虑去掉r-channel,只考虑[X sobel on hls]和[X sobel],或者换种二值化方法;此外绘制也可以简化,从而提高帧率。
····快去试试吧~~

如有侵权,请私戳~~感谢。


  1. https://blog.csdn.net/u010128736/article/details/52860364 ↩︎

  2. https://blog.csdn.net/lxy_2011/article/details/80675803 ↩︎

  3. https://blog.csdn.net/xiaowei_cqu/article/details/26471527 ↩︎

你可能感兴趣的:(车道线检测-从单车道到多车道的车道线检测(二))