Skip to content


Installing OpenCV 2.4.9 in Ubuntu 14.04 LTS

The latest Long Term Support version of Ubuntu(14.04 LTS) is out and a new version of OpenCV was recently released as well. This means that now is a great opportunity to update my OpenCV installation guide to the latest versions, Ubuntu 14.04 LTS and OpenCV 2.4.9.

viz

One of the great additions in this new release of OpenCV is the new viz module, which offers a nice 3D visualizer. This module is based on the great Visualization Toolkit library (VTK) and nicely prepares the users for the upcoming OpenCV 3.

In this guide, I will show you how to install OpenCV with a lot of the features it provides. Here are some of the things that are going to be enabled when you are finished following through with this installation tutorial:

  • viz module (3D visualization)
  • Qt version of the HighGUI module (Better 2D window interface with zoom, image saving capabilities, etc)
  • OpenGL support
  • C++ interface and examples
  • C interface and examples
  • Python interface and examples
  • Java interface and examples
  • Intel Threading Building Blocks (TBB)

Note: I have noticed some copies of my posts elsewhere, so make sure that you are reading this from the original source, at samontab dot com, accessible from here so that you don’t miss the comments.

OK, so the first step is to make sure that everything in the system is updated and upgraded. Open the terminal and write this:

sudo apt-get update
sudo apt-get upgrade

Now, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, other libraries, 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 libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev

Time to get the OpenCV 2.4.9 source code:

cd ~
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip
unzip opencv-2.4.9.zip
cd opencv-2.4.9

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 the viz module, Python, Java, TBB, OpenGL, Qt, work with videos, etc, here is where we need to set that. Just execute the following line at the terminal to create the appropriate Makefile. Note that there are two dots at the end of the line, it is an argument for the cmake program and it means the parent directory (because we are inside the build directory, and we want to refer to the OpenCV directory, which is its parent).

mkdir build
cd build
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON ..

OpenCVConfig

Check that the above command produces no error and that in particular it reports FFMPEG as YES. If this is not the case you will not be able to read or write videos. Check that Java, Python, TBB, OpenGL, V4L, OpenGL and Qt are all detected correctly.

OpenCVmodulesToBeBuilt

Make sure you scroll up and check that the modules that are going to be built are these:
core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu ocl nonfree contrib java python stitching superres ts videostab viz.

If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again.

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

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.

Now you have OpenCV 2.4.9 installed in your computer with 3D visualization, Python, Java, TBB, OpenGL, video, and Qt support.

Check out the cool Qt interface which provides image viewing capabilities with zoom, as well as the ability to save the current image with just one click.

If you zoom in enough, you can see the RGB (or intensity) values for each pixel.

Now let’s build some samples included in OpenCV:

cd ~/opencv-2.4.9/samples/c
chmod +x build_all.sh
./build_all.sh
These examples use the old C interface:
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg

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

The following examples use the new C++ interface:

~/opencv-2.4.9/build/bin/cpp-example-grabcut ~/opencv-2.4.9/samples/cpp/lena.jpg

~/opencv-2.4.9/build/bin/cpp-example-calibration_artificial

Now let’s run some Python code:

python ~/opencv-2.4.9/samples/python2/turing.py

Now you can build a Java sample using ant for example. (Make sure that you change /home/samontab/ with your actual home directory):

cd ~/opencv-2.4.9/samples/java/ant
ant -DocvJarDir=/home/samontab/opencv-2.4.9/build/bin -DocvLibDir=/home/samontab/opencv-2.4.9/build/lib

java_ant

Now let’s read a video and use OpenGL with Qt through this great sample that detects the features from the video, then estimates the 3D location of the structure using POSIT, and finally uses OpenGL to draw in 3D (great sample Javier):

cd ~/opencv-2.4.9/samples/cpp/Qt_sample
mkdir build
cd build
cmake ..
make
./OpenGL_Qt_Binding

posit

And finally, let’s build a sample using the 3D visualization module viz:

cd ~/opencv-2.4.9/samples/cpp/tutorial_code/viz
g++ -o widget_pose `pkg-config opencv --cflags` widget_pose.cpp `pkg-config opencv --libs`
./widget_pose

viz

As you can see, now you can use OpenCV with C++, C, Python, and Java. The Qt enhanced 2D interface is enabled, 3D data can be displayed using OpenGL directly, or using the new viz module. Multi threading functionality is enabled using TBB. Also, video support is enabled as well.

If you correctly finished this tutorial, you are now ready to do some nice computer vision work.

Posted in Image Processing, Open Source, Programming, Qt.


