OpenCV example to convert RGB to gray / other color spaces

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 \leftrightarrow GRAY ( CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR, CV_GRAY2RGB )
RGB \leftrightarrow YCrCb JPEG (or YCC) ( CV_BGR2YCrCb,CV_RGB2YCrCb,CV_YCrCb2BGR,CV_YCrCb2RGB )
RGB \leftrightarrow HSV ( CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB )
RGB \leftrightarrow HLS ( CV_BGR2HLS, CV_RGB2HLS, CV_HLS2BGR, CV_HLS2RGB )
RGB \leftrightarrow CIE L*a*b* ( CV_BGR2Lab, CV_RGB2Lab, CV_Lab2BGR, CV_Lab2RGB )
RGB \leftrightarrow CIE L*u*v* ( CV_BGR2Luv, CV_RGB2Luv, CV_Luv2BGR, CV_Luv2RGB )
Bayer \rightarrow RGB ( CV_BayerBG2BGR, CV_BayerGB2BGR, CV_BayerRG2BGR, CV_BayerGR2BGR, CV_BayerBG2RGB, CV_BayerGB2RGB, CV_BayerRG2RGB, CV_BayerGR2RGB )
RGB \leftrightarrow CIE XYZ.Rec 709 with D65 white point ( CV_BGR2XYZ, CV_RGB2XYZ, CV_XYZ2BGR, CV_XYZ2RGB )

Steps:

  1. Load an image 
  2. Convert to gray scale
  3. Show result

Functions:

Example:

-------------
#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;
}
-------------


Sources:

9 comments:

  1. Hi
    I want to use cvtColor function to convert RGB to HSV but unfortunately I have only cvCvtColor funtions and when I use it I face this ERROR:

    error C2664: 'cvCvtColor' : cannot convert parameter 1 from 'cv::Mat' to 'const CvArr *'

    can someone help me?

    ReplyDelete
    Replies
    1. you only convert 2 images have same type, Mat or IplImage

      Delete
  2. Thank you so much for the code. It helped me to rectify my one silly mistake!

    ReplyDelete
  3. can we use cvtColor function to convert binary image to RGB image

    ReplyDelete
    Replies
    1. There is no option for that in the cvtColor function. But you can first convert the image to a gray color image( read some papers for this) then convert to color.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. i have a little prob for example when i use imshow , it says that it's undefined ? haw can i fix this please

    ReplyDelete