24 September 2011

Installing OpenCV 2.3 on Ubuntu 11.04

Building and installing OpenCV 2.3 on Ubuntu 11.04 -
1. Install the OpenCV dependencies by typing following commands.

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev
To cover all the dependencies install -
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install pkg-config
sudo apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3
sudo apt-get install libpnglite-dev libpngwriter0-dev libpngwriter0c2
sudo apt-get install zlib1g-dbg zlib1g zlib1g-dev
sudo apt-get install libjasper-dev libjasper-runtime libjasper1
sudo apt-get install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
sudo apt-get install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs
sudo apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev
sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0  libgstreamer0.10-dev
sudo apt-get install libxine1-ffmpeg  libxine-dev libxine1-bin
sudo apt-get install libunicap2 libunicap2-dev
sudo apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils
sudo apt-get install swig
sudo apt-get install libv4l-0 libv4l-dev
sudo apt-get install python-numpy

Further optional dependencies could be checked and installed from the configure option during building. For more info go to OpenCV site.

2. Download the OpenCV 2.3 source from here or type following command -
   wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2/

3. Extract the OpenCV tar file (preferably in your home directory)
    tar -xvf OpenCV-2.3.1a.tar.bz2

4. Now create the build directory for opencv
    $ cd OpenCV-2.3.1
    $ mkdir opencv.build
    $ cd opencv.build

5. Configure, make and install the code -
    $ cmake ..
    $ make
    $ sudo make install

6. Open the .bashrc file from your home folder
    $ gedit .bashrc
     and add the following at the end of the file -

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/<username>/OpenCV-2.3.1/opencv.build/lib
    export PKG_CONFIG_PATH=/home/<username>/OpenCV-2.3.1/lib/opencv.build/pkgconfig
    alias gcv="g++ `pkg-config --cflags opencv` `pkg-config --libs opencv`"

7. Now test building the samples -
    $ cd /home/<username>/OpenCV-2.3.1/samples/c
    $ chmod a+x build_all.sh
    $ ./build_all.sh
   If the samples are successfully compiled, then your opencv is ready to use.

8. Since we added an alias in the bashrc file named "gcv", you can compile any opencv code just by using this alias as -
    $ gcv hellocv.cpp

