Hits: 0

# color space transformation

### 1. Introduction

[​ A general image has three channels] : red (R), green (G), and blue (B) . Each channel is composed of (0-255) different values, which constitute a colorful image, which is called the image color space. In image processing, there are additional color spaces (such as HSV, HIS), which are more separable and maneuverable. So many image algorithms need to convert images from RGB to other spaces.

### Second, RGB and grayscale space conversion

​ RGB (red, green and blue) is a space defined by colors recognized by the human eye and can represent most colors. However, RGB color space is generally not used in scientific research, because its details are difficult to digitally adjust. It expresses hue, brightness, and saturation together, and it is difficult to separate them. Here, the RGB space and the grayscale space are converted to each other.
Common conversion formulas are as follows

Algorithm name conversion formula
Maximum grayscale processing g r a y = m a x ( R , G , B ) gray=max(R,G,B) gray=max(R,G,B)
Floating point grayscale processing g r a y = 0.3 R + 0.59 G + 0.11 B gray=0.3R+0.59G+0.11B gray=0 . 3 R+0.59G+0.11B
Integer grayscale processing g r a y = ( 30 R + 59 G + 11 B ) / 100 gray=(30R+59G+11B)/100 gray=( 30 R _+59G+11B)/100
Shift grayscale processing g r a y = ( 28 R + 151 G + 77 B ) > > 8 gray=(28R+151G+77B)>>8 gray=(28R+151G+7 7 B )>>8
Average grayscale processing g r a y = ( R , G , B ) / 3 gray=(R,G,B)/3 gray=(R,G,B)/3
Weighted Average Grayscale Processing g r a y = 0.299 R + 0.5877 G + 0.144 B gray=0.299R+0.5877G+0.144B gray=0 . 2 9 9 R+0 . 5 8 7 7 G+0.144B

The main purpose of grayscale is to convert the color of three channels into one channel. The most common is weighted average grayscale processing.

After knowing the formula, you can directly use the algorithm to convert the color value of each pixel into a gray value. There are algorithms in opencv that can directly convert RGB images to grayscale images, as follows:

#opencv's own method
img_GRAY = cv2.cvtColor(img_RGB, cv2.COLOR_BGR2GRAY) #Use

numpy to convert grayscale space, use weighted average grayscale
height, width, channel = img_GRAY.shape
grayimg = np.zeros((height, width, 3), np.uint8) #Image
weighted average grayscale processing method
for  i in range(height):
for  j in range(width): #Gray
weighted average method
gray = 0.30 * img[i,j] + 0.59 * img[i,j] + 0.11 * img[i,j]
grayimg[i,j] = np.uint8(gray)

### 3. RGB to HSV

HSV is a way of representing points in the RGB color space in an inverted cone. HSV is Hue, Saturation, Value, also known as HSB (B is Brightness). Hue is the basic attribute of color, which is the name of the color that is usually said, such as red, yellow, etc. Saturation (S) refers to the purity of the color. The higher the color, the purer the color is, and the lower the color is, the more gray it is. Take a value from 0 to 100%. Brightness (V), take 0-max (the value range of HSV in the computer is related to the length of storage). The HSV color space can be described by a cone space model. At the vertex of the cone, V=0, H and S are undefined, representing black. At the center of the top surface of the cone, V=max, S=0, and H is undefined, representing white.

This model is described in terms of colors, shades, and shades.

H is color

S is the shade, when S = 0, only grayscale

V is light and dark, indicating the brightness of the color.

RGB to HSV conversion formula:
V = m a x ( R , G , B ) S = { V − m i n ( R , G , B ) V i f V ≠ 0 0 o t h e r w i s e H = { 60 ( G − B ) / ( V − m i n ( R , G , B ) ) i f V = R 120 + 60 ( B − R ) / ( V − m i n ( R , G , B ) ) i f V = G 240 + 60 ( R − G ) / ( V − m i n ( R , G , B ) ) i f V = B V = max(R,G,B)\ S =

$$\begin{cases} \frac{V-min(R,G,B)} {V} &if&V\not=0\ 0& otherwise \end{cases}$$

\ H =

$$\begin{cases} 60(G-B)/(V-min(R,G,B))&if&V=R\ 120+60(B-R)/(V-min(R,G,B))&if &V=G\ 240+60(R-G)/(V-min(R,G,B))&if &V=B \end{cases}$$

V=max(R,G,B)S={VV−min(R,G,B)​0​ifotherwise​V​=0H=⎩⎪⎨⎪⎧​60(G−B)/(V−m i n ( R ,G,B))120+60(B−R)/(V−m i n ( R ,G,B))240+60(R−G ) / ( V−m i n ( R ,G,B))​ififif​V=RV=GV=B​Opencv provides a method to convert RGB to HSV. You only need to pass in the original image, and the output is an image in HSV space.

img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV)

During the experiment, it was found that the image format directly read by opencv is arranged in BGR. When it is converted to RGB, the color changes, but the images in HSV space generated by two different formats have the same result. code show as below:

img_BGR = cv2.imread("img.jpg")
cv2.imshow("img_BGR",img_BGR)

# First convert the image to RGB format
img_RGB = cv2.cvtColor(img_BGR,cv2.COLOR_BGR2RGB)
cv2.imshow("img_RGB",img_RGB)

# use rgb to convert hsv
img_RGB2HSV = cv2.cvtColor(img_BGR,cv2.COLOR_BGR2HSV)
cv2.imshow("img_RGB2HSV",img_RGB2HSV)

# use bgr to convert hsv
img_BGR2HSV = cv2.cvtColor(img_BGR,cv2.COLOR_BGR2HSV)
cv2.imshow("img_BGR2HSV",img_BGR2HSV)

### 4. HSV to RGB

As above, you can know how to convert from RGB space to HSV space, and continue to record the conversion from HSV space to RGB space below.

When given a value (h, s, v) in an HSV, the conversion formula is as follows:

The displayed RGB values ​​range from 0.0 to 1.0

RGB HSV
（1,0,0） （0°，1,1）
（0.5,1,0.5） （120°，0.5,1）
（0,0,0.5） （240°，1,0.5）

img_RGB = cv2.cvtColor(img_HSV,cv2.COLOR_HSV2RGB)

The hsv image is the same as the above result, and the obtained rgb image is as follows:

The obtained result is arranged in RGB. If you want to restore the original style, you need to perform RGB2BGR operation.