Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

python - HSL range for yellow lane lines

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

Here's the original image:

http://prntscr.com/ng2cx6

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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).

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).

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:

White mask

The yellow-ish mask looks like this:

Yellow mask

The masked image from your code looks like this:

Masked image

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...