Load, Modify and Save an Image

Refrenced from OpenCV Tutorials, Release 2.4.3 P89-93

Load and Display an Image

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <iostream>

using namespace cv;

using namespace std;

int main( int argc, char** argv )

{

if( argc != 2)

{

cout <<” Usage: display_image ImageToLoadAndDisplay” << endl;

return -1;

}

Mat image;

image = imread(argv[1], CV_LOAD_IMAGE_COLOR); if(! image.data )

// Read the file // Check for invalid input

{

cout << “Could not open or find the image” << std::endl ; return -1;

}

namedWindow( “Display window”, CV_WINDOW_AUTOSIZE );// Create a window for display.

imshow( “Display window”, image ); // Show our image inside it.

waitKey(0); // Wait for a keystroke in the window return 0;

}

Load, Modify, and Save an Image

#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_RGB2GRAY );

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;

}