[OpenCV] Installation (with python support)
We have 2 ways in order to install OpenCV on ours Ubuntu machine.
Here I explain both of them:
Then we can check if the python wrapper is properly installed.
Prerequisites
- C++ compiler like g++ (sudo apt-get install g++)
- GTK+ 2.0 or higher (sudo apt-get install libgtk2.0-dev)
Procedure
- Open a terminal and type “sudo apt-get install” for all these packages
libcv2.1 libcv-dev libcvaux2.1 libcvaux-dev libhighgui2.1 libhighgui-dev opencv-doc python-opencv
- Then, always in a terminal, type:
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.
- To check the path where opencv & other lib files are stored (needed to compile your opencv programs) type (and check the output):
$ pkg-config --cflags opencv -I/usr/include/opencv $ pkg-config --libs opencv -lcxcore -lcv -lhighgui -lcvaux -lml - Create a test file “hello.c” and type this code in it:
#include <stdio.h> #include "cv.h" #include "highgui.h" int main( int argc, char** argv ) { /* data structure for the image */ IplImage *img = 0; /* check for supplied argument */ if( argc < 2 ) { fprintf( stderr, "Usage: loadimg <filename>\n" ); return 1; } /* load the image, use CV_LOAD_IMAGE_GRAYSCALE to load the image in grayscale */ img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR ); /* always check */ if( img == 0 ) { fprintf( stderr, "Cannot load file %s!\n", argv[1] ); return 1; } /* create a window */ cvNamedWindow( "image", CV_WINDOW_AUTOSIZE ); /* display the image */ cvShowImage( "image", img ); /* wait until user press a key */ cvWaitKey(0); /* free memory */ cvDestroyWindow( "image" ); cvReleaseImage( &img ); return 0; } - Compile it
g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.c -o hello
- Then run it
./hello img.jpg
where “img” is the name of any image within the same folder of the C script.
If this not run, tryexport PATH=$HOME/usr/bin/:$PATH
and go to step2 again
- In order to simplify the command used for compile the script, we can create a shortcut
- go to your local home directory:
cd /home/
- open the .bashrc file (it will be hidden):
vi .bashrc
- append this line:
alias gcv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm"
- save and clode the file, then close the terminal and open it again
- go to your local home directory:
- Now go to directory containing a sample program and type:
gcv hello.c -o hello ./hello img.jpg
Further prerequisite: CMake 2.6 or later
Procedure
- Get the latest stable OpenCV version downloading the source tarball (OpenCV-x.y.z.tar.bz2)
- Create a folder named “opencv” (for example in the folder “/projects”) and extract in it the content of the tarball
- Then open a terminal and type:
cd ~/projects/opencv # the directory containing INSTALL, CMakeLists.txt etc. mkdir release cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
- Then, build the library and install it to the target directory:
make sudo make install
If you get some library errors, like the following:
./hello: error while loading shared libraries: libcv.so.2.1: wrong ELF class: ELFCLASS64
you can solve the problem typing:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH sudo ldconfig
and then recompiling the script in this way:
g++ hello.c -o hello `pkg-config --cflags --libs opencv`
In order to check if the python wrapper is properly installed create a new file named “hello_python.py”
import cv im = cv.LoadImage( "sample_image.jpg" ) cv.ShowImage( "sample1", im ) cv.WaitKey()
Then open a terminal and type:
python hello_python.py




No comments yet.