//////////////////////////////////////////////////////////////////
//
// trial1a.cpp
//
// This is a simple, introductory OpenCV Program. The program will
// read an image from a file, clone it, invert it, and display the
// result.
//
//////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
IplImage* img = 0;
IplImage* img1 = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;
if(argc<2)
{
printf("Usage: main
exit(0);
}
//load an image
img1=cvLoadImage(argv[1]);
if(!img1)
{
printf("Could not load image file: %s\n",argv[1]);
exit(0);
}
//clonning the image before processing
img=cvCloneImage(img1);
//get the image data
height=img->height;
width=img->width;
step=img->widthStep;
channels=img->nChannels;
data=(uchar *)img->imageData;
printf("Processing a %dx%d image with %d channels\n",height,width,channels);
//create a window
cvNamedWindow("Image Window",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Image Window",0,0);
//show the original image
cvShowImage("Image Window",img1);
//wait for input any key
cvWaitKey(0);
//invert the image
for(i=0;i<height;i++)
for(j=0;j<width;j++)
for(k=0;k<channels;k++)
*(data+(i*step+j*channels+k))=255-*(data+(i*step+j*channels+k));
//create another window
cvNamedWindow("Invert Image Window",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Invert Image Window",500,0);
//show the inverted image
cvShowImage("Invert Image Window",img);
//wait for input any key
cvWaitKey(0);
//release the image
cvReleaseImage(&img1);
cvReleaseImage(&img);
return 0;
}
After typing and saving it using Kate Editor, I compile using gcc with command (in the working directory):
$ g++ -Wall trial1a.cpp -o trial1a `pkg-config --cflags --libs opencv`
Note that the option `pkg-config --cflags --libs opencv` will inserts -I/usr/local/include/opencv -L/usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml automatically to the command. If that is not working, you can try this:
$ g++ -Wall trial1a.cpp -o trial1a -I/usr/local/include/opencv -L/usr/local/lib -lcxcore -lcv -lhighgui -lcvaux -lml
Next, if there are no errors, I type (in the working directory):
$ ./trial1a foto.jpg
Then ... (see screenshots)
References:
- OpenCV wiki (http://opencvlibrary.sourceforge.net)
- Introduction to programming with OpenCV, by Gady Agam (http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html)
No comments:
Post a Comment