Color Space convertion

http://docs.opencv.org/trunk/modules/imgproc/doc/miscellaneous_transformations.html?highlight=cvtcolor#void%20cvtColor%28InputArray%20src,%20OutputArray%20dst,%20int%20code,%20int%20dstCn%29

cvtColor

Converts an image from one color space to another.

C++: void cvtColor (InputArray src, OutputArray dst, int code, int dstCn=0 )

Python: cv2. cvtColor (src, code [, dst [, dstCn ] ] ) → dst

C: void cv CvtColor (const CvArr* src, CvArr* dst, int code )
Parameters:
  • src – input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision floating-point.
  • dst – output image of the same size and depth as src.
  • code – color space conversion code (see the description below).
  • dstCn – number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code .

The function converts an input image from one colorspace to another. In case of a transformation to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR).Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.

The conventional ranges for R, G, and B channel values are:

  • 0 to 255 for CV_8U images
  • 0 to 65535 for CV_16U images
  • 0 to 1 for CV_32F images

In case of linear transformations, the range does not matter.But in case of a non-linear transformation, an input RGB image should be normalized to the proper value range to get the correct results, for example, for RGB L*u*v* transformation. For example, if you have a 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will have the 0..255 value range instead of 0..1 assumed by the function. So, before calling cvtColor , you need first to scale the image down:

img *= 1./255;
cvtColor(img, img, COLOR_BGR2Luv);

If you use cvtColor with 8-bit images, the conversion will have some information lost. For many applications, this will not be noticeable but it is recommended to use 32-bit images in applications that need the full range of colors or that convert an image before an operation and then convert back.

The function can do the following transformations:

  • RGB GRAY ( CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR, CV_GRAY2RGB )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:

    and

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

    cvtColor(src, bwsrc, COLOR_RGB2GRAY);
    

    More advanced channel reordering can also be done withmixChannels() .

  • RGB CIE XYZ.Rec 709 with D65 white point ( COLOR_BGR2XYZ, COLOR_RGB2XYZ, COLOR_XYZ2BGR, COLOR_XYZ2RGB ):

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

  • RGB YCrCb JPEG (or YCC) ( COLOR_BGR2YCrCb, COLOR_RGB2YCrCb, COLOR_YCrCb2BGR, COLOR_YCrCb2RGB )

    where

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

  • RGB HSV ( COLOR_BGR2HSV, COLOR_RGB2HSV, COLOR_HSV2BGR, COLOR_HSV2RGB )

    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.

    If then . On output, , .

    The values are then converted to the destination data type:

    • 8-bit images

    • 16-bit images (currently not supported)

    • 32-bit images

      H, S, and V are left as is

  • RGB HLS ( COLOR_BGR2HLS, COLOR_RGB2HLS, COLOR_HLS2BGR, COLOR_HLS2RGB ).

    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.

    If then . On output, , .

    The values are then converted to the destination data type:

    • 8-bit images

    • 16-bit images (currently not supported)

    • 32-bit images

      H, S, V are left as is

  • RGB CIE L*a*b* ( COLOR_BGR2Lab, COLOR_RGB2Lab, COLOR_Lab2BGR, COLOR_Lab2RGB ).

    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.

    where

    and

    This outputs, , . The values are then converted to the destination data type:

    • 8-bit images

    • 16-bit images

      (currently not supported)

    • 32-bit images

      L, a, and b are left as is

  • RGB CIE L*u*v* ( COLOR_BGR2Luv, COLOR_RGB2Luv, COLOR_Luv2BGR, COLOR_Luv2RGB ).

    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.

    This outputs, , .

    The values are then converted to the destination data type:

    • 8-bit images

    • 16-bit images

      (currently not supported)

    • 32-bit images

      L, u, and v are left as is

    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 sitehttp://www.poynton.com/ColorFAQ.html

  • Bayer RGB ( COLOR_BayerBG2BGR, COLOR_BayerGB2BGR, COLOR_BayerRG2BGR, COLOR_BayerGR2BGR, COLOR_BayerBG2RGB, COLOR_BayerGB2RGB, COLOR_BayerRG2RGB, COLOR_BayerGR2RGB ). 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:

    The output RGB components of a pixel are interpolated from 1, 2, or4 neighbors of the pixel having the same color. There are severalmodifications of the above pattern that can be achieved by shiftingthe pattern one pixel left and/or one pixel up. The two letters and in the conversion constants CV_Bayer 2BGR and CV_Bayer 2RGB indicate the particular patterntype. These are components from the second row, second and thirdcolumns, respectively. For example, the above pattern has a verypopular “BG” type.

你可能感兴趣的:(Color Space convertion)