黄色车道线的HSL范围

时间:2019-04-24 04:24:19

标签: python python-3.x opencv colors opencv3.0

我目前正在进行简单车道检测,但在查找黄色车道线的范围/输入值时遇到了一些麻烦。

def color_filter(image):
    #convert to HLS to mask based on HLS
    hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
    lower = np.array([0,190,0])
    upper = np.array([255,255,255])

    yellower = np.array([40,70,60]) #NOT SURE WHAT TO PUT
    yelupper = np.array([50,90,65]) #NOT SURE WHAT TO PUT

    yellowmask = cv2.inRange(hls, yellower, yelupper)    
    whitemask = cv2.inRange(hls, lower, upper)

    mask = cv2.bitwise_or(yellowmask, whitemask)  
    masked = cv2.bitwise_and(image, image, mask = mask)    

    return masked

这是我过滤的图像(仅显示白色通道):

http://prntscr.com/ng2cgp

这是原始图片:

http://prntscr.com/ng2cx6

1 个答案:

答案 0 :(得分:3)

我建议您进一步了解HSL / HSV颜色空间的工作原理,也许从Wikipedia article开始?此外,要轻松获取一些初始值,您可以使用HSL计算器,例如this个。

要检测图像中的白色部分,只要亮度(L)值足够高(我们需要鲜艳的颜色)且饱和度(S)值是足够低(我们希望饱和度较低的颜色)。

通常,H值在[0 ... 360]之内,而S和L值在[0.0 ... 1.0]之内。 color conversions上的OpenCV文档告诉您,这些值已映射到[0 ... 180]中的H以及[0 ... 255]中的S和L(对于8位图像)。

现在,要检测图像中的淡黄色部分,可以通过“游玩”从上述HSL计算器中获取适当的H,S和L值,这些值可能适合在颜色中找到的颜色。图片。

我准备了以下示例代码,请看一下:

import cv2
import numpy as np

# Load input image
input = cv2.imread('images/input.png', cv2.IMREAD_COLOR)

# Convert to HLS color space
hls = cv2.cvtColor(input, cv2.COLOR_BGR2HLS)

# White-ish areas in image
# H value can be arbitrary, thus within [0 ... 360] (OpenCV: [0 ... 180])
# L value must be relatively high (we want high brightness), e.g. within [0.7 ... 1.0] (OpenCV: [0 ... 255])
# S value must be relatively low (we want low saturation), e.g. within [0.0 ... 0.3] (OpenCV: [0 ... 255])
white_lower = np.array([np.round(  0 / 2), np.round(0.75 * 255), np.round(0.00 * 255)])
white_upper = np.array([np.round(360 / 2), np.round(1.00 * 255), np.round(0.30 * 255)])
white_mask = cv2.inRange(hls, white_lower, white_upper)

# Yellow-ish areas in image
# H value must be appropriate (see HSL color space), e.g. within [40 ... 60]
# L value can be arbitrary (we want everything between bright and dark yellow), e.g. within [0.0 ... 1.0]
# S value must be above some threshold (we want at least some saturation), e.g. within [0.35 ... 1.0]
yellow_lower = np.array([np.round( 40 / 2), np.round(0.00 * 255), np.round(0.35 * 255)])
yellow_upper = np.array([np.round( 60 / 2), np.round(1.00 * 255), np.round(1.00 * 255)])
yellow_mask = cv2.inRange(hls, yellow_lower, yellow_upper)

# Calculate combined mask, and masked image
mask = cv2.bitwise_or(yellow_mask, white_mask)
masked = cv2.bitwise_and(input, input, mask = mask)

# Write output images
cv2.imwrite('images/white_mask.png', white_mask)
cv2.imwrite('images/yellow_mask.png', yellow_mask)
cv2.imwrite('images/masked.png', masked)

白色面具看起来像这样:

White mask

淡黄色面具看起来像这样:

Yellow mask

代码中的蒙版图像如下所示:

Masked image

如您所见,必须对参数进行微调。但是我希望,您现在有了大致的想法,并且可以继续自己做下去。