For a detailed discussion HSL and HSV color space "reprint"

Reprinted from the original garden blog  Brave-Sailor   https://www.cnblogs.com/Free-Thinker/p/4952741.html

 

 

Currently there are many types of color space (color space) in the field of computer vision. HSV and HSL color model are the two most common cylindrical coordinate representation, it remap the RGB model, it is possible to visually more visually intuitive than the RGB model.

HSV color space
HSV (hue, saturation, value) color space model corresponding to a subset of the conical cylindrical coordinate system, a top face of the cone corresponding to V = 1. It contains the RGB model is R = 1, G = 1, B = 1 three sides, represented by the color brighter. H color the rotation angle around the V axis given. Red corresponds to an angle 0 °, green corresponding to the angle of 120 °, an angle corresponding to the blue 240 °. In the HSV color model, each color and its complementary color by 180 °. Saturation S values from 0 to 1, the radius of the top surface of the cone 1. Color domain HSV color model is represented by a subset of the CIE chromaticity diagram, the model color saturation of one hundred percent, generally less than one hundred percent purity. At the apex of the cone (i.e., the origin) at, V = 0, H and S are not defined, black. At the center of the top surface of the cone S = 0, V = 1, H undefined, representing white. From the point of origin to the representative brightness darkening gray, i.e. having different gradations of gray. For these points, S = 0, the value of H is not defined. It can be said, V axis corresponds to the HSV model is the RGB color space of the main diagonal. Colors on the circumferential surface of the apex of the cone, V = 1, S = 1 , the color is a solid color. HSV color model corresponds to the method of the painter. Painter obtained by changing the color depth and color concentrate from certain method of solid color different color tones, in the addition of a solid color to change white color concentrate, is added to alter the black color depth, while adding different proportions of white and black can be obtained a variety of shades. 

 

HSI color space
HSI color space is from the human visual system, a hue (Hue), saturation (Saturation, or by Chroma) and luminance (or the Brightness Intensity) to describe color. HSI color space can be described with a conical model space. This model is described by a cone rather complicated HIS color space, but they can hue, brightness, and color change situation is shown clearly saturation. Usually known as hue and saturation of color, for indicating the type and the degree of color depth. Since the human visual sensitivity to brightness is much stronger than the sensitivity of the color shade, color processing and in order to facilitate identification, the human visual system is often used HSI color space, it is more consistent with human visual characteristics than the RGB color space. In the image processing and computer vision algorithms can be conveniently used a lot in the HSI color space, they can be handled separately and are mutually independent. Therefore, in the HSI color space can greatly simplify the workload analysis and image processing. HSI color space and RGB color space just different representations of the same quantity, thus there is conversion relationship between them.

HSI color model is from the human visual system, with H represents hue (Hue), S representative of saturation (Saturation,) and a representative luminance I (Intensity) to describe color. White light amount of color saturation and just the reverse, it can be said that a brightly colored or not the index. So if we use the HIS models to process images on the display, it will be able to get a more realistic effect.
Hue (Hue): refers to the wavelength reflected by an object or conduction. More common is the color such as red, orange or green to identify, take values from 0 to 360 degrees to measure.
Saturation (Saturation): also known as chroma, it refers to the intensity or color purity. Saturation and hue of gray representation, and 0% (gray) to 100% (fully saturated) to measure.
Brightness (Intensity): refers to the relative brightness of the color, generally a percentage from 0% (black) to 100% (white) to measure.

 

 

 


Representative HSL hue (Hue), saturation (Saturation,) and brightness (Lightness), also commonly referred to as HLS. HSV representative of hue, saturation, and value (Value). Note that the two H HSL and HSV meaning is the same, but are different definitions of saturation, although the call saturation, can be seen from the back of the difference between the two definitions.
Both HSV and HSL color regarded described points within the cylinder, the central axis of the cylinder from the value of the bottom to the top of the black, white and gray in the middle thereof, about the axis angle corresponds to the "hue", this distance axis corresponds to "saturation", the distance along the axis corresponds to the "brightness", "hue" or "lightness." HSV to mankind more familiar way encapsulates information about color: "What color is how light and dark shades of what???." HSL color space is similar to HSV, in some ways even better than it. The model HSL double cone shape.
Both expressed in a similar purpose, but there are differences in the method. Mathematically are both cylindrical, but the HSV (hue, saturation, lightness) may conceptually be considered as an inverted conical colors (black dot next vertex, a white circle on the bottom), the HSL shows conceptually one pair of cone and sphere (half white is at the apex of gray, black lower vertices, the center of the largest cross-section). HSV model in 1978, founded by Herve? Ray? Smith. The following figure shows a cylindrical model of HSV and HSL.

