0% found this document useful (0 votes)
17 views1 page

Load, Modify, and Save An Image: Goals

This document provides code to load an image using imread, convert it to grayscale using cvtColor, and save the grayscale image using imwrite. The code loads an image from the command line argument, converts it to grayscale, displays both the original and grayscale images, and saves the grayscale image to a file.

Uploaded by

Anonymous xRvGMB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Load, Modify, and Save An Image: Goals

This document provides code to load an image using imread, convert it to grayscale using cvtColor, and save the grayscale image using imwrite. The code loads an image from the command line argument, converts it to grayscale, displays both the original and grayscale images, and saves the grayscale image to a file.

Uploaded by

Anonymous xRvGMB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Load, Modify, and Save an Image

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;
}

You might also like