OpenCV is a library package that is commonly used for Image Processing in C/C++. Here I have mentioned the installation process of OpenCV in Ubuntu. Just altering the installation commands can make it work for other Linux flavours.
Step-1:
Prerequisites for Installations of OpenCV:
1. A C++ compiler like g++.
You can install it by using the command:
$ sudo apt-get install g++
2. GTK+ 2.0
It is a graphical user interface library. Install it by:
$ sudo apt-get install libgtk2.0-dev
Step-2:
Now we can install the OpenCV packages.
You can use the Package Manager or can use the commands from terminal to install the packages.
OpenCV consists of:
1. libcv-dev
2.libcvaux-dev
3.libhighgui-dev
4.opencv-doc
Search in the Package Manager or Use the Command :
$ sudo apt-get install <package-name>
Now the Installation is complete.
Step-3:
Let us write a small program to test it.
////////////////////////////////////////////////////////////////////////
//
// hello-world.cpp
//
// This is a simple, introductory OpenCV program. The program reads an
// image from a file, inverts it, and displays 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;
int height,width,step,channels;
uchar *data;
int i,j,k;
if(argc<2){
printf("Usage: main <image-file-name>\n\7");
exit(0);
}
// load an image
img=cvLoadImage(argv[1]);
if(!img){
printf("Could not load image file: %s\n",argv[1]);
exit(0);
}
// 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("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
// 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];
// show the image
cvShowImage("mainWin", img );
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img );
return 0;
}
Step-4:
Now let us compile it.
We use the command:$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello-world.cppStep-5:Execute it:$ ./a.out <image-name>Voila!!!! Here runs your first IP program.Step-6:But every time you have to compile you have to write a long command. So, we can have an alias defined for it.Open the file /etc/bash.bashrc in super-user mode.You can use:$ sudo gedit /etc/bash.bashrcNow append the following line to the file to have an alias named "ipt".alias ipt="$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm"Close the terminal and open it to make the changes effective.Now to compile you can use:$ ipt hello-world.cppThats all with the installation. In my upcoming posts, I will focus on basic programming concepts.
