Chapter 3 - OpenCV Basics - 5 - Color Processing

front content 

The RGB color space is a common color space, and there are other color spaces such as GRAY color space, HSV color space, etc., which can understand colors from different angles.

Analogous to the number 10, it can be expressed as binary, octal or hexadecimal, expressed in different rules, there is no error, but the calculation of each base must be performed according to the rules of each base,

The same is true for color spaces, each color space can be converted to each other, and analogy can also be converted between different bases.

Color Space Basics

Gray color space

When the image is converted from RGB color space to GRAY color space (grayscale image), the standard conversion formula is: GRAY=0.299 * R + 0.587 * G + 0.114 * B

Sometimes it's also easier to calculate the transformation: GRAY = ( R + G + B ) / 3

When GRAY color space is converted to BGR color space, all channels will be the same: B = G = R = GRAY

HSV color space

RGB is mixed into various colors through different proportions of the three primary colors, and in practice, it is more customary to perceive colors in an intuitive way, which is the HSV color space.

The HSV color space contains hue H, saturation S, lightness V

  • Hue H (Hue)

Hue is the color of light. We know that sunlight is mixed light, which contains various lights such as red, orange, yellow, green, blue, blue, and purple. These different colors of light appear as different colors due to different wavelengths of light. These different lights are different hues.

In the HSV color space, the value range of the hue H is [0,360], and the value range of each pixel is [0.255]. In order to map the hue from [0,360] to [0,255], OpenCV divides the hue by 2, The obtained value is [0,180], to adapt to the storage and representation range of 8-bit binary numbers.

The values ​​corresponding to some typical colors are as follows:

color

tone

Values ​​inside OpenCV

red

0

0

yellow

60

30

green

120

60

blue

180

90

blue

240

120

magenta

300

150

A brief description of the use of hue: In the HSV image, the pixel corresponding to the value of the H channel 120 is blue.

  • Saturation S(Saturation)

Saturation refers to the vividness of the color, expressed as the relative purity of the color. Saturation depends on the proportion of gray in the color, the more gray, the lower the color saturation. When the color does not contain gray, the color appears as a pure color at this time.

When the saturation of the color is very low (too much gray), the calculated hue (light color) is not accurate, and there is no color information at this time, only gray information.

The value range of saturation is [0,1]. When the saturation is 0, there is only grayscale. When the saturation is 1, it is a pure color.

Saturation also needs to map [0,1] to [0,255] for display in an 8-bit bitmap.

  • Lightness V(Value)

Brightness refers to the brightness of the color perceived by the human eye, and reflects the brightness and darkness of the light perceived by the human eye.

For achromatic colors (black, white, gray), the lightness of white is the highest, and the lightness of black is the lowest, and there are grays of different lightness between white and black.

For color images, the higher the brightness, the brighter the image, and vice versa.

The value range of brightness is [0,1], and it is also mapped to [0,255] in OpenCV.

for example:

Hue=0 Saturation=1 Brightness=1, the color seen by human eyes is red, and the color is brighter.

Hue=120 Saturation=0.3 Brightness=0.4, the color seen by human eyes is light green and darker.

color space conversion

OpenCV provides methods to realize the conversion of different color spaces

dst = cv2.cvtColor( src , code [,dstCn] )

dst : The converted output image has the same data type and depth as the original image.

src : original input image

code : Color space conversion code, this parameter tells OpenCV which color space to convert from to which color space, the specific length is cv2.COLOR_XXX2YYY

dstCn : the number of channels of the target image

The rounding caused by the mapping operation is inevitable in the conversion process of each space, so the process of converting color space back and forth is not accurate and reversible.

Example - extract skin color

According to the range of skin color in HSV color space, the part containing skin color can be extracted

  • Hue value [0,33]
  • saturation[10,255]
  • Lightness value [80,255]

The specific implementation is as follows:

import cv2 as cv
import numpy as np

img = cv.imread("x.png")
# BGR转为HSV
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
print(hsv)  # HSV色彩空间的数据 二维数组的[H S V]或者三维数组
min_HSV = np.array([0, 10, 80], dtype="uint8")  # [0 10 80]
max_HSV = np.array([33, 255, 255], dtype="uint8")  # [33 255 255]
# cv2.inRange(src,min,max)低于min或高于max的值置为0 在min和max范围内的值置为255
mask = cv.inRange(hsv, min_HSV, max_HSV)
print(mask)  # 掩模嘛,就是个二维数组
result = cv.bitwise_and(img, img, mask=mask)  # 2个img这个我看的有点懵一开始,自己与自己,等于自己,就是图像在掩模作用范围内做算法
cv.imshow("img", img)
cv.imshow("mask", mask)
cv.imshow("result", result)
cv.waitKey()
cv.destroyAllWindows()

The operation is as follows (I originally wanted to use lena's color map, but the whole lena map has a wide and mixed yellow color, and the effect is not very good):

Guess you like

Origin blog.csdn.net/sunguanyong/article/details/129232791