Linux · 2012-06-17

Install OpenCV 2.4 on Ubuntu 12.04

If you’re going to process live streams from cameras or videos, the most important library needed for OpenCV is ffmpeg. You can find a good tutorial on how to compile and install ffmpeg on Ubuntu here.
You need to follow those steps, the only difference necessary is to add

--enable-shared --enable-pic

to all configurations. For example this is the configuration needed for ffmpeg:

./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb 
    --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx 
    --enable-nonfree --enable-version3 --enable-x11grab --enable-shared --enable-pic

If you forget to do so it’s likely you get this error when compiling ffmpeg:

undefined reference to `x264_encoder_open_124'

Once ffmpeg is installed you’re ready to compile OpenCV. There is a short introduction in the official documentation on how to compile and install OpenCV.

Installation in Linux

These steps have been tested for Ubuntu 10.04 but should work with other distros.

Required packages

  • GCC 4.4.x or later. This can be installed with
    sudo apt-get install build-essential
  • CMake 2.6 or higher
  • Subversion (SVN) client
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkgconfig
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev.

All the libraries above can be installed via Terminal or by using Synaptic Manager

Getting OpenCV source code

You can use the latest stable OpenCV version available in sourceforge or you can grab the latest snapshot from the SVN repository.

Getting the latest stable OpenCV version

Getting the cutting-edge OpenCV from SourceForge SVN repository

Launch SVN client and checkout either

  1. the current OpenCV snapshot from here: http://code.opencv.org/svn/opencv/trunk
  2. or the latest tested OpenCV snapshot from here: http://code.opencv.org/svn/opencv/tags/latest_tested_snapshot

In Ubuntu it can be done using the following command, e.g.:

cd ~/<my_working _directory>
svn co http://code.opencv.org/svn/opencv/trunk

Building OpenCV from source using CMake, using the command line

  1. Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated Makefiles, project files as well the object filees and output binaries
  2. Enter the <cmake_binary_dir> and type
    cmake [<some optional parameters>] <path to the OpenCV source directory>

    For example

    cd ~/opencv
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
  3. Enter the created temporary directory (<cmake_binary_dir>) and proceed with:
    make
    sudo make install

Following those steps should be very easy. After running cmake a summary is printed which is important and you should pay attention to. This is the configuration summary on my system:

-- General configuration for OpenCV 2.4.0 =====================================
-- Version control:                 svn:8310M
-- 
--   Platform:
--     Host:                        Linux 3.2.0-24-generic x86_64
--     CMake:                       2.8.7
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++ (ver 4.6.3)
--     C++ flags (Release):         -Wall -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -O3 -DNDEBUG   -DNDEBUG
--     C++ flags (Debug):           -Wall -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG -ggdb3
--     C Compiler:                  /usr/bin/gcc
--     C flags (Release):           -Wall -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -Wall -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG -ggdb3
--     Linker flags (Release):      
--     Linker flags (Debug):        
-- 
--   OpenCV modules:
--     To be built:                 calib3d contrib core features2d flann gpu highgui imgproc legacy ml nonfree objdetect photo python stitching ts video videostab
--     Disabled by user:            -
--     Disabled by dependency:      -
--     Unavailable:                 androidcamera java
-- 
--   GUI: 
--     QT 4.x:                      YES (ver 4.8.1 EDITION = OpenSource)
--     QT OpenGL support:           YES (/usr/lib/x86_64-linux-gnu/libQtOpenGL.so)
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/libSM.so /usr/lib/x86_64-linux-gnu/libICE.so /usr/lib/x86_64-linux-gnu/libX11.so /usr/lib/x86_64-linux-gnu/libXext.so)
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.3.4)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.46)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
--     OpenEXR:                     /usr/lib/libImath.so /usr/lib/libIlmImf.so /usr/lib/libIex.so /usr/lib/libHalf.so /usr/lib/libIlmThread.so (ver 1.6.1)
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
-- 
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  YES (ver 2.2.0)
--     GStreamer:                   
--       base:                      YES (ver 0.10.36)
--       app:                       YES (ver 0.10.36)
--       video:                     YES (ver 0.10.36)
--     UniCap:                      NO
--     UniCap ucil:                 NO
--     PvAPI:                       NO
--     V4L/V4L2:                    Using libv4l (ver 0.8.5)
--     Xine:                        NO
--     FFMPEG:                      YES
--       codec:                     YES (ver 53.7.0)
--       format:                    YES (ver 53.4.0)
--       util:                      YES (ver 51.9.1)
--       swscale:                   YES (ver 2.0.0)
--       gentoo-style:              YES
-- 
--   Other third-party libraries:
--     Use IPP:                     NO
--     Use TBB:                     YES (ver 4.0 interface 6000)
--     Use Cuda:                    NO
--     Use Eigen:                   YES (ver 2.0.17)
--     Use Clp:                     NO
-- 
--   Python:
--     Interpreter:                 /usr/bin/python (ver 2.7.3)
--     Libraries:                   /usr/lib/libpython2.7.so
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.6.1)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Documentation:
--     Build Documentation:         YES
--     Sphinx:                      /usr/local/bin/sphinx-build (ver 1.1.3)
--     PdfLaTeX compiler:           /usr/bin/pdflatex
-- 
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     Examples:                    NO
-- 
--   Install path:                  /usr/local
-- 
--   cvconfig.h is in:              /home/shambool/code/libs/opencv_svn/build
-- -----------------------------------------------------------------

It’s a good idea to install Intel TBB if you don’t have it already, to improve the speed of some methods. As you can see in the summary my system doesn’t support CUDA and hence OpenCV couldn’t find the libraries. There are two ways to change these parameters. One is to pass the value of the parameters to cmake, and the second is to edit

opencv/CMakeLists.txt

and change the value of the parameters. I prefer the former, and have enabled the support for OpenGL and QT.

Update: there is an installation guide here that you may find useful, particularly if you don’t know what libraries are needed for OpenCV. And you can find the official documentation [url=”http://docs.opencv.org/”here[/url].

 

 

Installing OpenCV 2.2 in Ubuntu 11.04

Many people have used my previous tutorial about installing OpenCV 2.1 in Ubuntu 9.10. In the comments of that post, I noticed great interest for using OpenCV with Python and the Intel Threading Building Blocks (TBB). Since new versions of OpenCV and Ubuntu are available, I decided to create a new post with detailed instructions for installing the latest version of OpenCV, 2.2, in the latest version of Ubuntu, 11.04, with Python and TBB support.

First, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, etc… This step is very easy, you only need to write the following command in the Terminal

1 sudo apt-get install build-essential libgtk2.0-dev libjpeg62-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev

Now we need to get and compile the ffmpeg source code so that video files work properly with OpenCV. This section is partially based on the method discussed here.

1 cd ~
3 tar -xvzf ffmpeg-0.7-rc1.tar.gz
4 cd ffmpeg-0.7-rc1
5 ./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libxvid --enable-x11grab --enable-swscale --enable-shared
6 make
7 sudo make install

The next step is to get the OpenCV 2.2 code:

1 cd ~
3 tar -xvf OpenCV-2.2.0.tar.bz2
4 cd OpenCV-2.2.0/

Now we have to generate the Makefile by using cmake. In here we can define which parts of OpenCV we want to compile. Since we want to use Python and TBB with OpenCV, here is where we set that. Just execute the following line at the console to create the appropriate Makefile. Note that there is a dot at the end of the line, it is an argument for the cmake program and it means current directory.

1 cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON .

Check that the above command produces no error and that in particular it reports FFMPEG as 1. If this is not the case you will not be able to read or write videos. Also, check that Python reports ON and Python numpy reports YES. Also, check that under Use TBB it says YES. If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again. You should see something similar to this:

Now, you are ready to compile and install OpenCV 2.2:

1 make
2 sudo make install

Now you have to configure OpenCV. First, open the opencv.conf file with the following code:

1 sudo gedit /etc/ld.so.conf.d/opencv.conf

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:

1 /usr/local/lib

Run the following code to configure the library:

1 sudo ldconfig

Now you have to open another file:

1 sudo gedit /etc/bash.bashrc

Add these two lines at the end of the file and save it:

1 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
2 export PKG_CONFIG_PATH

Finally, close the console and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

There is a final step to configure Python with OpenCV. You need to copy the file cv.so into the correct place. You can do that by just executing the following command:

1 sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so

Now you have OpenCV 2.2 installed in your computer with Python and TBB support.

Let’s check some demos included in OpenCV.
First, let’s see some C demos:

1 cd ~/OpenCV-2.2.0/samples/c
2 chmod +x build_all.sh
3 ./build_all.sh

Some of the training data for object detection is stored in /usr/local/share/opencv/haarcascades. You need to tell OpenCV which training data to use. I will use one of the frontal face detectors available. Let’s find a face:

1 ./facedetect --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"--scale=1.5 lena.jpg

Note the scale parameter. It allows you to increase or decrease the size of the smallest object found in the image (faces in this case). Smaller numbers allows OpenCV to find smaller faces, which may lead to increasing the number of false detections. Also, the computation time needed gets larger when searching for smaller objects.

You can also detect smaller objects that are inside larger ones. For example you can search for eyes inside any detected face. You can do that with the nested-cascade parameter:

1 ./facedetect --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/opencv/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg


Feel free to experiment with other features like mouth or nose for example using the corresponding cascades provided in the haarcascades directory.

Now let’s check some C++ demos:

1 cd ~/OpenCV-2.2.0/samples/cpp
2 make

Now all the C++ demos are built in ~/OpenCV-2.2.0/bin. Let’s see a couple of them. For example, a simulated chessboard calibration:

1 ~/OpenCV-2.2.0/bin/calibration_artificial


In OpenCV 2.2, the grabcut algorithm is provided as a C++ sample. This is a very nice segmentation algorithm that needs very little user input to segment the objects in the image. For using the demo, you need to select a rectangle of the area you want to segment. Then, hold the Control key and left click to select the background (in Blue). After that, hold the Shift key and left click to select the foreground (in Red). Then press the n key to generate the segmentation. You can press n again to continue to the next iteration of the algorithm.

1 ~/OpenCV-2.2.0/bin/grabcut ~/OpenCV-2.2.0/samples/cpp/lena.jpg

This image shows the initial rectangle for defining the object that I want to segment.

Now I roughly set the foreground (red) and background (blue).

When you are ready, press the n key to run the grabcut algorithm. This image shows the result of the first iteration of the algorithm.

Now let’s see some background subtraction from a video. The original video shows a hand moving in front of some trees. OpenCV allows you to separate the foreground (hand) from the background (trees).

1 ~/OpenCV-2.2.0/bin/bgfg_segm ~/OpenCV-2.2.0/samples/c/tree.avi

And finally, let’s see Python working with OpenCV:

1 cd ~/OpenCV-2.2.0/samples/python/

Let’s run the kmeans.py example. This script starts with randomly generated 2D points and then uses a clustering method called k-means. Each cluster is presented in a different color.

1 python kmeans.py

Now let’s see the convexhull.py demo. This algorithm basically calculates the smallest convex polygon that encompasses the data points.

1 python convexhull.py

Python scripts can also be executed directly like the following example. This script reads a video file (../c/tree.avi) within pyhton and shows the first frame on screen:

1 ./minidemo.py


Have fun with OpenCV in C, C++ or Python…