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:

20 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
    2. Just wanna say a big thank you patrice cythia for introducing me to Dr Idedia the great HERBALIST that helped me prepare home remedies that cured my herpes simplex virus (HSV1&2)
      I was infected with herpes for the past two years and i was not happy all day so i was desperate to get a cure so that i can live normal and get healthy.One day i was less busy so i decided to make latest research on herpes  cure  and i found a site were everyone was talking about Dr Idedia and has ability to cure Herpes, HIV, HPV, Cancer, ALS, COPD, Anthrax, genital wart, cold sore, Arthritis skin tag and all manners of diseases and virus.So i discussed with cythia and she explained to me that its very easy working with Dr Idedia  so i contacted Dr Idedia via email  dr.idediatraditionalhealinghome@gmail.com and he helped me just as he has helped others now I am  now cured from herpes am very happy now.and i can also assure you that he can also help you. so if you need the service of DR Idedia kindly contact him dr.idediatraditionalhealinghome@gmail.com or whatsapp +2349074505296.
      visit his websites  https://dr-idedia-traditional-healing-home9.webnode.com/ Or https://sites.google.com/view/dridediatraditionalhealinghome/ Or https://dr-idedia-traditional-healing-home.business.site/ 

      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
    Replies
    1. by adding "cv::" before imshow if u're working with ROS

      Delete
  6. Replies
    1. Afraid not. https://usersnap.com/blog/programming-languages-2018/

      Delete
  7. Currently Python is the most popular Language in IT. Python adopted as a language of choice for almost all the domain in IT including Web Development, Cloud Computing (AWS, OpenStack, VMware, Google Cloud, etc.. ),Read More

    ReplyDelete
  8. We are among the best online companies providing affordable nursing essay writing services. When you nursing writing services, you get the services of experts and specialists in your field.

    ReplyDelete
  9. Criminology Papers Assignment Services online produces different assignments, including Criminology Research Paper Services, to help students deal with the Criminology Essay Writing Services challenges they face every day.

    ReplyDelete