Lane detection 中的色彩检验
起因:我们通常分析图片都用RGB,如果直接转成gray,或者简单的利用threshhold进行颜色提取,会丢掉许多的颜色信息
Note: If you read in an image using matplotlib.image.imread() you will get an RGB image, but if you read it in using OpenCV cv2.imread() this will give you a BGR image.
解决方案:采取HLS或者HSV进行处理。HLS分别是
Hue:色相,深红浅红暗红,他们的Hue均相同
Lightness:不用说,亮度,都懂
Saturation: 饱和度,打个比方接近于白色的红饱和度低,浓郁的红(intensive)饱和度高
opencv中也直接给出了转换公式
hls = cv2.cvtColor(im, cv2.COLOR_RGB2HLS
举个例子:ABC都是紫,所以三者Hue相同。但是 L C > L B > L A L_C>L_B>L_A LC>LB>LA
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxSejpWTykleNNTT6hVMSdVYopkMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL0YDO1EjNwITMwIjNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
具体使用我们可以见得S在检测中起了相当robust的作用
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
# Read in an image, you can also try test1.jpg or test4.jpg
image = mpimg.imread('test6.jpg')
# TODO: Define a function that thresholds the S-channel of HLS
# Use exclusive lower bound (>) and inclusive upper (<=)
def hls_select(img, thresh=(0, 255)):
# 1) Convert to HLS color space
hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
S = hls[:,:,2]
# 2) Apply a threshold to the S channel
# 3) Return a binary image of threshold result
binary_output = np.copy(S) # placeholder line
binary_output[(S > thresh[0]) & (S<=thresh[1]) ] = 1
return binary_output
# Optional TODO - tune the threshold to try to match the above image!
hls_binary = hls_select(image, thresh=(90, 255))
# Plot the result
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
f.tight_layout()
ax1.imshow(image)
ax1.set_title('Original Image', fontsize=50)
ax2.imshow(hls_binary, cmap='gray')
ax2.set_title('Thresholded S', fontsize=50)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)