Saturday, 6 June 2015

Convert an Image from one color space to another by using cvtcolor function

This opencv tutorial is about converting an image from one color space to that of the other using cvtcolor:

As the name suggests , cvtcolor converts an image from one color space to another.

Syntax of the code:
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

where
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 .

As it has already been mentioned that the functions converts an image form one color space to another, there are various types of conversion possible.


Transformation
Syntax
RGB Description: \leftrightarrow GRAY

CV_RGB2GRAY
BGR Description: \leftrightarrow GRAY

CV_BGR2GRAY
GRAY Description: \leftrightarrow RGB

CV_GRAY2RGB 
GRAY Description: \leftrightarrow BGR

CV_GRAY2BGR


Note:
The default color format in OpenCV is generally said to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red.


Other Tranformations are:-
Transformation
Syntax
RGB Description: \leftrightarrow YCrCb
CV_RGB2YCrCb
BGR Description: \leftrightarrow YCrCb
CV_BGR2YCrCb
YCrCb Description: \leftrightarrow RGB
CV_YCrCb2RGB 
 YCrCb
CV_YCrCb2BGR

Transformation
Syntax
RGB Description: \leftrightarrow HSV
CV_RGB2HSV
BGR Description: \leftrightarrow HSV
CV_BGR2HSV
HSVDescription: \leftrightarrow RGB
CV_HSV2RGB 
HSVDescription: \leftrightarrow BGR
CV_HSV2BGR

Transformation
Syntax
RGB Description: \leftrightarrow CIE L*a*b*
CV_RGB2Lab
BGR Description: \leftrightarrow CIE L*a*b*
CV_BGR2Lab
CIE L*a*b* Description: \leftrightarrow RGB
CV_Lab2RGB 
CIE L*a*b* Description: \leftrightarrow BGR
CV_Lab2BGR

Transformation
Syntax
RGB Description: \leftrightarrow CIE L*u*v*
CV_RGB2Luv
BGR Description: \leftrightarrow CIE L*u*v*
CV_BGR2Luv
CIE L*u*v*Description: \leftrightarrow RGB
CV_Luv2RGB 
CIE L*u*v*Description: \leftrightarrow BGR
CV_Luv2BGR

Transformation
Syntax
RGB Description: \leftrightarrow CIE XYZ
CV_RGB2XYZ
BGR Description: \leftrightarrow CIE XYZ
CV_BGR2XYZ
CIE XYZDescription: \leftrightarrow RGB
CV_XYZ2RGB 
CIE XYZDescription: \leftrightarrow BGR
CV_XYZ2BGR

Opencv Code for color space conversion:
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"

using namespace cv;

int main( )
{
// char* imageName = argv[1];

 Mat image;
 image = imread( "C:\\Users\\arjun\\Desktop\\color-image.png", 1 );

 Mat RGB2GRAY_image;
 cvtColor( image, RGB2GRAY_image, CV_RGB2GRAY );

 Mat BGR2GRAY_image;
 cvtColor( image, BGR2GRAY_image, CV_BGR2GRAY );

 Mat RGB2YCrCb_image;
 cvtColor( image, RGB2YCrCb_image, CV_RGB2YCrCb );

 Mat BGR2YCrCb_image;
 cvtColor( image, BGR2YCrCb_image, CV_BGR2YCrCb );

 Mat RGB2HSV_image;
 cvtColor( image, RGB2HSV_image, CV_RGB2HSV );

 Mat BGR2HSV_image;
 cvtColor( image, BGR2HSV_image, CV_BGR2HSV );

 Mat RGB2Lab_image;
 cvtColor( image, RGB2Lab_image, CV_RGB2Lab );

 Mat BGR2Lab_image;
 cvtColor( image, BGR2Lab_image, CV_BGR2Lab );

 Mat RGB2Luv_image;
 cvtColor( image, RGB2Luv_image, CV_RGB2Luv );

  Mat BGR2Luv_image;
 cvtColor( image, BGR2Luv_image, CV_BGR2Luv );

 Mat RGB2XYZ_image;
 cvtColor( image, RGB2XYZ_image, CV_RGB2XYZ );

 Mat BGR2XYZ_image;
 cvtColor( image, BGR2XYZ_image, CV_BGR2XYZ );

 namedWindow( "original image", CV_WINDOW_AUTOSIZE );
imshow( "original image", image );

namedWindow( "RGB2GRAY image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2GRAY image",RGB2GRAY_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2GRAY.jpg", RGB2GRAY_image );

namedWindow( "BGR2GRAY image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2GRAY image", BGR2GRAY_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2GRAY.jpg", BGR2GRAY_image );

namedWindow( "RGB2YCrCb image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2YCrCb image", RGB2YCrCb_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2YCrCb.jpg", RGB2YCrCb_image );


namedWindow( "BGR2YCrCb image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2YCrCb image", BGR2YCrCb_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2YCrCb.jpg", BGR2YCrCb_image );

namedWindow( "RGB2HSV image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2HSV image", RGB2HSV_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2HSV.jpg", RGB2HSV_image );

namedWindow( "BGR2HSV image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2HSV image", BGR2HSV_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2HSV.jpg", BGR2HSV_image );

namedWindow( "RGB2Lab image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2Lab image", RGB2Lab_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2Lab.jpg", RGB2Lab_image );

namedWindow( "BGR2Lab image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2Lab image", BGR2Lab_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2Lab.jpg", BGR2Lab_image );

namedWindow( "RGB2Luv image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2Luv image", RGB2Luv_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2Luv.jpg", RGB2Luv_image );

namedWindow( "BGR2Luv image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2Luv image", BGR2Luv_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2Luv.jpg", BGR2Luv_image );

namedWindow( "RGB2XYZ image", CV_WINDOW_AUTOSIZE );
imshow( "RGB2XYZ image", RGB2XYZ_image );
imwrite( "C:\\Users\\arjun\\Desktop\\RGB2XYZ.jpg", RGB2XYZ_image );

namedWindow( "BGR2XYZ image", CV_WINDOW_AUTOSIZE );
imshow( "BGR2XYZ image", BGR2XYZ_image );
imwrite( "C:\\Users\\arjun\\Desktop\\BGR2XYZ.jpg", BGR2XYZ_image );

 waitKey(0);

 return 0;
}

Original Image:
opencv original image

RGB2GRAY:
opencv RGB2GRAY


BGR2GRAY:
opencv BGR2GRAY

RGB2YCrCb:
opencv RGB2YCrCb


BGR2YCrCb:
opencv BGR2YCrCb


RGB2HSV:
opencv RGB2HSV


BGR2HSV:
opencv BGR2HSV


RGB2Lab:
opencv RGB2Lab


BGR2Lab:
opencv BGR2Lab


RGB2Luv:
opencv RGB2Luv


BGR2Luv:
opencv BGR2Luv


RGB2XYZ:
opencv RGB2XYZ


BGR2XYZ:
opencv BGR2XYZ

Here is the link of the opencv code:


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.

Transformation
Syntax
BayerRG Description: \rightarrow RGB
CV_BayerRG2RGB
BayerRG Description: \rightarrow BGR
CV_BayerRG2BGR
BayerGB Description: \rightarrow RGB
CV_BayerGB2RGB
BayerGB Description: \rightarrow BGR
CV_BayerGB2BGR
BayerGR Description: \rightarrow RGB
CV_BayerGR2RGB
BayerGR Description: \rightarrow BGR
CV_BayerGR2BGR
BayerBG Description: \rightarrow RGB
CV_BayerBG2RGB
BayerBG Description: \rightarrow BGR
CV_BayerBG2BGR



No comments:

Post a Comment