FIG conic model:

On the cone, the angle representative of hue H, saturation S is represented by a vertical line from the center point, or the brightness value V represented by the central vertical lines. Red angle of 0 degrees, followed by yellow, green, cyan, blue, orange. Continuous two color difference angle of 60 degrees.

 

 The following figure shows an example of a mapping of RGB to the HS plane.


The following table shows a sample mapping of RGB to HSV and HSL.

So how from RGB to HSL and HSV conversion it? Using the following steps can be achieved and the conversion from RGB to HSV, HSL.
Point R is known, G, B, the maximum and minimum values

 

 

Calculated H:

 

L is calculated, and V:

 

Calculated S:

上面的公式实现了从RGB到HSL和HSV的转换。那么当采用Opencv的彩色空间转化函数cvCvtColor(orgFrame,destFrame, CV_BGR2HSV),如果图像的数据类型为8位字符型时,则H、S、V都量化到整数{0~255},可以看出H的精度不是很高。


当在HSL和HSV空间进行处理后,需要转换到RGB,可以采用下面的公式计算RGB。
先讨论HSV到RGB的转换,这里H ∈ [0°, 360°], S ∈ [0, 1],  V ∈ [0, 1]。转换公式如下:

计算C

 

 

最后得到:

 

从HSL到RGB的转换。这里H ∈ [0°, 360°], S ∈ [0, 1],  V ∈ [0, 1]。转换公式如下: 

 

 然后:

 

 

最后得到:

展示的 RGB 值的范围是 0.0 到 1.0。


