---------------------------------------------------------------------------------------------------------------
Description::
This is a C++ program to show the use of cvtColor funtion which is inbuilt in the opencv library. cvtColor takes 3 parametres. FIrst is mat object for image, second is mat object of another desired color and last parameter is keyname for conversion from one color to another color
Example:
1)COLOR_RGB2GRAY ::convert image from RGB to grey
2)COLOR_RGB2HSV:: convert image from RGB to HSV
3)COLOR_RGB2Lab :: convert image from RGB to Lab
---------------------------------------------------------------------------------------------------------------
Program::
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
char* ImageFile = argv[1]; //image file
Mat image;
image = imread( ImageFile, IMREAD_COLOR ); //reading file
if( argc != 2 || !image.data )
{
printf( " No image data \n " );
return -1;
}
Mat gray_image;
Mat HSV_image; //Mat object for storing data
Mat Lab_image;
cvtColor( image, gray_image, COLOR_RGB2GRAY ); //convert image from RGB to grey
imwrite( "/home/crmgogo/Pictures/greyImage.png", gray_image );
namedWindow( ImageFile, WINDOW_AUTOSIZE );
namedWindow( "Gray image", WINDOW_AUTOSIZE );
imshow( ImageFile, image );
imshow( "Gray image", gray_image );
cvtColor( image, HSV_image, COLOR_RGB2HSV ); //convert image from RGB to HSV
imwrite( "/home/crmgogo/Pictures/HSVImage.png", HSV_image );
namedWindow( "HSV image", WINDOW_AUTOSIZE );
imshow( "HSV image", HSV_image );
cvtColor( image, Lab_image, COLOR_RGB2Lab ); //convert image from RGB to Lab
imwrite( "/home/crmgogo/Pictures/LAbImage.png", Lab_image );
namedWindow( "Lab image", WINDOW_AUTOSIZE );
imshow( "Lab image", Lab_image );
waitKey(0);
return 0;
}
---------------------------------------------------------------------------------------------------------------
Commands to run program on terminal::
$ g++ -ggdb `pkg-config --cflags filename` -o `basename filename .cpp` filename `pkg-config --libs opencv`
$ ./cvopen filename
---------------------------------------------------------------------------------------------------------------
Output::
Comments