docs.opencv.org Open in urlscan Pro
2a06:98c1:3121::3  Public Scan

Submitted URL: https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html#color_convert_rgb_xyz
Effective URL: https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html
Submission: On October 12 via api from US — Scanned from NL

Form analysis 1 forms found in the DOM

<form class="gsc-search-box gsc-search-box-tools" accept-charset="utf-8">
  <table cellspacing="0" cellpadding="0" role="presentation" class="gsc-search-box">
    <tbody>
      <tr>
        <td class="gsc-input">
          <div class="gsc-input-box" id="gsc-iw-id1">
            <table cellspacing="0" cellpadding="0" role="presentation" id="gs_id50" class="gstl_50 gsc-input" style="width: 100%; padding: 0px;">
              <tbody>
                <tr>
                  <td id="gs_tti50" class="gsib_a"><input autocomplete="off" type="text" size="10" class="gsc-input" name="search" title="search" aria-label="search" id="gsc-i-id1" dir="ltr" spellcheck="false"
                      style="width: 100%; padding: 0px; border: none; margin: 0px; height: auto; background: url(&quot;https://www.google.com/cse/static/images/1x/en/branding.png&quot;) left center no-repeat rgb(255, 255, 255); outline: none;"></td>
                  <td class="gsib_b">
                    <div class="gsst_b" id="gs_st50" dir="ltr"><a class="gsst_a" href="javascript:void(0)" title="Clear search box" role="button" style="display: none;"><span class="gscb_a" id="gs_cb50" aria-hidden="true">×</span></a></div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </td>
        <td class="gsc-search-button"><button class="gsc-search-button gsc-search-button-v2"><svg width="13" height="13" viewBox="0 0 13 13">
              <title>search</title>
              <path
                d="m4.8495 7.8226c0.82666 0 1.5262-0.29146 2.0985-0.87438 0.57232-0.58292 0.86378-1.2877 0.87438-2.1144 0.010599-0.82666-0.28086-1.5262-0.87438-2.0985-0.59352-0.57232-1.293-0.86378-2.0985-0.87438-0.8055-0.010599-1.5103 0.28086-2.1144 0.87438-0.60414 0.59352-0.8956 1.293-0.87438 2.0985 0.021197 0.8055 0.31266 1.5103 0.87438 2.1144 0.56172 0.60414 1.2665 0.8956 2.1144 0.87438zm4.4695 0.2115 3.681 3.6819-1.259 1.284-3.6817-3.7 0.0019784-0.69479-0.090043-0.098846c-0.87973 0.76087-1.92 1.1413-3.1207 1.1413-1.3553 0-2.5025-0.46363-3.4417-1.3909s-1.4088-2.0686-1.4088-3.4239c0-1.3553 0.4696-2.4966 1.4088-3.4239 0.9392-0.92727 2.0864-1.3969 3.4417-1.4088 1.3553-0.011889 2.4906 0.45771 3.406 1.4088 0.9154 0.95107 1.379 2.0924 1.3909 3.4239 0 1.2126-0.38043 2.2588-1.1413 3.1385l0.098834 0.090049z">
              </path>
            </svg></button></td>
        <td class="gsc-clear-button">
          <div class="gsc-clear-button" title="clear results">&nbsp;</div>
        </td>
      </tr>
    </tbody>
  </table>
</form>

Text Content

×

search
 

Custom Search

Sort by:
Relevance

Relevance
Date




OpenCV
 4.8.0-dev4.8.04.8.0-pre5.0.0-pre4.7.04.6.04.5.54.5.44.5.34.5.24.5.14.5.04.4.04.3.04.2.04.1.24.1.14.1.04.0.14.0.03.4.20-dev3.4.203.4.193.4.183.4.173.4.163.4.153.4.143.4.133.4.123.4.113.4.103.4.93.4.83.4.73.4.63.4.53.4.43.4.33.4.23.4.13.4.03.3.13.3.03.2.03.1.03.0.0
Open Source Computer Vision

 * Main Page
 * Related Pages
 * Modules
 * +Namespaces
   * Namespace List
 * +Classes
   * Class List
   * Class Index
 * +Files
   * Files index
   * +Global objects
     * +All
       * _
       * a
       * c
       * d
       * f
       * h
       * i
       * k
       * l
       * m
       * n
       * o
       * p
       * r
       * s
       * t
       * u
       * v
     * +Functions
       * _
       * a
       * c
       * h
       * m
       * n
       * p
       * u
     * Variables
     * +Typedefs
       * c
       * i
       * n
       * s
       * u
       * v
     * Enumerations
     * +Enumerator
       * c
       * n
     * +Macros
       * _
       * c
       * d
       * f
       * h
       * i
       * l
       * m
       * n
       * o
       * r
       * s
       * t
       * u
       * v
 * Examples
 * Java documentation
 * 

