Different conversion formulas of RGB Color Space

Source: Internet
Author: User

During image processing, we generally use RGB space, but in some special cases, we also use other color spaces. This article mainly introduces some common color space concepts and conversion formulas.

The essence of color is a light wave. It exists because there are three entities: Light, observed object, and observer. The human eye uses color as a light wave formed by the observed object absorbing or reflecting different wavelengths. For example, when we see an object in the sun red on a sunny day, it is because the object absorbs light of other wavelengths, the reason for reflecting the red wavelength of light to our eyes. Of course, what our human eyes can feel is the light wave signal of the wavelength within the visible range. When optical signals of different wavelengths enter a certain point in our eyes, our Visual organs mix them and accept them as a color. We also need to mix colors when processing images, but we need to follow certain rules, that is, we are processing colors in different color modes.

1. RGB color mode

Although the visible light wavelength has a certain range, we do not need to represent the color of each wavelength separately when processing the color. Because all colors in nature can be combined with different bandwidths of red, green, and blue (RGB). This is what people often say about the three-color principle. Therefore, these three colors are often called three primary colors or three primary colors. Sometimes we also call these three colors additive colors, because we get brighter colors when we add different wavelengths of light. When three colors are overlapped, the following colors are generated: cyan, magenta, and yellow ). This also introduces the concept of complementary colors. The base and secondary colors are complementary colors, that is, the most different colors between each other. For example, cyan is composed of blue and green, while red is a missing color. Therefore, Cyan and red constitute complementary colors. In digital videos, each of the three RGB colors is 8-bit encoded to form about 0.167 million colors. This is what we often call true color. By the way, both TV and computer monitors are created in RGB color mode.

2. Lab color mode

The LAB color is converted from RGB, which is a bridge from RGB to HSB and CMYK. The color mode consists of a luminance and two color (a, B) axes. The ring line on the plane formed by the color axis represents the color change. The radial direction represents the color saturation change, and the saturation increases gradually from the inner to the outer; the circumferential direction represents the variation of the color tone, and each circumference forms a color ring. Different distributions indicate different brightness and correspond to different ring color variations. It is a color mode with "device-independent", that is, the colors of the lab remain unchanged regardless of the monitor or printer.

RGB => lab

| X | 0.433910 0.376220 0.189860 | r/255 | Y | = | 0.212649 0.715169 0.072182 | * | G/255 | z | 0.017756 0.109478 0.872915 | B/255 | L = 116 * Y1/3 for Y> 0.008856l = 903.3 * Y for Y <= 0.008856a = 500 * (f (x) -f (y) B = 200 * (f (y)-f (z) where f (t) = t1/3 for T> 0.008856 F (t) = 7.787 * t + 16/116 for T <= 0.008856

3. HSB color mode

From a psychological perspective, color has three elements: color (Hue), saturation (saturation), and brightness (brightness ). The HSB color pattern is a color pattern based on people's psychological feelings about colors. It is converted from RGB three-color to lab mode, and then based on the lab mode, people's psychological feelings about the color are converted. Therefore, this color pattern is more intuitive to people. It can be represented by the two cone stereo models connected to the bottom and the bottom. The axial direction indicates the brightness, from the top to the bottom, from the white to the black; the radial direction indicates the color saturation, and gradually increases from the inside to the outside; and the circumference direction, the color changes to form a color ring.

RGB => HSB

V = max (R, G, B) S = (V-min (R, G, B) * 255/V if V! = 0, 0 otherwise (G-B) * 60/s, if v = RH = 180 + (B-r) * 60/s, if v = g 240 + (r-g) * 60/s, if v = B if H <0, H = H + 360

Use the formula from 0 ° to 360 ° above to calculate the hue values, so that they can be used for 8 bits after being divided by 2.

4. YUV color mode

This is a commonly used color pattern in television systems, that is, the so-called component signal in television. This mode consists of a Brightness Signal y and two chromatic aberration signals u and v. It utilizes the human eye's sensitivity to brightness signals and is not sensitive to color signals, the RGB color is converted to a Brightness Signal y by using the formula Y = 039r + 050G + 011b and two components of the color difference signal U (R-Y), V (B-Y ), that is, the color difference signal is compressed in the frequency band. Undoubtedly, this is at the cost of sacrificing the quality of the signal.

RGB<=>YUV
Y = 0.299R + 0.587G + 0.114B
U = -0.147R - 0.289G + 0.436B
V = 0.615R - 0.515G - 0.100B

R = Y + 1.14V
G = Y - 0.39U - 0.58V
B = Y + 2.03U

5. CMYK color mode

This is a color mode used for color printing. It consists of four colors: cyan, magenta, yellow, and black. The reason why black is represented by K is to avoid confusion with blue (represented by B) in RGB. The basis for creating this mode is different from that for creating RGB mode. Instead of adding light, it subtract light. Because unlike a monitor or TV, printing paper cannot create a light source and it does not emit light, only light can be absorbed and reflected. Therefore, the combination of the above four colors can produce the vast majority of colors in the visible spectrum.

RGB <= CMYK

R = (255-C) * (255-k)/255)
G = (255-m) * (255-k)/255)
B = (255-y) * (255-k)/255)

 
 