22 comments:

  1. If you are trying to install OpenCV 2.2 on Ubuntu 11.04, it will give error while installation. Check out this link for correction - https://code.ros.org/trac/opencv/changeset/5206

    ReplyDelete
  2. While installing OpenCV 2.2 on Ubuntu 11.04, the error which occurs is undefined reference to `cvCreateCameraCapture_V4L(int)'. The solution to this problem can be found at Opencv changeset

    ReplyDelete
  3. hello i m utakrsh and i have installed opencv 2.3 but i have got this error..

    /usr/bin/ld: cannot find -lcv
    /usr/bin/ld: cannot find -lhighgui
    /usr/bin/ld: cannot find -lcvauxSs
    collect2: ld returned 1 exit status

    i have no idea what to do please help me thanks in advance...

    ReplyDelete
  4. Hi Utkarsh, Did your opencv building process gave any errors? Are you getting this error while compiling a sample program?

    As far I feel, if your library is built properly, then your system is not able to find the path of the library. So there could be a mistake in these two lines of your .bashrc file.
    1. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/your username/OpenCV-2.3.1/opencv.build/lib
    2. export PKG_CONFIG_PATH=/home/your username /OpenCV-2.3.1/lib/opencv.build/pkgconfig

    Check for the path correctly and try to recompile the program. Tell me if there is any problem.

    ReplyDelete
  5. i tried to compile some sample programs in opencv 2.3.1 after i downloaded using the above instructions. I get the following error.:
    fatal error: opencv2/imgproc/imgproc.hpp: No such file or directory
    compilation terminated.

    ReplyDelete
  6. Hi Rakshith,
    I want to know which sample program gave you this error. Did you run the script named buildall.sh in your opencv>samples>C extracted folder. If this script runs properly, then your opencv has installed correctly otherwise there is some problem with your opencv installation. Moreover imgproc.hpp is the image processing header file of opencv. So check if it exists in your opencv installed libraries folder mostly present in /usr/include/opencv. Check for presence of opencv headers in this directly if you want.

    ReplyDelete
  7. Splendid Job Gaurav!

    ReplyDelete
  8. 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

    This is caused by the the file /usr/local/libavcodec/avcodec.h installed by ffmpeg is different from the file in /OpenCV2.2_source/3rdparty/include/ffmpeg_/libavcodec/avcodec.h.
    So I did the steps to slove the problem
    1) uninstall ffmpeg first by typing "sudo make uninstall" in ffmpeg source directory. This will remove ffmpeg files in /usr/local/...
    2) Kill configured OpenCV files and reconfigure it by CMake with "With FFMPEG" selected.
    3) then "make".
    OpenCV will use the /3rdparty/...ffmpeg_ instead and it is OK on my system.

    ReplyDelete
  9. I am installing OpenCV.2.3.1 in Ubuntu 10.10. It complains that libjpeg-prog does not exist, the correct package is libjpeg-progs.

    ReplyDelete
  10. The path to opencv-2.3.1 is not
    http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.2/OpenCV-2.3.1a.tar.bz2

    the correct one is
    http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2/

    ReplyDelete
  11. hello,

    i cant acess the cvcaputrecamera in ubuntu 11.04 and 10.10 is there any way to fix this?
    especiallt facedetect can't intialise camera!
    i used this guide http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/

    ReplyDelete
    Replies
    1. what is the error its giving while intialization?
      The cap_ffmpeg.cpp is the source file which uses ffmpeg to perform capturing of the device. While doing cmake for building opencv, check if the ffmpeg dependency is met or not. As given in three comments above, latest ffmpeg has some issues with OpenCV 2.3. Try installing this library first and then build opencv again. Hope it should work.

      Delete
    2. Install these packages before building your opencv - libv4l-0 libv4l-dev ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev

      Delete
  12. Hi, I get this error when I try to sudo make install... any idea? thanks

    [ 25%] Built target pch_Generate_opencv_highgui
    [ 25%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/loadsave.o
    In file included from /home/christian/Descargas/OpenCV-2.3.1/modules/highgui/src/grfmts.hpp:54:0,
    from /home/christian/Descargas/OpenCV-2.3.1/modules/highgui/src/loadsave.cpp:47:
    /home/christian/Descargas/OpenCV-2.3.1/modules/highgui/src/grfmt_exr.hpp:48:31: fatal error: ImfChromaticities.h: No existe el fichero o el directorio
    compilation terminated.
    make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/loadsave.o] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make: *** [all] Error 2

    ReplyDelete
  13. Ouch, I found the problem, I need to install Openexr from repository packages and now compile fine!

    ReplyDelete
  14. Hi i'm new to linux,i followed the instruction and when i typed
    $ gedit .bashrc
    it couldn't find the file. moreover i got these red lines when i typed
    $ make
    the error are
    [18%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui_pch_dephel.dir/opencv_highgui_pch_dephelp.o
    [19%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap.o
    [19%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_images.o
    [20%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.o
    [20%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/loadsave.o
    [20%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/precomp.o
    [20%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/utils.o
    [20%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/window.o
    [21%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_dc1394_v2.o
    [21%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_lib4l.o
    [21%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/bitstrm.o
    [21%] Bulding CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/grfmt_ext.o

    ReplyDelete
    Replies
    1. Which linux are you using? .bashrc file is present in your home/'user'/ folder of your ubuntu. This file name or location may change depending on the linux you are using.
      The lines you have shown are normal building process, its not error.

      Delete
    2. I am using Oracle virtual box manager running under Debian squeeze. i want to install OPencv over debian

      Delete
  15. To install OpenCV using the terminal on Ubuntu:
    $ su -
    # apt-get update
    # apt-get install build-essential
    # apt-get install libavformat-dev
    # apt-get install x264 v4l-utils ffmpeg
    # apt-get install libcv2.3 libcvaux2.3 libhighgui2.3 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev

    http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html

    ReplyDelete