Install OpenCV 2.4.9 in Ubuntu 14.04, 13.10

  • by
  • 3 Years ago
  • 35

OpenCV is the most popular and advanced code library for Computer Vision related applications today, spanning from many very basic tasks (capture and pre-processing of image data) to high-level algorithms (feature extraction, motion tracking, machine learning). It is free software and provides a rich API in C, C++, Java and Python. Other wrappers are available. The library itself is platform-independent and often used for real-time image processing and computer vision.

What’s new in 2.4.9

  • new 3D visualization module ‘viz’
  • performance fixes in ‘ocl’ module
  • fixes in Android Camera
  • improved CUDA support for mobile platforms
  • bugfixes from community
  • 55 reported bugs have been closed
  • 156 pull requests have been merged

See the full ChangeLog for more information

Install OpenCV

Step 1: Make Directory

mkdir opencv

cd /opencv/

Step 2: Remove any pre-installed ffmpeg and x264

sudo apt-get -qq remove ffmpeg x264 libx264-dev

Step 3:  Install Dependenices

[box type=”info” icon=”info”]As reported by kumaranbus, installing ‘libopencv-dev‘ on non nvidia graphics card messes up the drivers. As a workaround for non nvidia cards like AMD/ATI, Radeon, replace libopencv-dev below with ‘ocl-icd-libopencl1‘ which is a generic driver. Please note that I have not tried this out yet as I have no ATI/Radeon cards to test with[/box]

sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils

– Install ffmpeg

sudo add-apt-repository ppa:mc3man/trusty-media

sudo apt-get update

sudo apt-get install ffmpeg gstreamer0.10-ffmpeg

Step 4: Download and extract OpenCV

wget -O OpenCV-2.4.9.zip http://fossies.org/linux/misc/opencv-2.4.9.zip

unzip OpenCV-2.4.9.zip

cd opencv-2.4.9

Step 5: Begin Build Process

Note that this bit would take some time to complete

mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -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 ..

make -j2

sudo make install

sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'

sudo ldconfig

opencv-2.4.9-01

– Reboot your machine

Step 6: Example using OpenCV

– Change directory to following location

cd /usr/local/share/OpenCV/samples/c

– Execute following commands

sudo chmod +x build_all.sh

sudo ./build_all.sh

./facedetect "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" lena.jpg

or

./facedetect --cascade="/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" lena.jpg