•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages

Color conversions

See cv::cvtColor and cv::ColorConversionCodes

Todo:document other conversion modes




RGB ↔ GRAY

Transformations within RGB space like adding/removing the alpha channel,
reversing the channel order, conversion to/from 16-bit RGB color (R5:G6:B5 or
R5:G5:B5), as well as conversion to/from grayscale using:



RGB[A] to Gray:Y←0.299⋅R+0.587⋅G+0.114⋅B



and



Gray to RGB[A]:R←Y,G←Y,B←Y,A←max(ChannelRange)



The conversion from a RGB image to gray is done with:

cvtColor(src, bwsrc, cv::COLOR_RGB2GRAY);

More advanced channel reordering can also be done with cv::mixChannels.

See alsocv::COLOR_BGR2GRAY, cv::COLOR_RGB2GRAY, cv::COLOR_GRAY2BGR,
cv::COLOR_GRAY2RGB




RGB ↔ CIE XYZ.REC 709 WITH D65 WHITE POINT



⎡⎣⎢XYZ⎤⎦⎥←⎡⎣⎢0.4124530.2126710.0193340.3575800.7151600.1191930.1804230.0721690.950227⎤⎦⎥⋅⎡⎣⎢RGB⎤⎦⎥





⎡⎣⎢RGB⎤⎦⎥←⎡⎣⎢3.240479−0.9692560.055648−1.537151.875991−0.204043−0.4985350.0415561.057311⎤⎦⎥⋅⎡⎣⎢XYZ⎤⎦⎥



X, Y and Z cover the whole value range (in case of floating-point images, Z may
exceed 1).

See alsocv::COLOR_BGR2XYZ, cv::COLOR_RGB2XYZ, cv::COLOR_XYZ2BGR,
cv::COLOR_XYZ2RGB




RGB ↔ YCRCB JPEG (OR YCC)



Y←0.299⋅R+0.587⋅G+0.114⋅B





Cr←(R−Y)⋅0.713+delta





Cb←(B−Y)⋅0.564+delta





R←Y+1.403⋅(Cr−delta)





G←Y−0.714⋅(Cr−delta)−0.344⋅(Cb−delta)





B←Y+1.773⋅(Cb−delta)



where



delta=⎧⎩⎨⎪⎪128327680.5for 8-bit imagesfor 16-bit imagesfor floating-point images



Y, Cr, and Cb cover the whole value range.

See alsocv::COLOR_BGR2YCrCb, cv::COLOR_RGB2YCrCb, cv::COLOR_YCrCb2BGR,
cv::COLOR_YCrCb2RGB




RGB ↔ HSV

In case of 8-bit and 16-bit images, R, G, and B are converted to the
floating-point format and scaled to fit the 0 to 1 range.



V←max(R,G,B)





