Home | History | Annotate | Download | only in doc
      1 Color conversions {#imgproc_color_conversions}
      2 =================
      3 See cv::cvtColor and cv::ColorConversionCodes
      4 @todo document other conversion modes
      5 
      6 @anchor color_convert_rgb_gray
      7 RGB \f$\leftrightarrow\f$ GRAY
      8 ------------------------------
      9 Transformations within RGB space like adding/removing the alpha channel, reversing the channel
     10 order, conversion to/from 16-bit RGB color (R5:G6:B5 or R5:G5:B5), as well as conversion
     11 to/from grayscale using:
     12 \f[\text{RGB[A] to Gray:} \quad Y  \leftarrow 0.299  \cdot R + 0.587  \cdot G + 0.114  \cdot B\f]
     13 and
     14 \f[\text{Gray to RGB[A]:} \quad R  \leftarrow Y, G  \leftarrow Y, B  \leftarrow Y, A  \leftarrow \max (ChannelRange)\f]
     15 The conversion from a RGB image to gray is done with:
     16 @code
     17     cvtColor(src, bwsrc, cv::COLOR_RGB2GRAY);
     18 @endcode
     19 More advanced channel reordering can also be done with cv::mixChannels.
     20 @see cv::COLOR_BGR2GRAY, cv::COLOR_RGB2GRAY, cv::COLOR_GRAY2BGR, cv::COLOR_GRAY2RGB
     21 
     22 @anchor color_convert_rgb_xyz
     23 RGB \f$\leftrightarrow\f$ CIE XYZ.Rec 709 with D65 white point
     24 --------------------------------------------------------------
     25 \f[\begin{bmatrix} X  \\ Y  \\ Z
     26   \end{bmatrix} \leftarrow \begin{bmatrix} 0.412453 & 0.357580 & 0.180423 \\ 0.212671 & 0.715160 & 0.072169 \\ 0.019334 & 0.119193 & 0.950227
     27   \end{bmatrix} \cdot \begin{bmatrix} R  \\ G  \\ B
     28   \end{bmatrix}\f]
     29 \f[\begin{bmatrix} R  \\ G  \\ B
     30   \end{bmatrix} \leftarrow \begin{bmatrix} 3.240479 & -1.53715 & -0.498535 \\ -0.969256 &  1.875991 & 0.041556 \\ 0.055648 & -0.204043 & 1.057311
     31   \end{bmatrix} \cdot \begin{bmatrix} X  \\ Y  \\ Z
     32   \end{bmatrix}\f]
     33 \f$X\f$, \f$Y\f$ and \f$Z\f$ cover the whole value range (in case of floating-point images, \f$Z\f$ may exceed 1).
     34 
     35 @see cv::COLOR_BGR2XYZ, cv::COLOR_RGB2XYZ, cv::COLOR_XYZ2BGR, cv::COLOR_XYZ2RGB
     36 
     37 @anchor color_convert_rgb_ycrcb
     38 RGB \f$\leftrightarrow\f$ YCrCb JPEG (or YCC)
     39 ---------------------------------------------
     40 \f[Y  \leftarrow 0.299  \cdot R + 0.587  \cdot G + 0.114  \cdot B\f]
     41 \f[Cr  \leftarrow (R-Y)  \cdot 0.713 + delta\f]
     42 \f[Cb  \leftarrow (B-Y)  \cdot 0.564 + delta\f]
     43 \f[R  \leftarrow Y + 1.403  \cdot (Cr - delta)\f]
     44 \f[G  \leftarrow Y - 0.714  \cdot (Cr - delta) - 0.344  \cdot (Cb - delta)\f]
     45 \f[B  \leftarrow Y + 1.773  \cdot (Cb - delta)\f]
     46 where
     47 \f[delta =  \left \{ \begin{array}{l l} 128 &  \mbox{for 8-bit images} \\ 32768 &  \mbox{for 16-bit images} \\ 0.5 &  \mbox{for floating-point images} \end{array} \right .\f]
     48 Y, Cr, and Cb cover the whole value range.
     49 @see cv::COLOR_BGR2YCrCb, cv::COLOR_RGB2YCrCb, cv::COLOR_YCrCb2BGR, cv::COLOR_YCrCb2RGB
     50 
     51 @anchor color_convert_rgb_hsv
     52 RGB \f$\leftrightarrow\f$ HSV
     53 -----------------------------
     54 In case of 8-bit and 16-bit images, R, G, and B are converted to the floating-point format and
     55 scaled to fit the 0 to 1 range.
     56 
     57 \f[V  \leftarrow max(R,G,B)\f]
     58 \f[S  \leftarrow \fork{\frac{V-min(R,G,B)}{V}}{if \(V \neq 0\)}{0}{otherwise}\f]
     59 \f[H  \leftarrow \forkthree{{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\)}\f]
     60 If \f$H<0\f$ then \f$H \leftarrow H+360\f$ . On output \f$0 \leq V \leq 1\f$, \f$0 \leq S \leq 1\f$,
     61 \f$0 \leq H \leq 360\f$ .
     62 
     63 The values are then converted to the destination data type:
     64 - 8-bit images: \f$V  \leftarrow 255 V, S  \leftarrow 255 S, H  \leftarrow H/2  \text{(to fit to 0 to 255)}\f$
     65 - 16-bit images: (currently not supported) \f$V <- 65535 V, S <- 65535 S, H <- H\f$
     66 - 32-bit images: H, S, and V are left as is
     67 
     68 @see cv::COLOR_BGR2HSV, cv::COLOR_RGB2HSV, cv::COLOR_HSV2BGR, cv::COLOR_HSV2RGB
     69 
     70 @anchor color_convert_rgb_hls
     71 RGB \f$\leftrightarrow\f$ HLS
     72 -----------------------------
     73 In case of 8-bit and 16-bit images, R, G, and B are converted to the floating-point format and
     74 scaled to fit the 0 to 1 range.
     75 
     76 \f[V_{max}  \leftarrow {max}(R,G,B)\f]
     77 \f[V_{min}  \leftarrow {min}(R,G,B)\f]
     78 \f[L  \leftarrow \frac{V_{max} + V_{min}}{2}\f]
     79 \f[S  \leftarrow \fork { \frac{V_{max} - V_{min}}{V_{max} + V_{min}} }{if  \(L < 0.5\) }
     80     { \frac{V_{max} - V_{min}}{2 - (V_{max} + V_{min})} }{if  \(L \ge 0.5\) }\f]
     81 \f[H  \leftarrow \forkthree {{60(G - B)}/{(V_{max}-V_{min})}}{if  \(V_{max}=R\) }
     82   {{120+60(B - R)}/{(V_{max}-V_{min})}}{if  \(V_{max}=G\) }
     83   {{240+60(R - G)}/{(V_{max}-V_{min})}}{if  \(V_{max}=B\) }\f]
     84 If \f$H<0\f$ then \f$H \leftarrow H+360\f$ . On output \f$0 \leq L \leq 1\f$, \f$0 \leq S \leq
     85 1\f$, \f$0 \leq H \leq 360\f$ .
     86 
     87 The values are then converted to the destination data type:
     88 - 8-bit images:  \f$V  \leftarrow 255 \cdot V, S  \leftarrow 255 \cdot S, H  \leftarrow H/2 \; \text{(to fit to 0 to 255)}\f$
     89 - 16-bit images: (currently not supported)  \f$V <- 65535 \cdot V, S <- 65535 \cdot S, H <- H\f$
     90 - 32-bit images: H, S, V are left as is
     91 
     92 @see cv::COLOR_BGR2HLS, cv::COLOR_RGB2HLS, cv::COLOR_HLS2BGR, cv::COLOR_HLS2RGB
     93 
     94 @anchor color_convert_rgb_lab
     95 RGB \f$\leftrightarrow\f$ CIE L\*a\*b\*
     96 ---------------------------------------
     97 In case of 8-bit and 16-bit images, R, G, and B are converted to the floating-point format and
     98 scaled to fit the 0 to 1 range.
     99 
    100 \f[\vecthree{X}{Y}{Z} \leftarrow \vecthreethree{0.412453}{0.357580}{0.180423}{0.212671}{0.715160}{0.072169}{0.019334}{0.119193}{0.950227} \cdot \vecthree{R}{G}{B}\f]
    101 \f[X  \leftarrow X/X_n,  \text{where} X_n = 0.950456\f]
    102 \f[Z  \leftarrow Z/Z_n,  \text{where} Z_n = 1.088754\f]
    103 \f[L  \leftarrow \fork{116*Y^{1/3}-16}{for \(Y>0.008856\)}{903.3*Y}{for \(Y \le 0.008856\)}\f]
    104 \f[a  \leftarrow 500 (f(X)-f(Y)) + delta\f]
    105 \f[b  \leftarrow 200 (f(Y)-f(Z)) + delta\f]
    106 where
    107 \f[f(t)= \fork{t^{1/3}}{for \(t>0.008856\)}{7.787 t+16/116}{for \(t\leq 0.008856\)}\f]
    108 and
    109 \f[delta =  \fork{128}{for 8-bit images}{0}{for floating-point images}\f]
    110 
    111 This outputs \f$0 \leq L \leq 100\f$, \f$-127 \leq a \leq 127\f$, \f$-127 \leq b \leq 127\f$ . The values
    112 are then converted to the destination data type:
    113 - 8-bit images:  \f$L  \leftarrow L*255/100, \; a  \leftarrow a + 128, \; b  \leftarrow b + 128\f$
    114 - 16-bit images:  (currently not supported)
    115 - 32-bit images:  L, a, and b are left as is
    116 
    117 @see cv::COLOR_BGR2Lab, cv::COLOR_RGB2Lab, cv::COLOR_Lab2BGR, cv::COLOR_Lab2RGB
    118 
    119 @anchor color_convert_rgb_luv
    120 RGB \f$\leftrightarrow\f$ CIE L\*u\*v\*
    121 ---------------------------------------
    122 In case of 8-bit and 16-bit images, R, G, and B are converted to the floating-point format and
    123 scaled to fit 0 to 1 range.
    124 
    125 \f[\vecthree{X}{Y}{Z} \leftarrow \vecthreethree{0.412453}{0.357580}{0.180423}{0.212671}{0.715160}{0.072169}{0.019334}{0.119193}{0.950227} \cdot \vecthree{R}{G}{B}\f]
    126 \f[L  \leftarrow \fork{116 Y^{1/3}}{for \(Y>0.008856\)}{903.3 Y}{for \(Y\leq 0.008856\)}\f]
    127 \f[u'  \leftarrow 4*X/(X + 15*Y + 3 Z)\f]
    128 \f[v'  \leftarrow 9*Y/(X + 15*Y + 3 Z)\f]
    129 \f[u  \leftarrow 13*L*(u' - u_n)  \quad \text{where} \quad u_n=0.19793943\f]
    130 \f[v  \leftarrow 13*L*(v' - v_n)  \quad \text{where} \quad v_n=0.46831096\f]
    131 
    132 This outputs \f$0 \leq L \leq 100\f$, \f$-134 \leq u \leq 220\f$, \f$-140 \leq v \leq 122\f$ .
    133 
    134 The values are then converted to the destination data type:
    135 -   8-bit images:  \f$L  \leftarrow 255/100 L, \; u  \leftarrow 255/354 (u + 134), \; v  \leftarrow 255/262 (v + 140)\f$
    136 -   16-bit images:   (currently not supported)
    137 -   32-bit images:   L, u, and v are left as is
    138 
    139 The above formulae for converting RGB to/from various color spaces have been taken from multiple
    140 sources on the web, primarily from the Charles Poynton site <http://www.poynton.com/ColorFAQ.html>
    141 
    142 @see cv::COLOR_BGR2Luv, cv::COLOR_RGB2Luv, cv::COLOR_Luv2BGR, cv::COLOR_Luv2RGB
    143 
    144 @anchor color_convert_bayer
    145 Bayer \f$\rightarrow\f$ RGB
    146 ---------------------------
    147 The Bayer pattern is widely used in CCD and CMOS cameras. It enables you to get color pictures
    148 from a single plane where R,G, and B pixels (sensors of a particular component) are interleaved
    149 as follows:
    150 
    151 ![Bayer pattern](pics/bayer.png)
    152 
    153 The output RGB components of a pixel are interpolated from 1, 2, or 4 neighbors of the pixel
    154 having the same color. There are several modifications of the above pattern that can be achieved
    155 by shifting the pattern one pixel left and/or one pixel up. The two letters \f$C_1\f$ and \f$C_2\f$ in
    156 the conversion constants CV_Bayer \f$C_1 C_2\f$ 2BGR and CV_Bayer \f$C_1 C_2\f$ 2RGB indicate the
    157 particular pattern type. These are components from the second row, second and third columns,
    158 respectively. For example, the above pattern has a very popular "BG" type.
    159 
    160 @see cv::COLOR_BayerBG2BGR, cv::COLOR_BayerGB2BGR, cv::COLOR_BayerRG2BGR, cv::COLOR_BayerGR2BGR, cv::COLOR_BayerBG2RGB, cv::COLOR_BayerGB2RGB, cv::COLOR_BayerRG2RGB, cv::COLOR_BayerGR2RGB
    161