不同的色彩空间
A color space is a specific organization of colors; color spaces provide a way to categorize colors and represent them in digital images.
- RGB:red,green,blue
For example, white has the coordinate (255, 255, 255), which has the maximum value for red, green, and blue.
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.
- HSV:hue(0~179),saturation,value
- HLS: hue, lightness, saturation
- Hue as the value that represents color independent of any change in brightness.
- Lightness and Value represent different ways to measure the relative lightness or darkness of a color.
- saturation is a measurement of colorfulness.
Note:
For example, a dark red will have a similar hue but much lower value for lightness than a light red.
So, as colors get lighter and closer to white, they have a lower saturation value, whereas colors that are the most intense, like a bright primary color (imagine a bright red, blue, or yellow), have a high saturation value.
you can get a better idea of these value from the picture above.
and you can get more from this
function
hls = cv2.cvtColor(im, cv2.COLOR_RGB2HLS)
converts images from one color space to another
note: it would be necessary to divide the RGB values by 255 such that they fall in the range (0, 1)(0,1), but OpenCV will perform this for you in cv2.cvtColor().
There are three different equations, which one is used depends on the the value of Vmax whether that’s R, G, or B.
Note: In OpenCV, for 8-bit images, the range of H is from 0-179. It’s typically from 0-359 for degrees around the cylindrical colorspace, but this number is divided in half so that the range can be represented in an 8-bit image whose color values range from 0-255.
code——分离各色彩通道的图像值
- 灰度选择车道线
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('test6.jpg')
thresh = (180, 255)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
binary = np.zeros_like(gray)
binary[(gray > thresh[0]) & (gray <= thresh[1])] = 1
- RGB三通道的各自的图像
R = image[:,:,0]
G = image[:,:,1]
B = image[:,:,2]
thresh = (200, 255)
binary = np.zeros_like(R)
binary[(R > thresh[0]) & (R <= thresh[1])] = 1
- HLS三通道的各自的图像
hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
H = hls[:,:,0]
L = hls[:,:,1]
S = hls[:,:,2]
thresh = (90, 255)
binary = np.zeros_like(S)
binary[(S > thresh[0]) & (S <= thresh[1])] = 1
thresh = (15, 100)
binary = np.zeros_like(H)
binary[(H > thresh[0]) & (H <= thresh[1])] = 1
example
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(image, cv2.COLOR_RGB2HLS)
# 2) Apply a threshold to the S channel
S = hls[:,:,2]
binary = np.zeros_like(S)
binary[(S > thresh[0]) & (S <= thresh[1])] = 1
# 3) Return a binary image of threshold result
# binary_output = np.copy(img) # placeholder line
return binary
# 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.)