S←{V−min(R,G,B)V0if V≠0otherwise





H←⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪60(G−B)/(V−min(R,G,B))120+60(B−R)/(V−min(R,G,B))240+60(R−G)/(V−min(R,G,B))0if V=Rif V=Gif V=Bif R=G=B



If H<0 then H←H+360 . On output 0≤V≤1, 0≤S≤1, 0≤H≤360 .

The values are then converted to the destination data type:

 * 8-bit images: V←255V,S←255S,H←H/2(to fit to 0 to 255)
 * 16-bit images: (currently not supported) V<−65535V,S<−65535S,H<−H
 * 32-bit images: H, S, and V are left as is

See alsocv::COLOR_BGR2HSV, cv::COLOR_RGB2HSV, cv::COLOR_HSV2BGR,
cv::COLOR_HSV2RGB




RGB ↔ HLS

In case of 8-bit and 16-bit images, R, G, and B are converted to the
floating-point format and scaled to fit the 0 to 1 range.



Vmax←max(R,G,B)





Vmin←min(R,G,B)





L←Vmax+Vmin2





S←⎧⎩⎨Vmax−VminVmax+VminVmax−Vmin2−(Vmax+Vmin)if L<0.5 if L≥0.5 





H←⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪60(G−B)/(Vmax−Vmin)120+60(B−R)/(Vmax−Vmin)240+60(R−G)/(Vmax−Vmin)0if Vmax=R if Vmax=G if Vmax=B if R=G=B 



If H<0 then H←H+360 . On output 0≤L≤1, 0≤S≤1, 0≤H≤360 .

The values are then converted to the destination data type:

 * 8-bit images: V←255⋅V,S←255⋅S,H←H/2(to fit to 0 to 255)
 * 16-bit images: (currently not supported) V<−65535⋅V,S<−65535⋅S,H<−H
 * 32-bit images: H, S, V are left as is

See alsocv::COLOR_BGR2HLS, cv::COLOR_RGB2HLS, cv::COLOR_HLS2BGR,
cv::COLOR_HLS2RGB




RGB ↔ CIE L*A*B*

In case of 8-bit and 16-bit images, R, G, and B are converted to the
floating-point format and scaled to fit the 0 to 1 range.



⎡⎣⎢XYZ⎤⎦⎥←⎡⎣⎢0.4124530.2126710.0193340.3575800.7151600.1191930.1804230.0721690.950227⎤⎦⎥⋅⎡⎣⎢RGB⎤⎦⎥





X←X/Xn,whereXn=0.950456





Z←Z/Zn,whereZn=1.088754





L←{116∗Y1/3−16903.3∗Yfor Y>0.008856for Y≤0.008856





a←500(f(X)−f(Y))+delta





b←200(f(Y)−f(Z))+delta



where



f(t)={t1/37.787t+16/116for t>0.008856for t≤0.008856



and



delta={1280for 8-bit imagesfor floating-point images



This outputs 0≤L≤100, −127≤a≤127, −127≤b≤127 . The values are then converted to
the destination data type:

 * 8-bit images: L←L∗255/100,a←a+128,b←b+128
 * 16-bit images: (currently not supported)
 * 32-bit images: L, a, and b are left as is

See alsocv::COLOR_BGR2Lab, cv::COLOR_RGB2Lab, cv::COLOR_Lab2BGR,
cv::COLOR_Lab2RGB




RGB ↔ CIE L*U*V*

In case of 8-bit and 16-bit images, R, G, and B are converted to the
floating-point format and scaled to fit 0 to 1 range.



⎡⎣⎢XYZ⎤⎦⎥←⎡⎣⎢0.4124530.2126710.0193340.3575800.7151600.1191930.1804230.0721690.950227⎤⎦⎥⋅⎡⎣⎢RGB⎤⎦⎥





L←{116∗Y1/3−16903.3Yfor Y>0.008856for Y≤0.008856





u′←4∗X/(X+15∗Y+3Z)





v′←9∗Y/(X+15∗Y+3Z)





u←13∗L∗(u′−un)whereun=0.19793943





v←13∗L∗(v′−vn)wherevn=0.46831096



This outputs 0≤L≤100, −134≤u≤220, −140≤v≤122 .

The values are then converted to the destination data type:

 * 8-bit images: L←255/100L,u←255/354(u+134),v←255/262(v+140)
 * 16-bit images: (currently not supported)
 * 32-bit images: L, u, and v are left as is

Note that when converting integer Luv images to RGB the intermediate X, Y and Z
values are truncated to [0,2] range to fit white point limitations. It may lead
to incorrect representation of colors with odd XYZ values.

The above formulae for converting RGB to/from various color spaces have been
taken from multiple sources on the web, primarily from the Charles Poynton site
http://www.poynton.com/ColorFAQ.html

See alsocv::COLOR_BGR2Luv, cv::COLOR_RGB2Luv, cv::COLOR_Luv2BGR,
cv::COLOR_Luv2RGB




BAYER → RGB

The Bayer pattern is widely used in CCD and CMOS cameras. It enables you to get
color pictures from a single plane where R, G, and B pixels (sensors of a
particular component) are interleaved as follows:

Bayer patterns (BGGR, GBRG, GRGB, RGGB)

The output RGB components of a pixel are interpolated from 1, 2, or 4 neighbors
of the pixel having the same color.

NoteSee the following for information about correspondences between OpenCV Bayer
pattern naming and classical Bayer pattern naming.
Bayer pattern

There are several modifications of the above pattern that can be achieved by
shifting the pattern one pixel left and/or one pixel up. The two letters C1 and
C2 in the conversion constants CV_Bayer C1C2 2BGR and CV_Bayer C1C2 2RGB
indicate the particular pattern type. These are components from the second row,
second and third columns, respectively. For example, the above pattern has a
very popular "BG" type.

See alsocv::COLOR_BayerRGGB2BGR, cv::COLOR_BayerGRBG2BGR,
cv::COLOR_BayerBGGR2BGR, cv::COLOR_BayerGBRG2BGR, cv::COLOR_BayerRGGB2RGB,
cv::COLOR_BayerGRBG2RGB, cv::COLOR_BayerBGGR2RGB, cv::COLOR_BayerGBRG2RGB
cv::COLOR_BayerBG2BGR, cv::COLOR_BayerGB2BGR, cv::COLOR_BayerRG2BGR,
cv::COLOR_BayerGR2BGR, cv::COLOR_BayerBG2RGB, cv::COLOR_BayerGB2RGB,
cv::COLOR_BayerRG2RGB, cv::COLOR_BayerGR2RGB

--------------------------------------------------------------------------------

Generated on Wed Oct 11 2023 01:39:58 for OpenCV by   1.8.13