Load, Modify, and Save An Image: Goals
Load, Modify, and Save An Image: Goals
Note: Weassumethatbynowyouknowhowtoloadanimageusingimreadandto
displayitinawindow(usingimshow).ReadtheLoadandDisplayanImagetutorial
otherwise.
Goals
Inthistutorialyouwilllearnhowto:
Loadanimageusingimread
TransformanimagefromBGRtoGrayscaleformatbyusingcvtColor
Saveyourtransformedimageinafileondisk(usingimwrite)
Code
Hereitis:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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;
}