Skip to content


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


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.

cd ~
wget http://ffmpeg.org/releases/ffmpeg-0.7-rc1.tar.gz
tar -xvzf ffmpeg-0.7-rc1.tar.gz
cd ffmpeg-0.7-rc1
./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
make
sudo make install

The next step is to get the OpenCV 2.2 code:

cd ~
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.2/OpenCV-2.2.0.tar.bz2
tar -xvf OpenCV-2.2.0.tar.bz2
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.

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:

make
sudo make install

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

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:

/usr/local/lib

Run the following code to configure the library:

sudo ldconfig

Now you have to open another file:

sudo gedit /etc/bash.bashrc

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

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
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:

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:

cd ~/OpenCV-2.2.0/samples/c
chmod +x build_all.sh
./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:

./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:

./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:

cd ~/OpenCV-2.2.0/samples/cpp
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:

~/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.

~/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).

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

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

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.

python kmeans.py

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

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:

./minidemo.py


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

Posted in Image Processing, Programming.


88 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Vinod says

    Ran the camshiftdemo in the bin folder.
    The following message came:
    ———————-
    This is a demo that shows mean-shift based tracking
    You select a color objects such as your face and it tracks it.
    This reads from video camera (0 by default, or the camera number the user enters
    Usage:
    ./camshiftdemo [camera number]

    Hot keys:
    ESC – quit the program
    c – stop the tracking
    b – switch to/from backprojection view
    h – show/hide object histogram
    p – pause video
    To initialize tracking, select the object with mouse

    This is a demo that shows mean-shift based tracking
    You select a color objects such as your face and it tracks it.
    This reads from video camera (0 by default, or the camera number the user enters
    Usage:
    ./camshiftdemo [camera number]

    Hot keys:
    ESC – quit the program
    c – stop the tracking
    b – switch to/from backprojection view
    h – show/hide object histogram
    p – pause video
    To initialize tracking, select the object with mouse
    ***Could not initialize capturing…***
    Current parameter’s value:
    1 [ ] ( -1 – by default) – camera number
    ————————-
    Are you able to capture video from webcam.

  2. Vinod says

    To enable video capture instead of WITH_V4L=OFF , WITH_V4L=ON must be there in the cmake variable list.

  3. Adam Saltiel says

    This is the best tutorial I have found to build OpenCV on Linux.
    The latest two versions of opencv and ffmpeg prove to work for me.

    On an Intel dual core 2.8 with no Nvidia (on board graphics) I have found the wrong combination of ffmpeg and OpenCV results in errors on make here

    [ 15%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.o

    with output like this

    make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.o] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make: *** [all] Error 2

    The following works:-
    I have used
    ffmpeg-0.8.5 -
    wget http://ffmpeg.org/releases/ffmpeg-0.8.5.tar.gz
    tar -xvzf ffmpeg-0.8.5.tar.gz
    cd ffmpeg-0.8.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

    and OpenCV-2.3.1a -
    wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2

    To test some paths have to be adjusted, of course.
    e.g. in the following commands, obviously -
    cd ~/OpenCV-2.2.0/samples/c
    chmod +x build_all.sh
    ./build_all.sh

    I have also not been able to locate cv.so yet (I have cv2.so) but have not test python yet, so maybe OK without following command?
    sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so

    I have the following two directories -
    /usr/local/share/opencv/samples/c
    /usr/local/share/OpenCV/haarcascades

    So the path in the test below has been altered, again obviously.
    /opencv-magic/OpenCV-2.3.1/samples/c$ ./facedetect –cascade=”/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml” –scale=1.5 lena.jpg

  4. Adam Saltiel says

    Should say, this is a fresh install of Ubuntu 11.10 alpha
    I will come back after further tests.

  5. Adam Saltiel says

    I have run through all the python tests and all OK. Seems that the line
    sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so
    is not necessary with these latest builds.

  6. mahesh says

    hi i m maheshmhegade@gmail.com .
    i m new to opencv i want to read a perticular frame from a given .avi video (say i want to read a 210th frame of “fun.avi” file) how can i read and retrive frame using cvquery function.or using if any fuction
    thank you

  7. samontab says

    There are some functions in highgui to deal with that but they don’t work for me at least…
    The easiest workaround that I have found to solve your problem is to query a frame 210 times, and return that image.
    If that is too slow for you, you can also save the individual frames as images, for example video001_frame00001.jpg, video001_frame00002.jpg, etc. That way, you can access any frame at any moment.

    highgui is only for simple video usage (mainly for playing and recording videos), if you want to do more advanced video editing, you could use ffmpeg, mencoder, mplayer, etc. Or even post processing a video with avidemux for example…

  8. Tj.Wallas_ says

    Thank you so much!
    Very useful tutorial.

  9. Andrew says

    Thanks for the great tutorial. It worked well. I’ve recently reinstalled the Ubuntu and went with the newest version (11.10). While trying to build OpenCV 2.3.1, I get the message during linking:

    Linking CXX shared library ../../lib/libopencv_highgui.so
    /usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet’ can not be used when making a shared object; recompile with -fPIC
    /usr/local/lib/libavcodec.a: could not read symbols: Bad value
    collect2: ld returned 1 exit status
    make[2]: *** [lib/libopencv_highgui.so.2.3.1] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make: *** [all] Error 2

    Do you have any tips to resolve it? I’m on x64 Intel and x64 Ubuntu, if that makes a difference.

  10. Jeremy says

    Thank you so much! I went through so much pain and anguish trying to get openCV on my machine. This tutorial helped a bunch!

  11. NewtoOpencv says

    HELP.!
    I got error for proceed until the step
    “Now, you are ready to compile and install OpenCV 2.2:
    make
    sudo make install

    I follow all the step above and works very well. But there are error after i type “make”.

    /usr/include/c++/4.6/typeinfo:42:37: error: expected ‘}’ before end of line
    /usr/include/c++/4.6/typeinfo:42:37: error: expected declaration before end of line
    make[2]: *** [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.o] Error 1
    make[1]: *** [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/all] Error 2
    make: *** [all] Error 2

    How can i fix it? Thanks!

  12. samontab says

    Hard to tell… it could be many things…

    I recommend you to re install the OS and start from scratch following every line of the tutorial. It is the easiest way to make it work without a problem.

  13. NewtoOpencv says

    I tried several times…

    I’m using ubuntu 11.10. It is cause the problem?

    After i type make the output is

    [ 2%] Building CXX object modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.o
    In file included from /home/gp2/OpenCV-2.2.0/modules/core/src/precomp.hpp:55:0,
    from /home/gp2/OpenCV-2.2.0/modules/core/opencv_core_pch_dephelp.cxx:1:
    /home/gp2/OpenCV-2.2.0/modules/core/include/opencv2/core/core.hpp:354:13: error: ‘ptrdiff_t’ does not name a type

    Thanks for help again!

  14. samontab says

    It may be the problem.

    If you use another OS, the libraries and programs that are installed may be different. They may have different dependencies, or even work differently.
    In general, if you use an OS with small differences, like 11.04 and 11.10, it should be OK to follow the same tutorial but with small changes. The problem in that scenario is that you need to know what you are doing, and not follow the steps blindly.
    If you just need to get it working ASAP, install 11.04 from scratch and follow this tutorial.

  15. MartinSt says

    Hi to all.
    @NewtoOpencv
    please let yourself visit this link:
    https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/791527

    if I’d understood, we discuss about problems with newer g++ version?

  16. ton says

    this is a great tutorial … and very much thankfull for your knowledge sharing .. keep it up buddy.

  17. andol says

    i compiled the opencv using this tutorial, very useful
    but is there any update for newer versions of opencv?

  18. samontab says

    You may need to change some bits here and there, but if you know what you are doing, you should be able to install any newer openCV version based on this tutorial.

  19. Samarth says

    hi,
    great blog! I read in the ‘about’ section that you also work on IP cameras. Could you guide me how to capture a MJPEG stream from an IP camera to OpenCV?
    Thanks!

  20. samontab says

    Hello,

    I usually create a function, like getFrame(), that returns an OpenCV image and deals with everything I need to do to get an image from the connected camera. This can be just OpenCV highgui functions for webcams or more specific code for IP cameras. Then, I just use that function on the main loop of the program.
    That way, I can easily change the camera to another one and the program still works the same.

  21. Jigar says

    Hey thank you for your great help regarding installation of OpenCv 2.2 on ubuntu 11.04

  22. rupesh says

    how to install and make it working for c and c++ not for python….

  23. samontab says

    The post explains how to install the library for those three languages. Once you install it you can use it with C, C++ and Python.

  24. abyss says

    Hi, this is a extremely prefect tutorial I never saw before.But I have a small problem with the “TBB”. I use the function “cmake -D WITH_TBB=ON .”, but the TBB still with ‘no’ state. Is that my 11.04 ubuntu caused by this?.

  25. samontab says

    Make sure that you have correctly installed the TBB development library before you compile opencv

1 2

Continuing the Discussion

  1. Installing OpenCV 2.1 in Ubuntu – Sebastian Montabone linked to this post on June 17, 2011

    [...] UPDATE: Install OpenCV 2.2 in Ubuntu 11.04 with Python and TBB support here. [...]

  2. Tutorial para configurar a biblioteca OpenCV no compilador CodeBlocks Linux – Ubuntu 10.10 « Guilherme P. Cardim linked to this post on June 27, 2011

    [...] http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/ Publicado em Tudo, Tutoriais. Deixar um comentário » LikeBe the first to like this post. [...]

  3. OpenCV 2.2 with Natty , make problems | Coders & Admins linked to this post on July 8, 2011

    [...] I’m new with Linux and Ubuntu .I don’t if this is the correct section for this thread. I need some help with making OpenCV 2.2 , I’m using this guide to install it : http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/ [...]

  4. Playing video with OpenCV 2.2, Python, and wxWidgets « Brett Walenz linked to this post on August 12, 2011

    [...] For installing these things on Ubuntu 11.04, you can follow the guide. [...]

  5. OpenCV on Ubuntu 11.04 | 累積の奧義 linked to this post on August 20, 2011

    [...] samples 目錄裡的程式了。 要如何測試,請看下面這2篇,也是很NICE的文章: http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/ [...]

  6. voidnoise.co.uk » Ubuntu 11.04 & OpenCV 2.2 linked to this post on August 26, 2011

    [...] for opencv 2.2 to install hassle free. Sabastian Monabone explains the process better than I could here. Written by voidnoise in: Uncategorized | Tags: OpenCV, [...]

  7. Installing OpenCV 2.2 (and above?) in Ubuntu 11.04 x86-64 « Mr. Barry's Tech Blog linked to this post on August 29, 2011

    [...] guide, but it didn't work for me in Ubuntu 11.04 x86-64.  Well, he had posted another guide for OpenCV 2.2 in Ubuntu 11.04, which seemed to work for me, with a few minor [...]

  8. Installing and configuring OpenCV 1.1 Eclipse on ubuntu 10.04 and higher » Tj.Wallas | Zone.X. - Tech-Reviews-Blog linked to this post on October 19, 2011

    Installing and configuring OpenCV 1.1 Eclipse on ubuntu 10.04 and higher…

    During our computer vision course this semester we were strictly required to use OpenCV version 1.1 in our programming assignments. Since this is a very outdated version, there are some problems with ……

  9. Tutorials by garag - Pearltrees linked to this post on December 7, 2011

    [...] Installing OpenCV 2.2 in Ubuntu 11.04 – Sebastian Montabone [...]

  10. Installing OpenCV 2.3.1 on Ubuntu 11.10 « Ryan Hartman's Blog linked to this post on December 13, 2011

    [...] can install OpenCV (Open Source Computer Vision) on Ubuntu 11.10.  I based this guide off of a post by Sebastian [...]

  11. ubuntu 10.04 opencv ffmpeg « Lost Ferry linked to this post on December 31, 2011

    [...] just follow this guide [...]

  12. install ffmpeg and opencv 2.3.1 | iDetect Technologies linked to this post on January 9, 2012

    [...] is how you can install OpenCV  on Ubuntu 11.10.  I based this guide off of a post by Sebastian Montabone and another post from Osman Eralp. Step 1: We need to install all of the [...]

  13. The pain of having a 64 bit Linux laptop and OpenCV | In my humble opinion… linked to this post on February 8, 2012

    [...] libraries by myself. But that’s not alwasy as easy as it looks like. After try, nor one, two, three, four, but six different methods, I got OpenCV libraries working on my machine with Python [...]



Some HTML is OK

or, reply to this post via trackback.