Contents[Hide]

To covert color image to a gray scale has never been easier with OpenCV. Here is a small C++ program that does conversion from color image to a grayscale.

1. WRITE CODE

#include <iostream>
#include "cv.h"
#include "highgui.h"

using namespace std;

int main( int argc, char** argv )

{
//load color image specified by first argument
IplImage *source = cvLoadImage( argv[1]);

// create new image structure
// for the grayscale output image

IplImage *destination = cvCreateImage(
cvSize( source->width, source->height ), IPL_DEPTH_8U,
1 );



// set type CV_RGB2GRAY to convert
// RGB image to grayscale

cvCvtColor( source, destination, CV_RGB2GRAY );


// save grayscale image to a file specified by
// second argument

cvSaveImage( argv[2], destination );
return 0;

}

2. COMPILE

g++ `pkg-config opencv --cflags --libs` convert_grayscale.cpp -o convert_grayscale

3. USAGE

./convert_grayscale re_dsc00056.jpg gray_dsc00056.jpg

Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top