---------------------------------------------------------------------------------------------------------------------
Description::
This is a C++ program to convert BGR image to greyscale image using cvtColor function present in the OpenCV library in c++. Greyscale images are required for any functions in OpenCV also they are useful in distinguishing in intensity between the pixels as extra inforation is not required to be submitted.
---------------------------------------------------------------------------------------------------------------------
Program::
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
char* ImageFile = argv[1]; //image file
Mat image; //mat object for storing data
image = imread( ImageFile, IMREAD_COLOR ); //read file
if( argc != 2 || !image.data )
{
printf( " No image data \n " );
return -1;
}
Mat gray_image;
cvtColor( image, gray_image, COLOR_BGR2GRAY ); //convert image from grey to color
imwrite( "/home/crmgogo/Pictures/greyImage.png", gray_image );
namedWindow( ImageFile, WINDOW_AUTOSIZE );
namedWindow( "Gray image", WINDOW_AUTOSIZE );
imshow( ImageFile, image ); //show window containing images
imshow( "Gray image", gray_image );
waitKey(0); //to exit
return 0;
}
---------------------------------------------------------------------------------------------------------------------
Commands to run program on terminal::
$ g++ -ggdb `pkg-config --cflags opencv` -o `basename cvopen .cpp` filename `pkg-config --libs opencv`
$ ./cvopen filename
---------------------------------------------------------------------------------------------------------------------
Output::
Comments