#include <cv.h>
#include <highgui.h>
using namespace cv;
int main(int argc, char** argv)
{
char* imageName = argv[1];
Mat image;
image = imread(imageName, 1);
if (argc != 2 || !image.data)
{
printf(" No image data \n ");
return -1;
}
Mat gray_image;
cvtColor(image, gray_image, CV_BGR2GRAY);
imwrite("../../images/Gray_Image.jpg", gray_image);
namedWindow(imageName, CV_WINDOW_AUTOSIZE);
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
imshow(imageName, image);
imshow("Gray image", gray_image);
waitKey(0);
return 0;
}
Explanation1. We begin by loading an image using imread, located in the path given by imageName. For this example, assume
you are loading a RGB image.
do this kind of transformations:
As you can see, cvtColor takes as arguments:
• a source image ( image )
• a destination image ( gray_image ), in which we will save the converted image.
• an additional parameter that indicates what kind of transformation will be performed. In this case we use
CV_BGR2GRAY (because of imread has BGR default channel order in case of color images).
ends). To save it, we will use a function analagous to imread: imwrite
imwrite( "../../images/Gray_Image.jpg" , gray_image );
Which will save our gray_image as Gray_Image.jpg in the folder images located two levels up of my current
location.
the new one:
namedWindow( imageName, CV_WINDOW_AUTOSIZE );
namedWindow( "Gray image" , CV_WINDOW_AUTOSIZE );
imshow( imageName, image );
imshow( "Gray image", gray_image );