In the image processing system, color space conversion is a common basic function. Although the function is not very complex, there are also some problems. After all, the first image processing work has been encountered. Here, we will first introduce the conversion between the RGB model and the HSI model of the image.
In the HSI model, H: hue represents the color (purity, color)
S: Saturation indicates the saturation.
I: intensity indicates brightness
The formula for converting RGB to HSI is as follows:
The formula from the HSI model to the RGB model is as follows:
With the formula, it is easier to write code.
Convert RGB to HSI code and convert a single pixel.
Bool crgbihsalgo: rgb2ihs (byte R, byte g, byte B, float & I, float & H, float & S) {float rvalue = (float) R; float gvalue = (float) g; float bvalue = (float) B; I = (float) (Rvalue + gvalue + bvalue); if (I = 0) I = 1.0; // prevent the divisor from being 0 float Theta = 0.0; float Numerator = (Rvalue-gvalue) + (Rvalue-bvalue) * 0.5; // molecular float Denominator = SQRT (POW (Rvalue-gvalue, 2) + (Rvalue-bvalue) * (gvalue-bvalue); // denominator
Denominator + = 0.00000001; // Add a small number to prevent the divisor from being 0 float x = numerator/denominator; Theta = ACOs (x); If (B <= g) {H = Theta/(2*3.1415926);} else if (B> G) {H = (2*3.1415926-Theta)/(2*3.1415926 );} S = 1.0f-3 * min (Rvalue, gvalue), bvalue)/I; I = I/3; return true ;}
Code for converting an HSI model to an RGB model is also a single pixel.
// Color inverse transformation function of a single pixel static void historgb (float FH, float Fi, float FS, byte & R, byte & G, byte & B) {int R, G, b; float Pi2 = 3.1415926*2; FH * = Pi2; // restore h to 0-360 ° // note that the trigonometric function parameter is in radians // If (FH <Pi2/3.0) between 0-120 degrees) & (FH> = 0.0) {B = Fi * (1-fs); r = Fi * (1.0 + FS * Cos (FH)/cos (Pi2/6.0-fh )); G = 3 * fi-(R + B);} // H between 120-240 degrees else if (FH <2 * Pi2/3.0) & (FH> = Pi2/3.0) {FH-= Pi2/3.0; r = Fi * (1-fs); G = Fi * (1.0 + FS * Cos (F H)/cos (Pi2/6.0-fh); B = 3 * fi-(R + G );} // H else {FH-= 2 * Pi2/3.0; G = Fi * (1-fs); B = Fi * (1.0 + FS * Cos (FH) between and degrees) /cos (Pi2/6.0-fh); r = 3 * fi-(B + G);} r = r> 255? 255: R; G = G> 255? 255: G; B = B> 255? 255: B ;}
When the same image is first converted from RGB to hsi and then to RGB, the theoretical pixel value remains unchanged. However, there is actually an error in floating point operations, but the error is not big.