This article is basically for installing Opencv 2.1 in Ubuntu 10.10. Opencv basically helps in image processing using c++ . For reading or writing images in c++ , we require Opencv. The steps below are the ways Opencv can be installed.
1. Go to Synaptic Package Manager (System> Administration> Synaptic Package Manager).
2. Search for “opencv” and install the main “opencv” package and the following lib files:
(you can also install opencv directly from the terminal by “sudo apt-get install” the above lib files
3. After installing all the packages, type this code:
The above ones are default paths for the opencv libraries.
4. To check the path where opencv & other lib files are stored, do:
This output shows the path of the header file as:
This output shows the libraries required to be included.
An Example code:
This code reads an image an displays it.It is named as img.cc
These paths are needed to compile your opencv programs as shown below:
5. To compile & run:
where img.cc is c++ code for reading an image and displaying it.
6. For simplyfying the above command create an alias of the command in the home directory as
Following steps 5 and 6 we get the output as shown below.
1. Go to Synaptic Package Manager (System> Administration> Synaptic Package Manager).
2. Search for “opencv” and install the main “opencv” package and the following lib files:
libcv
libcv-dev
libcvaux
libcvaux-dev
libhighgui
libhighgui-dev
opencv-doc
(‘python-opencv’ not required).(you can also install opencv directly from the terminal by “sudo apt-get install” the above lib files
3. After installing all the packages, type this code:
$ export LD_LIBRARY_PATH=/home/opencv/lib
$ export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig
The above ones are default paths for the opencv libraries.
4. To check the path where opencv & other lib files are stored, do:
$ pkg-config --cflags opencv
This output shows the path of the header file as:
-I/usr/include/opencv
$ pkg-config --libs opencv
This output shows the libraries required to be included.
-lcxcore -lcv -lhighgui -lcvaux -lml
An Example code:
This code reads an image an displays it.It is named as img.cc
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include </usr/include/opencv/cv.h>
#include </usr/include/opencv/highgui.h>
using namespace std;
int main(int argc, char *argv[])
{
IplImage* img = 0;
IplImage* img1=cvCreateImage(cvSize(256,256),IPL_DEPTH_8U,1);
int height,width,step,channels;
unsigned char *data;
int i,j,k;
if(argc<2){
cout<<"Usage: main <image-file-name>";
exit(0);
}
// load an image
img=cvLoadImage(argv[1],-1); //-1 is a flag (flag<0 the loaded image is loaded as is (with number of channels in the file)
if(!img){
cout<<"Could not load image file:"<<argv[1]<<endl;
exit(0);
}
// get the image data
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (unsigned char *)img->imageData;
cout<<"Height - "<<height<<endl;
cout<<"Width - "<<width<<endl;
cout<<"WidthStep - "<<step<<endl;
cout<<"Channels - "<<channels<<endl;
cout<<endl;
cvNamedWindow("Read Image", CV_WINDOW_AUTOSIZE);
// creating 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]=data[i*step+j*channels+k];
img1->imageData=(char*)data;
// show the image
cvShowImage("Read Image", img1 );
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img );
return 0;
}
These paths are needed to compile your opencv programs as shown below:
5. To compile & run:
$ g++ -I/usr/include/opencv -lcxcore -lhighgui -lm img.cc
where img.cc is c++ code for reading an image and displaying it.
$ ./a.out lena.bmp
6. For simplyfying the above command create an alias of the command in the home directory as
$ alias ocv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm"
$ ocv img.cc
$./a.out lena.bmp
Following steps 5 and 6 we get the output as shown below.
Reading an image in C++ using Opencv |
No comments:
Post a Comment