6. Some program code

Void hsi2rgb (byte & Br, byte & BG, byte & BB, byte BH, byte BS, byte Bi)
{
Int nhvalue = static_cast <int> (BH );
Int nsvalue = static_cast <int> (BS );
Int nlvalue = static_cast <int> (BI );

Float fhangle = (float) nhvalue)/255*360;
 
Float H = fhangle/180 * PI;
Float S = (float) nsvalue)/100;
Float I = (float) nlvalue)/100;

Float r =-1;
Float G =-1;
Float B =-1;

If (fhangle> = 0 & fhangle <120)
{
B = I * (1.0-S );
R = I * (1.0 + (S * Cos (h)/cos (60.0/180 * pi-h )));
G = 3.0 * I-(B + r );
}
Else if (fhangle> = 120 & fhangle <240)
{
R = I * (1.0-S );
G = I * (1.0 + S * Cos (H-120.0/180 * PI)/cos (180.0/180 * pi-h ));
B = 3.0 * I-(R + G );
}
Else if (fhangle> = 240 & fhangle <360)
{
G = I * (1.0-S );
B = I * (1.0 + S * Cos (H-240.0/180 * PI)/cos (300.0/180 * pi-h ));
R = 3.0 * I-(G + B );
}
Int r_value_in_rgb = r * 255;
Int g_value_in_rgb = GB * 255;
Int B _value_in_rgb = B * 255;
BR = static_cast <byte> (r_value_in_rgb );
BG = static_cast <byte> (g_value_in_rgb );
BB = static_cast <byte> (B _value_in_rgb );
}

Void rgb2hsi (byte R, byte g, byte B, byte & H, byte & S, byte & I)
{
Short m_fr = static_cast <short> (R );
Short m_fg = static_cast <short> (g );
Short m_fb = static_cast <short> (B );

Float m_fir = static_cast <float> (m_fr)/255;
Float m_fsg = static_cast <float> (m_fg)/255;
Float m_fhb = static_cast <float> (m_fb)/255;

If (m_fr = m_fg & m_fg = m_fb)
{
Int ihvalue = 0;
Int isvalue = 0;
Int ilvalue = (float) m_fr)/255*100;
H = static_cast <byte> (ihvalue );
S = static_cast <byte> (isvalue );
I = static_cast <byte> (ilvalue );
Return;
}
Float max_value_of_rgb = getmax (m_fir, m_fsg, m_fhb );
Float min_value_of_rgb = getmin (m_fir, m_fsg, m_fhb );
Float fsumrgb = m_fir + m_fsg + m_fhb;
If (fsumrgb <= 0.0)
Fsumrgb = 0.001;
Float I = (m_fir + m_fsg + m_fhb)/3;
Float S = 1.0-3.0 * min_value_of_rgb/fsumrgb;
Float H = ACOs (m_fir-m_fsg) + (m_fir-m_fhb)/2/SQRT (m_fir-m_fsg) * (m_fir-m_fsg) + (m_fir-m_fhb) * (m_fsg-m_fhb) + 0.0001 ));
Float fhangle = H/pI * 180;
If (m_fsg <m_fhb)
Fhangle = 360-fhangle;
If (fhangle> 360.0)
Fhangles = 360.0;
Int nhvalue = fhangle/360*255;
Int nsvalue = S * 100;
Int nlvalue = I * 100;
H = nhvalue;
S = nsvalue;
I = nlvalue;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.