车道线检测预处理(1)------ 融合白线黄线+高斯

总共分三个部分
第一步采用hls提取颜色空间,选择S通道
后面的阈值可以根据自己的需要进行调整,一般取值min=80,max=255

def hls_select(img, channel='S', thresh=(90, 255)):
    # 1) Convert to HLS color space
    # 2) Apply a threshold to the S channel
    # 3) Return a binary image of threshold result
    hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
    if channel == 'S':
        X = hls[:, :, 2]
    elif channel == 'H':
        X = hls[:, :, 0]
    elif channel == 'L':
        X = hls[:, :, 1]
    else:
        print('illegal channel !!!')
        return
    binary_output = np.zeros_like(X)
    binary_output[(X > thresh[0]) & (X <= thresh[1])] = 1
    return binary_output

然后是提取白色车道线部分,阈值化提取
参数可自行调整,一般min=200+ ,max=255

def r_select(img, thresh=(222, 255)):
    R = img[:,:,0]
    binary = np.zeros_like(R)
    binary[(R > thresh[0]) & (R <= thresh[1])] = 1
    return binary

接下来把黄色的车道线整合到白色车道线的图像上

s_binary = hls_select(img, channel='S', thresh=(80, 255))

R_select=r_select(img)

mask_YW_image = cv2.bitwise_or(R_select,s_binary)

最后对结合后的图像进行高斯滤波,去除噪声点

kernel_3x3 = np.array([

    [1, 2, 1],

    [2, 4, 2],

    [1, 2, 1]

])
kernel_3x3 = kernel_3x3 / kernel_3x3.sum()  # 加权平均
k3 = ndimage.convolve(mask_YW_image,kernel_3x3)

依次处理即可得到较好的二值图用到的库函数如下

import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage

你可能感兴趣的:(python,车道线,预处理)