cvtColor()学习

1 CvtColor
2 Void cv::cvtColor(InputArray src,
3                         OutputArray dst,
4                         INT code,
5                         INT dstCn =0 
6 7     

将图像从一个颜色空间转换为另一个。

该函数将输入图像从一个颜色空间转换为另一个颜色空间。在从RGB颜色空间转换的情况下,应明确指定通道的顺序(RGB或BGR)。请注意,OpenCV中的默认颜色格式通常称为RGB,但实际上是BGR(字节相反)。因此,标准(24位)彩色图像中的第一个字节将为8位蓝色分量,第二个字节为绿色,第三个字节为红色。第四,第五和第六个字节将是第二个像素(蓝色,然后是绿色,然后是红色),依此类推。

R,G和B通道值的常规范围是:

  • 0到255为CV_8U图像
  • 0到65535的CV_16U图像
  • 0到1用于CV_32F图像

在线性变换的情况下,范围无关紧要。但是在非线性变换的情况下,输入RGB图像应该被归一化到适当的值范围,以获得正确的结果,例如RGB →L * u * v *转换。例如,如果您有一个32位浮点图像直接从8位图像转换而不进行任何缩放,那么它将具有0..255的值范围而不是该函数假定的0..1。所以,在调用cvtColor之前,您需要先将图像缩小:

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

如果您使用cvtColor与8位图像,转换将有一些信息丢失。对于许多应用程序,这并不会引人注目,但建议在需要全部颜色的应用程序中使用32位图像,或者在操作之前转换图像然后转换。

如果转换添加了Alpha通道,其值将设置为相应通道范围的最大值:CV_8U为255,CV_16U为65535,CV_32F为1。

参数

SRC

输入图像:8位无符号,16位无符号(CV_16UC ...)或单精度浮点。

DST

输出与src相同大小和深度的图像。

code

颜色空间转换代码(见cv :: ColorConversionCodes)。

dstCn

目的地图像中的频道数; 如果参数为0,则从src和代码自动导出通道数。

也可以看看

Color conversions

例子:

camshiftdemo.cppedge.cppfacedetect.cppffilldemo.cpphoughcircles.cpphoughlines.cpplkdemo.cppwatershed.cpp

 

转换模式

RGB  GRAY

RGB模式增加移除alpha通道、改变通道顺序、和16位RGB色彩模式(R5:G6:B5 或者 R5:G5:B5)的图像之间的转换、和灰度图之间的转换:

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

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

示例:

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

高级用法见cv::mixChannels

或者

cv::COLOR_BGR2GRAYcv::COLOR_RGB2GRAYcv::COLOR_GRAY2BGRcv::COLOR_GRAY2RGB

RGB ↔ CIE XYZ.Rec 709 with D65 white point

X, Y and Z cover the whole value range (in case of floating-point images, Z may exceed 1).what’s the meaning of this sentence?

用法见:

cv::COLOR_BGR2XYZcv::COLOR_RGB2XYZcv::COLOR_XYZ2BGRcv::COLOR_XYZ2RGB

RGB ↔ YCrCb JPEG (or YCC)

 

其中

 

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

用法见:

cv::COLOR_BGR2YCrCbcv::COLOR_RGB2YCrCbcv::COLOR_YCrCb2BGRcv::COLOR_YCrCb2RGB

RGB ↔ HSV

在8位和16位图像的情况下,R,G和B将转换为浮点格式并缩放以适合0到1的范围。

用法见:

cv::COLOR_BGR2HSVcv::COLOR_RGB2HSVcv::COLOR_HSV2BGRcv::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.

 

用法见:

cv::COLOR_BGR2HLScv::COLOR_RGB2HLScv::COLOR_HLS2BGRcv::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.

 

用法见:

cv::COLOR_BGR2Labcv::COLOR_RGB2Labcv::COLOR_Lab2BGRcv::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.

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

用法见:

cv::COLOR_BGR2Luvcv::COLOR_RGB2Luvcv::COLOR_Luv2BGRcv::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:

 

The output RGB components of a pixel are interpolated from 1, 2, or 4 neighbors of the pixel having the same color. 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 C and C 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.

用法见:

cv::COLOR_BayerBG2BGRcv::COLOR_BayerGB2BGRcv::COLOR_BayerRG2BGRcv::COLOR_BayerGR2BGR

cv::COLOR_BayerBG2RGBcv::COLOR_BayerGB2RGBcv::COLOR_BayerRG2RGBcv::COLOR_BayerGR2RGB

原文地址:https://www.cnblogs.com/anqiang1995/p/7442984.html