opencv-2.4.9-02

  • facebook
  • googleplus
  • twitter
  • linkedin
  • linkedin
  • newroad

    I am trying to install opencv using this procedure on ARM platform with xubuntu 13.10. I am getting error when building opencv

    Do you have any specific procedure for arm platform

  • johnshoeb

    Which version of FFmpeg, yasm and x264 is required for opencv 2.4.9?

  • sysads

    johnshoeb  

    There is no minimum or specific requirement that I am aware of. So run the commands to install whatever version is currently available in Ubuntu repository.
    In the meantime, have you tried running it and ran into an issue?

  • sysads

    newroad  
    I am afraid, I don’t have any procedures yet for arm platform. In the meantime, why not post the error so other can assist.

  • johnshoeb

    sysads johnshoeb  
    Thanks! I exactly followed your tutorial and it works for the desktop version of Ubuntu 12.04 LTS. However, I need to setup this on server edition of Ubuntu 12.04. After following these steps it shows error “error while loading shared libraries: libopencv_core.so.2.3: cannot open shared object file: No such file or directory”. It seems some of the dependencies are missing in server edition. What to do now?

  • sysads

    johnshoeb sysads  
    Is this a 64bit version or 32bit version?

  • johnshoeb

    sysads johnshoeb  
    64 bit version

  • sysads

    johnshoeb sysads  

    @ what stage did you get the error?

  • johnshoeb

    sysads johnshoeb  
    All installation steps went well. While I execute my application, it shows “error while loading shared libraries: libopencv_core.so.2.3: cannot open shared object file: No such file or directory”. But I ran the same program on Ubuntu Desktop 12.04 following your tutorial. Error shows only on Ubuntu Server edition.

  • Sandhan_sarma

    It worked perfectly, except the last part of executing the facedetect program.
    Also,since i am a complete beginner to OpenCV and cmake, can you please guide me about what the different dependencies are for, and what is cmake, and how do we implement it.

  • chadaphone

    It is working perfectly.
    Thank so much 🙂

  • Nisl

    Hello, I did the complete installation and everything seemed to go well. However when I tried do the facedectect on lena I got an error.
    (./facedetect “/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml” lena.jpg )

    however I get the following error: 
    This program demonstrates the cascade recognizer. Now you can use Haar or LBP features.
    This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.
    It’s most known use is for faces.
    Usage:
    ./facedetect [–cascade=<cascade_path> this is the primary trained classifier such as frontal face]
    [–nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]
       [–scale=<image scale greater or equal to 1, try 1.3 for example>]
       [–try-flip]
       [filename|camera_index]
    see facedetect.cmd for one call:
    ./facedetect –cascade=”../../data/haarcascades/haarcascade_frontalface_alt.xml” –nested-cascade=”../../data/haarcascades/haarcascade_eye.xml” –scale=1.3
    During execution:
            Hit any key to quit.
            Using OpenCV version 2.4.9
    Processing 1 /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml
    Processing 2 lena.jpg
    ERROR: Could not load classifier cascade
    This program demonstrates the cascade recognizer. Now you can use Haar or LBP features.
    This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.
    It’s most known use is for faces.
    Usage:
    ./facedetect [–cascade=<cascade_path> this is the primary trained classifier such as frontal face]
       [–nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]
       [–scale=<image scale greater or equal to 1, try 1.3 for example>]
       [–try-flip]
       [filename|camera_index]
    see facedetect.cmd for one call:
    ./facedetect –cascade=”../../data/haarcascades/haarcascade_frontalface_alt.xml” –nested-cascade=”../../data/haarcascades/haarcascade_eye.xml” –scale=1.3
    During execution:
            Hit any key to quit.
            Using OpenCV version 2.4.9

  • kardoostamirhossein

    Nisl Hi, Could you solve this problem? I have the same problem! 🙁

  • chadaphone

    kardoostamirhossein Nisl 
    please try:
    ./facedetect –cascade=”/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml” lena.jpg

  • Nisl

    chadaphone 
    Thank you!

  • sysads

    chadaphone kardoostamirhossein Nisl
    Thanks for pointing this out. Will update the post

  • abacusreader

    Just a word of caution, I was running Ubuntu 13.10 on a AMD laptop with Radeon Graphics card. I followed the instructions above. I was able to build OpenCV but please note that libopencv seems to depend on NVidia video drivers which gets installed in the process and this results in the computer becoming completely unusable after a while as the video drivers are messed up. It was a nightmare to get my computer back  in shape. In case your are running 13.10 and if you are reading this comment after such a mess up. Don’t expect that upgrading your Ubuntu to 14.04 will revert the drivers to a working state, the drivers will be left untouched. So please remove libopencv and nvidia driver but I couldn’t figure a way to reinstall the AMD catalyst drivers with out a GUI, in the end I had to repartition my hard disk to make a fresh installation of Ubuntu. I am yet to compile opencv in it, I will post an update when it is successful. In the mean while this is a word of caution for users with non nvidia graphics card trying to build opencv using the above instruction. you need to read this
    http://askubuntu.com/questions/365897/how-to-install-opencv-without-nvidia-drivers

  • sysads

    abacusreader
    Thanks for bringing this to my notice. Strange that I didn’t see this while installing on my intel machine with Ubuntu 14.04.
    Unfortunately I am unable to reproduce this issue since I don’t have an AMD Radeon card, but I will update the post with non-nvidia cards dependencies so others don’t run into the same issue you ran into.

  • abacusreader

    sysads 
    Many thanks for updating your post quickly enough, I am now building opencv 2.4.9 on a fresh installation of Ubuntu 14.04 on a AMD 64bit machine with a Radeon graphics card.
    After following your instructions alone, the build fails at some point complaining about not finding libGL.so I  needed to make a link as below to proceed further

      sudo ln -s /usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/libGL.so
    The build failed further down the line and  complained about few more things so I had to install 
    sudo apt-get install openexr
     
    sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    The build is still going on, I will edit the comment as it progresses and if I encounter any issues.

  • abacusreader

    sysads 
    The build fails here, I am yet to find a resolution for this if you have any idea why this error occurs could you please share

    /usr/bin/ld: ../../lib/libopencv_highgui.a(cap_libv4l.cpp.o): undefined reference to symbol ‘v4l2_munmap’
    //usr/lib/x86_64-linux-gnu/libv4l2.so.0: error adding symbols: DSO missing from command line
    collect2: error: ld returned 1 exit status
    make[2]: *** [bin/opencv_test_highgui] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_test_highgui.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….
    [ 56%] Built target opencv_gpu

  • sysads

    abacusreader sysads

    At what stage of the process did you get this error?

  • abacusreader

    sysads 
    At around 50% of the build
    – when it is trying to build
    [ 56%] Built target opencv_gpu

  • sysads

    abacusreader sysads
    Not able to replicate this issues since i do not have the same hardware spec like yours. In the meantime, i have logged a case with OpenCV. So will update you when i get a response or when they need more info

  • abacusreader

    An update with reference to the build process, I was using the source from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download to build, which resulted in the error I have posted earlier.
    I gave up on it and then downloaded the source using git. Then I  did
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

    as mentioned in the opencv website, instead of the elaborate make options to build the samples.

    With this, opencv compiled with out any error and installed. I can see the lib files and xml files in the correct folders.

    I manually compiled the sample files inside the “c” folder these too compiled with out any errors.

    I am able to run the facedetect example and it is working as expected.

    I am not sure whether the zip file has some different make files which mess up things during the build.

  • abacusreader

    To get rid of the DSO error refer to this hack

    http://code.opencv.org/issues/3726

  • sysads

    abacusreader
    Interesting! Its possible the copy on git’s is updated or newer than one hosted in sourceforge.

  • abacusreader

    I have tested both the sources now both of them don’t compile if you
    have v4l2 installed  on your machine unless you add the additional
    -lv4l2 entry in the linker scripts. 
    Bottom line is if you have v4l2 installed you are bound to get the DSO error due to an incomplete configuration setting.As I am a novice with Cmake and Make, I manually added the -lv4l2 in approximately 5 different places in a file called link.txt as and when the error occured. Now I have the Jar file for opencv Java development

  • antgr

    I have installed gcc4-9 following an other tutorial of your site. So now following this procedure I have an error.

    In file included from /usr/local/cuda-6.0/include/cuda_runtime.h:59:0,
                   from <command-line>:0:
    /usr/local/cuda-6.0/include/host_config.h:82:2: error: #error — unsupported GNU version! gcc 4.9 and up are not supported!
     #error — unsupported GNU version! gcc 4.9 and up are not supported!
      ^
    CMake Error at cuda_compile_generated_matrix_operations.cu.o.cmake:208 (message):
      Error generating
      /home/ant/opencv/opencv-2.4.9/build/modules/core/CMakeFiles/cuda_compile.dir/__/dynamicuda/src/cuda/./cuda_compile_generated_matrix_operations.cu.o
    make[2]: *** [modules/core/CMakeFiles/cuda_compile.dir/__/dynamicuda/src/cuda/./cuda_compile_generated_matrix_operations.cu.o] Error 1
    make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
    make: *** [all] Error 2

    Thanks a lot

  • sam768

    Hi, I get always an error with the command make -j2

    Linking CXX shared library ../../lib/libopencv_highgui.so
    /usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32 against `.rodata.str1.1′ can not be used when making a shared object; recompile with -fPIC
    /usr/local/lib/libavcodec.a: error adding symbols: Bad value
    collect2: error: ld returned 1 exit status

    can you help me please, I have Ubunut 14.04

    • Looks like you are missing some build dependencies. try this command first and repeat installation

      sudo apt-get build-dep opencv

      • sam768

        unfortunately I get still the same error 🙁

      • sam768

        well I did now a clean Ubuntu 14.04 installation on a virtual machine and surprise it worked… 🙂
        so I guess I have to install Ubuntu again on my laptop :-/

  • Boris

    thanks, work very fine to me :D, thanks a lot again

    • Thanks and please share with friends also 🙂

  • Mohamed Elaskily

    am tring to run a script on Matlab and this script use opencv, i
    install opencv 3.0 and when i running the script it gives an error
    Processing: dataset/MICC-F220/CRW_4853tamp1.jpg (1/220)
    lib/sift/bin/siftfeat:
    error while loading shared libraries: libopencv_core.so.2.3: cannot
    open shared object file: No such file or directory