内容纲要
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

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 .
Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed).

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

  • RGB <-> GRAY ( CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR, CV_GRAY2RGB )

  • RGB <-> YCrCb JPEG (or YCC) ( CV_BGR2YCrCb,CV_RGB2YCrCb,CV_YCrCb2BGR,CV_YCrCb2RGB )

  • RGB <-> HSV ( CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB )

  • RGB <-> HLS ( CV_BGR2HLS, CV_RGB2HLS, CV_HLS2BGR, CV_HLS2RGB )

  • RGB <-> CIE Lab* ( CV_BGR2Lab, CV_RGB2Lab, CV_Lab2BGR, CV_Lab2RGB )

  • RGB <-> CIE Luv* ( CV_BGR2Luv, CV_RGB2Luv, CV_Luv2BGR, CV_Luv2RGB )

  • Bayer <-> RGB ( CV_BayerBG2BGR, CV_BayerGB2BGR, CV_BayerRG2BGR, CV_BayerGR2BGR, CV_BayerBG2RGB, CV_BayerGB2RGB, CV_BayerRG2RGB, CV_BayerGR2RGB )

  • RGB <-> CIE XYZ.Rec 709 with D65 white point ( CV_BGR2XYZ, CV_RGB2XYZ, CV_XYZ2BGR, CV_XYZ2RGB )

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"

using namespace cv;
using namespace std;

int main( )
{

       Mat image;
       image = imread("lena.jpg", CV_LOAD_IMAGE_COLOR);  

       if(! image.data )                             
       {
              cout <<  "Could not open or find the image" << std::endl ;
              return -1;
       }

       // Create a new matrix to hold the gray image
       Mat gray;

       // convert RGB image to gray
       cvtColor(image, gray, CV_BGR2GRAY);

       namedWindow( "Display window", CV_WINDOW_AUTOSIZE );  
       imshow( "Display window", image );                 

       namedWindow( "Result window", CV_WINDOW_AUTOSIZE );   
       imshow( "Result window", gray );

       waitKey(0);                                          
       return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注