本文介绍了黄色车道线的HSL范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently working on simple lane detection and I have some trouble finding the range/input values for yellow colored lane lines.

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

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

Here is the image that I've filtered (only white lanes are showing):

http://prntscr.com/ng2cgp

这是原始图片:

http://prntscr.com/ng2cx6

推荐答案

我建议您进一步阅读HSL/HSV颜色空间的工作原理,也许从维基百科文章?此外,要轻松获取一些初始值,您可以使用HSL计算器,例如一个.

I suggest, you have a further reading on how the HSL/HSV color space works, maybe starting at the Wikipedia article? Furthermore, to easily get some initial values to work on, you can use a HSL calculator, e.g. this one.

要检测图像中的白色部分,只要亮度(L)值足够高(我们要使用明亮的颜色)并且饱和度(S)值是足够低(我们希望饱和度较低的颜色).

To detect white-ish parts in the image, the hue (H) value might by arbitrary, as long as the lightness (L) value is high enough (we want bright colors), and the saturation (S) value is low enough (we want low saturated colors).

通常,H值在[0 ... 360]之内,而S和L值在[0.0 ... 1.0]之内. 颜色转换上的OpenCV文档告诉您,这些值分别映射到[0 ... 180]中的H和[0 ... 255]中的S和L(对于8位图像).

In general, H values are within [0 ... 360], whereas S and L values are within [0.0 ... 1.0]. The OpenCV documentation on color conversions tells you, that these values are mapped to H within [0 ... 180], and S and L within [0 ... 255] (for 8-bit images).

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

Now, to detect yellow-ish parts in the image, appropriate H, S, and L values can be taken from the afore-mentioned HSL calculator by "playing around", what might fit to the colors to be found in the image.

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

I prepared the following example code, please have a look:

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)

白色面具看起来像这样:

The white-ish mask looks like this:

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

The yellow-ish mask looks like this:

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

The masked image from your code looks like this:

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

As you can see, fine-tuning the parameters must be done. But I hope, you now get the general idea, and can continue on your own.

这篇关于黄色车道线的HSL范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:31