41 Responses

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

  1. Jo says

    Thank you for your guide. I had really problems to get OpenCV work before and tried a bunch of other guides, but only yours worked in the end ;)

  2. samontab says

    You are welcome Jo. Great to hear that it worked for you.

  3. Pablo says

    Thanks for share.
    I was installing the OpenCV 2.4.9 in a cubieboard, with cubian (a debian fork) and with the help of your guide, it solve an issue.

    Thanks a great job

  4. samontab says

    Good job Pablo,

    Thanks for letting me know that this guide can also be applied to a cubieboard.

  5. Arbaaz Khan says

    I had mailed you a few days ago and while I managed the visualisation in matlab, this way of doing it with an integrated vtk is better. Thanks a ton for your help. :)

  6. samontab says

    Thanks Arbaaz Khan,

    I am glad it helped you.
    And, of course it is better, it is open source :)

  7. Arbaaz Khan says

    I’m having trouble with the reading a video through OpenGL and Qt. When I write the command ./OpenGL_Qt_Binding it says no such file exists. Also during build it shows support for qt and opengl and ffmpeg. What could the problem be ?

  8. samontab says

    Hi Arbaaz Khan,

    You are not providing enough information to generate a helpful answer.
    Most probably you missed a step, or you are using a different version of Ubuntu.
    From what you are saying, it may be that the executable is not being created for some reason. Do the other examples work?.
    Did you actually log out and log in again?
    Just follow every single step, line by line carefully in a fresh install of Ubuntu 14.04 LTS and it should just work.

  9. Toño says

    Your tutorial is great. But I have a error. This appear in the terminal.

    Linking CXX executable ../../bin/opencv_traincascade
    CMakeFiles/opencv_traincascade.dir/traincascade.cpp.o: no se reconoce el fichero: Fichero truncado
    collect2: error: ld returned 1 exit status
    make[2]: *** [bin/opencv_traincascade] Error 1
    make[1]: *** [apps/traincascade/CMakeFiles/opencv_traincascade.dir/all] Error 2
    make: *** [all] Error 2

  10. jeremy rutman says

    1000 blessings upon you for posting this.
    I get mostly thru , then hit
    “…By not providing “FindQt5Core.cmake” in CMAKE_MODULE_PATH this project has
    asked CMake to find a package configuration file provided by ‘Qt5Core’, but
    CMake did not find one…”
    upon trying the cmake. I did
    sudo apt-get install libboost-all-dev
    rm CMakeCache.txt
    and then the cmake again, same problem :{
    no clear answer on web search of that problem..

  11. samontab says

    Hello Jeremy,

    That is actually not an error, just a warning. It says that it can’t find Qt5, which is correct as it is not installed.
    That is fine as we are going to be using Qt4, which should be correctly installed and detected.
    Just ignore that message and continue, it should work as expected.

  12. samontab says

    Hello Toño,

    It seems that you have an incomplete download, or the file is somehow truncated.
    Try doing everything from scratch, and make sure that your download is complete.

  13. Seyedmajid says

    Hi
    Thank you for your great tutorial for installing Opencv. I just encountered with problem in step cmake. I did it again but before this step several kinds of python were installed.
    I just have a problem now regarding samples. I ran all samples except one. everything has been done according to your tutorial but when running sample for OpenGL. I receive this problem : bash: ./OpenGL_Qt_Binding: No such file or directory

    :~/opencv-2.4.9/samples/cpp/Qt_sample/build$ ls
    CMakeCache.txt CMakeFiles cmake_install.cmake cube4.avi Makefile

  14. Seyedmajid says

    Hi

    I wanted to test the core of Opencv by ./opencv_core_test
    at the end of the test just at the end it got a failed test reporting:[ FAILED ] 1 test, listed below:
    [ FAILED ] Core_globbing.accuracy . what is a problem? by the way I still have got my previous question posed in the other discussion forum in terms of one of samples using opengl. i executed all of samples after installing except this one. the output terminal says there is not opengl Qt directory in the made build.

    Best

  15. samontab says

    Hi Seyedmajid,

    It seems that you missed the make step.

  16. samontab says

    Hi Seyedmajid,

    There is nothing wrong with the code.
    The error in the test appears because testing is not completely setup.
    In particular, that test checks for particular files in OPENCV_TEST_DATA_PATH, but that path is not set. Nothing to worry about.
    If you want to learn more about that, take a look here:
    http://code.opencv.org/projects/opencv/wiki/CodingStyleGuide#Implementing-tests

  17. Umar Chughtai says

    Hi

    Great tutorial. Unfortunately I also am having trouble when it comes to reading a video with OpenGL and Qt. This is the error I get.

    bash: ./OpenGL_Qt_Binding: No such file or directory

    Is removing and re-installing the only option? I mean everything else works.

  18. samontab says

    Hi Umar,

    It means that the file is not there. Maybe you had a compilation issue or maybe you forgot a step or two.

  19. Umar Chughtai says

    Thanks for a speedy reply!

    But here is whats funny here. The file actually does exits. By the name of OpenGL_Qt_Binding.dir , it exists in the CMakeFiles folder. What I don’t get is why it isn’t being recognized

  20. RobM says

    A few people have commented on the video / OpenGL / Qt example. I also had problems, the example code seems to be missing a “make” command which builds the example and creates the file OpenGL_Qt_Binding, which can then be executed.

    I think the sample code for that section should look like this:

    cd ~/opencv-2.4.9/samples/cpp/Qt_sample
    mkdir build
    cd build
    cmake ..
    make
    ./OpenGL_Qt_Binding

    Cheers. Really great guide. I had been unable to get OpenCV running on 14.04 until this guide.

  21. samontab says

    Hi Umar,

    You need to make the executable first. I was missing one line in the tutorial, I just updated it.

  22. samontab says

    Thanks for spotting it RobM. I just updated the tutorial.

  23. Rushil says

    Hello sam, thanks for posting this great tutorial!

    So while following steps everything went great until i tried building the samples using:
    1 cd ~/opencv-2.4.9/samples/c
    2 chmod +x build_all.sh
    3 ./build_all.sh

    After which I start getting bombarded with errors similar to:
    compilation terminated.
    compiling smiledetect.cpp
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found
    smiledetect.cpp:1:43: fatal error: opencv2/objdetect/objdetect.hpp: No such file or directory
    #include “opencv2/objdetect/objdetect.hpp

    Where did I go wrong?

  24. samontab says

    Hello Rushil,

    It seems to me that the startup scripts were not called. Maybe you forgot to logout and login, or restart the machine before continuing the tutorial.
    Try this command:
    pkg-config --libs opencv
    It should tell you where the OpenCV libraries are installed. If that does not work, fix it by looking at that part of the tutorial, otherwise the buildall.sh will not work.

  25. Rushil says

    Hey sam,

    I had not installed all the VTK packages.
    I downloaded them and it works like a charm now.

    Cheers!

  26. baran says

    hello i really need ur help

    my system is 12.10 and i get cmake warning

    Looking for ffmpeg/avformat.h – not found

    i really need your help please..

  27. RB says

    Nice tutorial although it is missing additional information on some quite notorious issues such as the FFMPEG support. OpenCV usually (always? :D) requires some older version of the FFMPEG library that is currently installed on most people’s systems so you are quite often forced to seek out which version exactly you have to download, build and install in order for the OpenCV’s compilation procedure not to throw some poop in your face with FFMPEG somewhere between the lines in the resulting error messages. Some of the header files are also often not found concering:

    linux/videodev.h
    linux/videodev2.h
    sys/videoio.h
    libavformat/avformat.h
    ffmpeg/avformat.h

    Some of these can be resolved by manually editing as a root various files but it is ugly and tedious. I’m currently using Debian Wheezy and of course these issues are present. Switched from Ubuntu 12.04 LTS quite recently and there these very same problems were also present. If you look on the internet you will see that most people, who claim that their compilation process was successful, actually have these problems (you can catch their nonsense by looking at their terminal output of the cmake and make phases if any were provided). I’m curious if you had the same issues. If not, you are one hell of a lucky guy! LOL

  28. samontab says

    Hi RB,

    I actually did not find any issue at all. I am using the standard FFMPEG libraries from the Ubuntu repositories and it is working fine.
    I wouldn’t call it luck though, just documenting what I am doing and reproducing it from scratch to make sure that it works.

    Here for example I was using an older version of FFMPEG:
    http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/

  29. samontab says

    Great to hear that it worked Rushil

  30. Andrew says

    Hi. What should I do to configure Qt project for using with opencv?

  31. samontab says

    Hi Andrew,

    Just create a makefile for your project. Once you do that, you will be able to open that file with Qt.

  32. Passiday says

    Thank you for this elaborate guide, Sebastian! I hear that when working with Python, it is suggested to use virtual environments whenever possible. Can you, please, point out how to correctly install OpenCV in a Python virtualenv?

  33. Ganesh Babu says

    Well, it worked, thanks for the time you spent.. keep following your site, keep new things posted………..

  34. omar says

    Is this method still working in the case of ubuntu 64bit?
    Can you make a tutorial on how to configure codeblock IDE with openCV 2.4.9

  35. wang qaing says

    when i make it turns out errors that “relocation R_X86_64_32 against `.rodata.str1.1′ can not be used when making a shared object; recompile with -fPIC” can you help me? thank you

  36. samontab says

    Hi Omar,

    I created the guide using Ubuntu 14.04 LTS 64 bits, so it should work.
    Codeblocks is just an IDE, so it should work exactly the same as with any other library.

  37. samontab says

    Thanks Ganesh Babu

  38. samontab says

    Thanks Passiday,

    Using this guide you will have python and OpenCV installed system wide. I haven’t installed it on a python virtual environment, so I don’t have the instructions for you, sorry.

  39. samontab says

    Hi wang qaing,

    Well, you are either compiling it with other options, or in another environment or OS since it should just work.
    You could try adding -fPIC to the compilation line as suggested by the compiler.

Continuing the Discussion

  1. Installing OpenCV 2.4.1 in Ubuntu 12.04 LTS – Sebastian Montabone linked to this post on June 24, 2014

    […] EDIT: I published a new guide using Ubuntu 14.04LTS and OpenCV 2.4.9 here. […]



Some HTML is OK

or, reply to this post via trackback.