OpenCV (Open Source Computer Vision) is a library of programming functions for real time computer vision. It is written in C/C++ and is used worlwide in many computer vision applications.
This "how to" is a mix of two other great tutorials on OpenCV installations: A Compreehensive Guide to Installing and Configuring OpenCV 2.3.1 on Ubuntu from ozbots.org; and the OpenCV Installation Guide on Debian and Ubuntu from the OpenCV Wiki. I used this procedure to successfully install OpenCV on two amd64 computers (a laptop and a desktop) running Ubuntu 11.10 Oneiric Ocelot. However, if you find any problem you can check the Comprehensive OpenCV Installation Troubleshooting Guide from ozbots.
1. Remove any installed versions of ffmpeg and x264
sudo apt-get remove ffmpeg x264 libx264-dev
2. Get all the dependencies
sudo apt-get update sudo apt-get install build-essential checkinstall git pkg-config cmake libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev libpngwriter0-dev libpngwriter0c2 libfaac-dev libjack-jackd2-dev libjasper-dev libjasper-runtime libjasper1 libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev zlib1g-dbg zlib1g libgstreamer0.10-0 libgstreamer0.10-dev libgstreamer0.10-0-dbg gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog libavcodec-dev libavcodec52 libavformat52 libavformat-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig python-numpy libpython2.7 python-dev python2.7-dev libjpeg-progs libjpeg-dev libgtk2.0-0 libgtk2.0-dev gtk2-engines-pixbuf
3. Download and install x264 - download any recent stable snapshot from ftp://ftp.videolan.org/pub/videolan/x264/snapshots/ (I used x264-snapshot-20111012-2245-stable.tar.bz2) and extract it to a folder. Then enter the folder and configure and build it:
./configure --enable-static make sudo make install
NOTE: In my computers (64-bit) I had to add the flags "--enable-pic --enable-shared" to the configuration. Otherwise I would get compilation errors when building OpenCV. If you get them too, run
sudo make clean
and reconfigure and rebuild using
./configure --enable-static --enable-pic --enable-shared make sudo make install
4. Download and install ffmpeg - download any recent stable snapshot from http://ffmpeg.org/download.html (I used ffmpeg-0.8.5.tar.gz) and extract it to a folder. Then enter the folder and configure and build it:
NOTE: According to blog reader custom6109 some of the headers in ffmpeg changed and opencv does not recognize the deprecations (03/16/2012). Installing with ffmpeg0.10 should work. (Thanks custom6109!)
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab make sudo make install
NOTE: Once again, in my computers (64-bit) I had to add the flags "--enable-pic --enable-shared" to the configuration. Otherwise I would get compilation errors when building OpenCV. If you get them too, run
sudo make clean
and reconfigure and rebuild using
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-pic --enable-shared make sudo make install
5. Download and install v4l - download any recent stable snapshot from http://www.linuxtv.org/downloads/v4l-utils/ (I used v4l-utils-0.8.5.tar.bz2) and extract it to a folder. Then enter the folder and build it:
make sudo make install
6. Download and install OpenCV - download OpenCV 2.3.1 from http://sourceforge.net/projects/opencvlibrary/files/ and extract it to a folder. Then enter the folder and build it:
mkdir release cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON .. make sudo make install
7. Configure Linux - add the following line to your .bashrc in order to tell linux where the shared libraries for OpenCV are located without having to enter it everytime you start a new terminal:
export LD_LIBRARY_PATH=/usr/local/libusing any text editor add the following lines to the end of your /etc/bash.bashrc file
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
That's it! If you perform all the steps indicated you should have OpenCV installed in your system. You can check the samples in <your opencv folder>/samples/ . To compile all the C/C++ samples at once you can enter the <your opencv folder>/samples/c folder and give the build_all.sh script permissions to be executed
chmod +x build_all.sh
Then just run
and all the samples will be compiled.
To make python work run
python import cv2
If no error occurs, then the python wrappers are successfully installed.
To compile code that uses OpenCV libraries use
g++ `pkg-config --cflags opencv` my_code.cpp -o my_code `pkg-config --libs opencv
If you find any problems in this tutorial post them in the comments. I can't promise that I'll find the solution, since I'm not an expert, but I'll try.
Update (24/10/11): I placed an entry on this blog on How to use CMake with OpenCV to build projects that use the OpenCV libraries.Check it out if you want
Update (29/01/12): As suggested by blog reader Cactus (thanks for your contribution :)), I've added the packages "libopencore-amrnb-dev" and "gtk2-engines-pixbuf" to step 2.
Later I'll post some basic projects using OpenCV.
Did you find this post helpful? Do you wish to contribute to other projects regarding computer science, electronics, robotics or mechatronics that will be posted in this blog? If you wish to do so, you can donate via paypal using the button below. Thanks! :)