Skip to content


Installing OpenCV 2.1 in Ubuntu

OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis.

OpenCV 1.0 can be easily installed in Ubuntu via the repositories. You can install OpenCV 2.0 by following one of my previous posts http://www.samontab.com/web/2010/03/installing-opencv-2-0-in-ubuntu/

Unfortunately, the newer version of OpenCV, 2.1, which was released on April has a slightly different installation procedure. Since it contains many bug fixes and some nice new additions, I will show you how to install it.

Here are the steps that I used to successfully install OpenCV 2.1 in Ubuntu 9.10. I have used this procedure for previous versions of Ubuntu as well with minor modifications (if any).

First, you need to install many dependencies, such as support for reading and writing jpg files, movies, 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 libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev

The next step is to get the OpenCV 2.1 code:

cd ~
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.1/OpenCV-2.1.0.tar.bz2/download
tar -xvf OpenCV-2.1.0.tar.bz2
cd OpenCV-2.1.0/

In this version of OpenCV, the configure utility has been removed. Therefore you need to use Cmake to generate the makefile. Just execute the following line at the console. 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 .

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.

configuring opencv2.1

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

make
sudo make install

Now you have to configure the library. 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, open a new console, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

Now you have OpenCV 2.1 installed in your computer.

Let’s check some demos included in OpenCV:

cd ~
mkdir openCV_samples
cp OpenCV-2.1.0/samples/c/* openCV_samples
cd openCV_samples/
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.

In OpenCV 2.1, the grabcut algorithm is provided in the samples. 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.

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

./bgfg_segm tree.avi

There are many other samples that you can try.

Posted in Image Processing.


25 Responses

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

  1. alberto says

    Very nice tutorial, I am also fighting with the instalation of opencv 2.1. I think that it is essential in this version to get tbb to work.

    I installed libtbb2 and libtbb-dev from ubuntu 10.04, as sugested in the installation guide of opencv, but when I run:
    cmake -D:USE_TBB=ON .
    I get:

    - Interfaces:
    – Old Python: 0
    – Python: ON
    – Python interpreter: /usr/bin/python2.6
    – Python numpy: 0
    – Use IPP: NO
    – Use TBB: NO
    – Build Documentation 0

    Any ideas??

  2. AJ says

    Hi Alberto,
    I had the same problem. The documentation is incorrect. Use the flag -D:WITH_TBB=ON instead, and it will work.

    AJ

  3. alberto says

    Thanks for your response but still does not work, I think that I will wait until I get ubuntu 10.04,

    Currently my settings are:
    -Ubuntu 9.10
    - And these libraries obtained
    from ubuntu 10.04 repositories
    ii libtbb-dev 2.2+r009-1
    ii libtbb2 2.2+r009-1
    - The command I use is
    cmake -D:WITH_TBB=ON .

    But still no luck

  4. sreevani says

    What if FFMPEG is not listed as 1?How do we correct that?Or what is possibly wrong with the system?

  5. samontab says

    @Alberto, I have the same problem. under 9.10. Maybe it is easier to wait for 10.04.

    @sreevani, it means that the FFMPEG libraries are not being recongized by the Opencv installer. This may be because they are installed into a different location, not the default one. Try re installing FFMPEG, which is the first step in the tutorial. Also, this may be because you are running a different version of Ubuntu. In some cases you need to build FFMPEG manually, downloading the code from the svn repository.

  6. sreevani says

    ok got the answer.I missed out on a package needed therefore was not getting the 1 on FFMPEG.

  7. sreevani says

    Your tutorial was excellent.I can add to it though with the innocent mistakes I committed!!Excellent post man!!

  8. bear24rw says

    @alberto

    $ sudo aptitude install libtbb-dev
    $ cmake -D WITH_TBB=YES .

  9. Raam subram says

    wonderful post..Thanks

  10. stevben says

    Awesome, thank you ;)

  11. Gijs Molenaar says

    i’ve made ubuntu packages for opencv 2.1:
    http://gijs.pythonic.nl/blog/2010/apr/7/opencv-21-ubuntu-packages/

  12. tzolkin says

    Thanks :)
    if you want use python interface, you should use: cmake -D BUILD_PYTHON_SUPPORT=ON .

  13. Himanshu Pandey says

    damn good tutorial
    thanx

  14. Ibrahim says

    Thanku very much for an awesome tutorial..u made life easier

  15. Vikram Dhillon says

    Thanks for an awesome tutorial, just with some little tweaks I got this to work for lucid :) oh and I took your tutorial and made a script out of it, so the users can just execute the script and won’t have to worry about doing anything manually, are you interested in that script, you can attach it to your tutorial. Email me if you would like to see the script :)

  16. Concor says

    Your tutorial is simply awesome,thank u so much, my hat´s off

  17. Prashant says

    Thank you v much for this post . It was really useful to me , to get myself un-stuck from something I’d been battling for hours.

  18. Greg says

    The tutorial was great with one exception. When I build, it doesn’t seem to support OpenMP. When I build with Windows it does. What do I need to do to enable the multi-threading support?

  19. chandan says

    after running the bgfg_segm program i am getting this error..
    [swscaler @ 0x84d79a0]No accelerated colorspace conversion found.
    the output is not getting displayed.. Please help me..

  20. samontab says

    chandan, that seems to me that your opencv installation is not properly configured for reading videos. Go through the installing steps again and make sure that FFMPEG is listed as 1 before compiling.

  21. chandan says

    i installed ffmpeg again and now though i am able to read video files the same message is getting displayed..is it some kind of a bug in ffmpeg?

  22. Stewie says

    Hi,
    I installed OpenCV-2.1.0 on my Ubuntu 10.04, Lucid and at first everything worked fine.
    No errors, everything ok. I followed this tutorial an compiled all the examples of openCV and even that worked. Then I wanted to run the first example “facedetect”.
    As I clicked return I only got this terminal window message: ” Illegal instruction ”
    I had the OpenCV-2.0 running on Ubuntu 9.10 before and I never saw this Message.
    Does anybody know what this means or what the problem is? My Ubuntu is totally new, so I install only the things needed for OpenCV.
    Is it a problem that I’am using a AMD processor while openCV is made for Intel?
    Thanks in advance.

Continuing the Discussion

  1. OpenCV input output example – Sebastian Montabone linked to this post on April 24, 2010

    [...] installed in your machine. If you are using Ubuntu, you can read the installation instructions from here. OK, the first step is to download and extract the example that I made. You can download it from [...]

  2. Settling OpenCV2.1 on Ubuntu « Sun Ju's Blog linked to this post on June 10, 2010

    [...] Yesterday after a normal maintenance update, the box wouldn’t run smoothly. After desperately trying to  solve the problem for several times, without success, I turned to decide to make it a reincarnation. And …. 6 hours later, after the lengthy backup process, I installed the latest version of Ubuntu (10.04, Lucid) and was rewarded with another blessing: OpenCV2.1 now settled on it without any problem. Cheers! And as well thanks to this guy for his helpful blog. [...]

  3. Install OpenCV in Ubuntu 10.04 | Gnu Architecture linked to this post on July 29, 2010

    [...] Install OpenCV in Ubuntu 10.04 by Gnarc on July 29th, 2010 I found this great tutorial on http://www.samontab.com/web/2010/04/installing-opencv-2-1-in-ubuntu/ [...]



Some HTML is OK

or, reply to this post via trackback.