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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM