Color model 色彩模型

2020-03-10 10:59:01 浏览数 (1)

A color model is a system for creating a full range of colours from a small set of primary colors. There are two types of colour models: additive and subtractive. Additive color modelsuse light to display color, while subtractive color models use printing inks.

颜色模型指的是某个三维颜色空间中的一个可见光子集,它包含某个色彩域的所有色彩。一般而言,任何一个色彩域都只是可见光的子集,任何一个颜色模型都无法包含所有的可见光。常见的颜色模型有RGB CIECMY/CMYK、(HSK NTSC、YcbCr、HSV 等。

BGR/RGB <-> HLS/HSV <-> GRAY <-> BINARY

BGR (blue, green, red) and RGB (red, green, blue) w*h*3*[0-255]

HSL (hue, saturation, lightness) and HSV (hue, saturation, value) w*h*3

Gray = w*h*[0-255] Binary = w*h*[0/1]

代码语言:python代码运行次数:0复制
# RGB/BGR <-> HSL/HSV/GRAY
cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.cvtColor(image, cv2.COLOR_BGR2HSL)
# white color mask
lower = np.uint8([123, 116, 116])
upper = np.uint8([186, 172, 160])
white_mask = cv2.inRange(image, lower, upper)
# yellow color mask
lower = np.uint8([134, 121, 100])
upper = np.uint8([206, 155, 87])
yellow_mask = cv2.inRange(image, lower, upper)

# Gray -> Binary
cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # min 127, max 255
cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC)
cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO)
cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV)

# Binary -> Gray -> BGR
np.array(binary*255).astype(np.uint8)
cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)

# HSV/HSL color filter
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# white color mask
lower = np.uint8([0, 0, 0])
upper = np.uint8([0, 0, 255])
white_mask = cv2.inRange(hsv, lower, upper)
# yellow color mask
lower = np.uint8([ 10,   0, 100])
upper = np.uint8([ 40, 255, 255])
yellow_mask = cv2.inRange(hsv, lower, upper)
# combine the mask
mask = cv2.bitwise_or(white_mask, yellow_mask)

# combine the mask
mask = cv2.bitwise_or(white_mask, yellow_mask)
target = cv2.bitwise_and(img,img, mask=mask)

# ch_binary = np.zeros_like(img[:,:,int(ch-1)])
# ch_binary[(img[:,:,int(ch-1)]>=ch_thresh[0])&(img[:,:,int(ch-1)]<=ch_thresh[1])] = 1

0 人点赞