下面用C实现RGB到HSV的转换,采用宏定义的方式。

 

  1     // /////////////////////////////////////////////////////////////////////
  2     // 
  3     // cvlab.net
  4     // C/C++ Macro RGB to HSV
  5     #define PIX_RGB_TO_HSV_COMMON(R,G,B,H,S,V,NORM)                        \
  6     if((B > G) && (B > R))                                                 \
  7     {                                                                      \
  8       V = B;                                                               \
  9       if(V != 0)                                                           \
 10       {                                                                    \
 11         double min;                                                        \
 12         if(R > G) min = G;                                                 \
 13         else      min = R;                                                 \
 14         const double delta = V - min;                                      \
 15         if(delta != 0)                                                     \
 16           { S = (delta/V); H = 4 + (R - G) / delta; }                      \
 17         else                                                               \
 18           { S = 0;         H = 4 + (R - G); }                              \
 19         H *=   60; if(H < 0) H += 360;                                     \
 20         if(!NORM) V =  (V/255);                                            \
 21         else      S *= (100);                                              \
 22       }                                                                    \
 23       else                                                                 \
 24         { S = 0; H = 0;}                                                   \
 25     }                                                                      \
 26     else if(G > R)                                                         \
 27     {                                                                      \
 28       V = G;                                                               \
 29       if(V != 0)                                                           \
 30       {                                                                    \
 31         double min;                                                        \
 32         if(R > B) min = B;                                                 \
 33         else      min = R;                                                 \
 34         const double delta = V - min;                                      \
 35         if(delta != 0)                                                     \
 36           { S = (delta/V); H = 2 + (B - R) / delta; }                      \
 37         else                                                               \
 38           { S = 0;         H = 2 + (B - R); }                              \
 39         H *=   60; if(H < 0) H += 360;                                     \
 40         if(!NORM) V =  (V/255);                                            \
 41         else      S *= (100);                                              \
 42       }                                                                    \
 43       else                                                                 \
 44         { S = 0; H = 0;}                                                   \
 45     }                                                                      \
 46     else                                                                   \
 47     {                                                                      \
 48       V = R;                                                               \
 49       if(V != 0)                                                           \
 50       {                                                                    \
 51         double min;                                                        \
 52         if(G > B) min = B;                                                 \
 53         else      min = G;                                                 \
 54         const double delta = V - min;                                      \
 55         if(delta != 0)                                                     \
 56           { S = (delta/V); H = (G - B) / delta; }                          \
 57         else                                                               \
 58           { S = 0;         H = (G - B); }                                  \
 59         H *=   60; if(H < 0) H += 360;                                     \
 60         if(!NORM) V =  (V/255);                                            \
 61         else      S *= (100);                                              \
 62       }                                                                    \
 63       else                                                                 \
 64         { S = 0; H = 0;}                                                   \
 65     }
 66        
 67     // //////////////////////////////////////////////////////////////////
 68     // 
 69     // cvlab.net
 70     // C/C++ Macro HSV to RGB
 71     #define PIX_HSV_TO_RGB_COMMON(H,S,V,R,G,B)                          \
 72     if( V == 0 )                                                        \
 73     { R = 0; G = 0; B = 0; }                                            \
 74     else if( S == 0 )                                                   \
 75     {                                                                   \
 76       R = V;                                                            \
 77       G = V;                                                            \
 78       B = V;                                                            \
 79     }                                                                   \
 80     else                                                                \
 81     {                                                                   \
 82       const double hf = H / 60.0;                                       \
 83       const int    i  = (int) floor( hf );                              \
 84       const double f  = hf - i;                                         \
 85       const double pv  = V * ( 1 - S );                                 \
 86       const double qv  = V * ( 1 - S * f );                             \
 87       const double tv  = V * ( 1 - S * ( 1 - f ) );                     \
 88       switch( i )                                                       \
 89         {                                                               \
 90         case 0:                                                         \
 91           R = V;                                                        \
 92           G = tv;                                                       \
 93           B = pv;                                                       \
 94           break;                                                        \
 95         case 1:                                                         \
 96           R = qv;                                                       \
 97           G = V;                                                        \
 98           B = pv;                                                       \
 99           break;                                                        \
100         case 2:                                                         \
101           R = pv;                                                       \
102           G = V;                                                        \
103           B = tv;                                                       \
104           break;                                                        \
105         case 3:                                                         \
106           R = pv;                                                       \
107           G = qv;                                                       \
108           B = V;                                                        \
109           break;                                                        \
110         case 4:                                                         \
111           R = tv;                                                       \
112           G = pv;                                                       \
113           B = V;                                                        \
114           break;                                                        \
115         case 5:                                                         \
116           R = V;                                                        \
117           G = pv;                                                       \
118           B = qv;                                                       \
119           break;                                                        \
120         case 6:                                                         \
121           R = V;                                                        \
122           G = tv;                                                       \
123           B = pv;                                                       \
124           break;                                                        \
125         case -1:                                                        \
126           R = V;                                                        \
127           G = pv;                                                       \
128           B = qv;                                                       \
129           break;                                                        \
130         default:                                                        \
131           LFATAL("i Value error in Pixel conversion, Value is %d",i);   \
132           break;                                                        \
133         }                                                               \
134     }                                                                   \
135     R *= 255.0F;                                                        \
136     G *= 255.0F;                                                        \
137     B *= 255.0F;
138  
139  
140 ==============================================================================================================

 


在图像和视频处理中,经常需要在颜色空间YUV和RGB间转换。下面介绍如何在这两种格式间进行转换。
YUV可以实现较高的数据压缩率,这主要是由于U、V分量可以实现高度压缩。而RGB888采用8bits分别表示R、G、B三个分量。黑色:R = G = B = 0;白色:R = G = B = 255。

RGB888的采样格式为4:4:4。

转换RGB888为YUV
转换公式为:

1     Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) + 16
2     U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128
3     V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128
上述公式的结果为8bit,但中间结果需要16bits的精度。

 


转换YUV到RGB888
转换公式为:

1     C = Y - 16
2     D = U - 128
3     E = V - 128

利用上述结果,然后计算RGB,
1     R = clip(( 298 * C           + 409 * E + 128) >> 8)
2     G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8)
3     B = clip(( 298 * C + 516 * D           + 128) >> 8)

这里clip() 表示限制RGB的值的范围在(0~255)。这些转换结果精度仍然为8bit,但是中间结果需要16bits精度。

 

注意当结果超出范围时,需要进行饱和截止。
如何要转换4:2:0 或者 4:2:2 YUV,需要首先转换YUV为4:4:4格式的YUV,然后再转换4:4:4 YUV为RGB888。

 

 

 

Guess you like

Origin www.cnblogs.com/xiyanhuakai/p/20191209_0046.html