Wednesday, May 16, 2012

OpenCV 2.4 on Ubuntu 12.04

OpenCV 2.4.0 was released this May providing, a lot of improvements and bug fixes. For a complete change log see here.

This post will hopefully make your life easier to configure, compile, install and test it out.

This time I tried to categorize and explain the dependencies, so that it is easier to configure it for your needs. As always, comments and suggestions are welcome.

Ubuntu 12.04 provides a package of OpenCV 2.3.1 that you can easily install by typing:
sudo apt-get install libopencv-dev
If you do not care about having the latest version you could skip the rest of the post.

Install Dependencies


Essentials
These are libraries and tools required by OpenCV.
sudo apt-get install build-essential checkinstall cmake pkg-config yasm

Image I/O
Libraries for reading and writing various image types. If you do not install then the versions supplied by OpenCV will be used.
sudo apt-get install libtiff4-dev libjpeg-dev libjasper-dev


Video I/O
You need some or all of these packages to add video capturing/encoding/decoding capabilities to the highgui module.
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev

Python
Packages needed to build the Python wrappers.
sudo apt-get install python-dev python-numpy

Other third-party libraries
Install Intel TBB to enable parallel code in OpenCV.
sudo apt-get install libtbb-dev

GUI
The default back-end for highgui in Linux is GTK. You can optionally install QT instead of GTK and later enable it in the configuration (see next section). 
sudo apt-get install libqt4-dev libgtk2.0-dev

Compile and Install

Get a copy of the source code here, extract and create a build directory:
tar -xvf OpenCV-2.4.0.tar.bz2
cd OpenCV-2.4.0/
mkdir build
cd build
Configure using CMake. You have a lot of options in this step. This is what I use:
cmake -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..
Notice, that by adding the -D WITH_QT=ON, the highgui module will use QT instead of GTK. If you want to go with GTK just remove this. For more information on the options, look at the CMakeLists.txt file. When you are happy with the configuration you have, you can start compiling:
make
If compilation finishes without errors, you can install by saying:
sudo make install
Finally, make sure that your programs can link to the OpenCV library in run-time by adding the following line at the end of your /etc/ld.so.conf:
/usr/local/lib
And then configure dynamic linker run-time bindings:
sudo ldconfig

Testing

An easy way to test that the compilation went well is to use the OpenCV test utilities. For example, to test the core module go to OpenCV-2.4.0/build/bin and run:
 ./opencv_test_core
You should see something like that:


Note that some failures when testing other modules may come from missing image files. To correctly run these tests or samples you should move the corresponding image files from OpenCV-2.4.0/samples to OpenCV-2.4.0/build/bin.

For testing that you can compile your own programs and link against the installed OpenCV libraries I have packaged the face detection sample with all the necessary files and a simple Makefile. Download it here, extract and type:
make
This should compile and run with a test image, so you should see something like this:



59 comments:

  1. make -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..


    should be:


    cmake -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..

    ReplyDelete
  2. makefile doesn't work for the demo program in the last step

    ReplyDelete
  3. Hey Ayush,

    I noticed I packed the binary in the zip file and it created problems. The link is now fixed. Let me know if it works for you. If not, please post the error you get.

    ReplyDelete
  4. Works for me. Newb OpenCV developer here.

    ReplyDelete
  5. Really handy! Thank you very much. :)

    ReplyDelete
  6. Hi
    while executing make , i got the following build message , any idea?


    [ 6%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/rotcalipers.cpp.o
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp: In function ‘CvBox2D cvMinAreaRect2(const CvArr*, CvMemStorage*)’:
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:331:33: warning: ‘*((void*)& buffer +16)’ may be used uninitialized in this function [-Wuninitialized]
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:97:10: note: ‘*((void*)& buffer +16)’ was declared here
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:328:33: warning: ‘*((void*)& buffer +8)’ may be used uninitialized in this function [-Wuninitialized]
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:97:10: note: ‘*((void*)& buffer +8)’ was declared here
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:318:52: warning: ‘*((void*)& buffer +20)’ may be used uninitialized in this function [-Wuninitialized]
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:97:10: note: ‘*((void*)& buffer +20)’ was declared here
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:317:87: warning: ‘*((void*)& buffer +4)’ may be used uninitialized in this function [-Wuninitialized]
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:97:10: note: ‘*((void*)& buffer +4)’ was declared here
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:317:52: warning: ‘buffer’ may be used uninitialized in this function [-Wuninitialized]
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:97:10: note: ‘buffer’ was declared here
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:314:30: warning: ‘*((void*)& buffer +12)’ may be used uninitialized in this function [-Wuninitialized]
    /home/OpenCV-2.4.0/modules/imgproc/src/rotcalipers.cpp:97:10: note: ‘*((void*)& buffer +12)’ was declared here
    [ 7%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/phasecorr.cpp.o
    [ 7%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/geometry.cpp.o

    ReplyDelete
  7. I only see warnings in what you posted. Was there an error?

    ReplyDelete
  8. @sateeshkg
    I guess the uninitialized buffer makes this error/warning.
    Initializing the buffer after creation removes the error.
    buffer is in modules/imgproc/src/rotcalipers.cpp line 97
    But I got this message with codeblocks. no errors with gnu make

    I added
    memset((void *)(buffer), 0x00, 32);

    ReplyDelete
  9. Awesome.. I didn't have libgtk2.0-dev and pkg-config, as I chose to go with GTK I encountered a small error when running ur sample program..
    Otherwise everything went very well.. Thanks for the great tutorial..

    ReplyDelete
  10. Hi, it is possible install opencv 2.4 in ubuntu 11.04 with the above steps? thanks.

    ReplyDelete
  11. Thanx for the tutorial!

    It worked in the end, but not exactly as mentioned.

    The library was installed at /usr/local/lib/
    so in the beginning I couldn't compile the example.
    (I thought that the files would be installed at /usr/local/lib/openCV or sth similar)

    Then I added (at the LIBS +=) -L/usr/local/lib/, and everything worked.

    dimitris

    ReplyDelete
  12. Thank you very much. A long time I'm trying install OpenCV-2.4 but always had errors.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  14. thanks Laz...

    Great and clear tutorial...

    ReplyDelete
  15. Great and simple tutorial
    thanks LAZ

    ReplyDelete
  16. Thank you very much for such easy steps! Tried for the first time in life n it worked really fine. Got the face recognition image as was in the web. Thanks again for the great help!

    ReplyDelete
  17. Yoo !

    Tried a couple of tutorials and this one did the trick !! Thanks !

    ReplyDelete
  18. I'm really glad you guys find it useful

    ReplyDelete
  19. i am new in c++ and opencv..
    will you please help me to run a open cv program in ubuntu 12.10 that i wrote to load an image.
    Thanks..

    ReplyDelete
    Replies
    1. If you installed opencv, then all you have to do is write a makefile and compile your simple program. You can use the makefile I provide above.

      Delete
  20. I got error:
    Illegal instruction (core dumped)
    make: *** [run] Error 132

    This error happen when I try to run a sample or your example with faceDetect

    Any idea what I can do so I can ran the samples?

    Thanks

    ReplyDelete
    Replies
    1. I am sorry but this doesn't give me enough information. Can you post the whole make output?

      Delete
  21. Worked perfectly, first time (installed on a fresh copy of Xubuntu 12.04 LTS). Thank you for posting this, as it is the clearest and most current documentation I've seen.

    ReplyDelete
  22. I got the error:
    faceDetect.cpp:1:43: fatal error: opencv2/object/objdetect.hpp: no such files

    ReplyDelete
    Replies
    1. This was just solved by moving the files from /modules to /include/opencv2/ and adding -I/home/...(the directory of .cpp files) in Makefile, i.e., g++ -I/home/... blablabla

      Delete
  23. My current problem is:
    g++ faceDetect.cpp -I/home/johnestar/CV/OpenCV-2.4.0/include -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -o faceDetect
    /usr/bin/ld: cannot find -lopencv_core
    /usr/bin/ld: cannot find -lopencv_imgproc
    /usr/bin/ld: cannot find -lopencv_highgui
    /usr/bin/ld: cannot find -lopencv_objdetect
    collect2: ld returns 1

    Could you help me? I've passed ./opencv_test_core

    ReplyDelete
  24. Please,tell me your method:Use opencv and qt everywhere to build your project for KIT ARM (example: friendlyARM mini2440,tiny210...).Thanks

    ReplyDelete
  25. I did all steps but there is problem ;

    Description Resource Path Location Type
    Function 'cvDestroyWindow' could not be resolved main.cpp /deneme1/src line 19 Semantic Error
    make: *** [src/main.o] Hata 1 deneme1 C/C++ Problem
    Function 'cvReleaseImage' could not be resolved main.cpp /deneme1/src line 18 Semantic Error
    Function 'cvWaitKey' could not be resolved main.cpp /deneme1/src line 17 Semantic Error
    Function 'cvShowImage' could not be resolved main.cpp /deneme1/src line 16 Semantic Error
    Symbol 'CV_WINDOW_AUTOSIZE' could not be resolved main.cpp /deneme1/src line 15 Semantic Error
    Function 'cvNamedWindow' could not be resolved main.cpp /deneme1/src line 15 Semantic Error
    Function 'cvLoadImage' could not be resolved main.cpp /deneme1/src line 14 Semantic Error
    Type 'IplImage' could not be resolved main.cpp /deneme1/src line 14 Semantic Error
    Symbol 'cv' could not be resolved main.cpp /deneme1/src line 11 Semantic Error


    it is about cv and whan can I do :/

    ReplyDelete
    Replies
    1. I suspect that this error is Eclipse related. Are you using Eclipse? Did you try to Google it?

      Delete
  26. Thank you very much for the post!! I had no problems installing opencv following this guide.

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. I am in "build" and when i Configure Cmake , it says "cmake-D" command not found
    kindly help in this regard

    ReplyDelete
    Replies
    1. Did you install cmake? Maybe you skipped this part:

      sudo apt-get install build-essential checkinstall cmake pkg-config yasm

      Delete
  29. This comment has been removed by the author.

    ReplyDelete
  30. This comment has been removed by the author.

    ReplyDelete
  31. Even , i went for this too , but this also stucks in cmake step

    http://www.raben.com/book/export/html/3

    Kindly Help

    ReplyDelete
  32. @Rida Nisar: Please be careful while following instructions. Your first error was trying to untar the wrong file. Then you mistyped "make-D" instead of "make -D". Finally, the string "BUILD_EXAMPLES=ON.." is not supposed to be part of the path, but part of the input arguments to cmake. I would have to assume that you mistyped something again (probably skipped a space).

    Please, avoid flooding the comment section. Try to only post something that will also help other people.

    ReplyDelete
  33. Thank you so much Laz

    However, I have problems.

    At first install trial, it works well.
    Test completed successfully.
    However,

    when I enter this,

    $./facedetect.py (this is in the sample folder)
    Traceback (most recent call last):
    File "./facedetect.py", line 10, in
    import cv2.cv as cv
    ImportError: /usr/lib/i386-linux-gnu/libQtTest.so.4: undefined symbol: _ZN9QListData11detach_growEPii

    And,

    $ ./touching_object.py
    Traceback (most recent call last):
    File "./touching_object.py", line 5, in
    from all_module import *
    File "/home/gibeom/NAO/nao_murata/all_module.py", line 22, in
    import cv
    File "/usr/local/lib/python2.7/dist-packages/cv.py", line 1, in
    from cv2.cv import *
    ImportError: /usr/lib/i386-linux-gnu/libQtTest.so.4: undefined symbol: _ZN9QListData11detach_growEPii


    This error message's printed
    So I search the web, but I can't find answer.

    ReplyDelete
    Replies
    1. Of course,
      I also did it

      Finally, make sure that your programs can link to the OpenCV library in run-time by adding the following line at the end of your /etc/ld.so.conf:
      /usr/local/lib
      And then configure dynamic linker run-time bindings:
      sudo ldconfig



      Below is my ld.so.conf

      include /etc/ld.so.conf.d/*.conf

      /usr/local/lib


      Is there anything more that I have to do to point the PATH? I don't what is the problem.
      I tried remove libQtTest.so.4
      But he just printed I can't find libQtTest.so.4

      Delete
  34. Therefore, I try to uninstall

    sudo apt-get remove OpenCV-2.4.0
    It didn't work
    So try ./cmake_uninstall.cmake (in build folder)
    It didn't work.
    ~/opencv/OpenCV-2.4.0/build$ ./cmake_uninstall.cmake
    ./cmake_uninstall.cmake: line 5: syntax error near unexpected token `NOT'
    ./cmake_uninstall.cmake: line 5: `IF(NOT EXISTS "/home/gibeom/opencv/OpenCV-2.4.0/build/install_manifest.txt")'

    make clean did something but it seemed not working.
    folder and file exist.

    ReplyDelete
  35. Anyway, I try to re-install opencv
    But, It printed error message

    [ 22%] Building CXX object modules/imgproc/CMakeFiles/opencv_perf_imgproc.dir/perf/perf_precomp.cpp.o
    Linking CXX executable ../../bin/opencv_perf_imgproc
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QTextureGlyphCache::populate(QFontEngine*, int, unsigned int const*, QFixedPoint const*)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QTextureGlyphCache::fillInPendingGlyphs()'
    ../../lib/libopencv_highgui.so.2.4.0: undefined reference to `QMutex::unlockInternal()'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QStaticTextItem::setFontEngine(QFontEngine*)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QPaintEngineEx::drawPixmapFragments(QRectF const*, QRectF const*, int, QPixmap const&, QFlags)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QImageTextureGlyphCache::createTextureData(int, int)'
    ../../lib/libopencv_highgui.so.2.4.0: undefined reference to `QMutex::lockInternal()'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QRadialGradient::focalRadius() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QRadialGradient::centerRadius() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QWidgetPrivate::hasHeightForWidth() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QImageTextureGlyphCache::fillTexture(QTextureGlyphCache::Coord const&, unsigned int, QFixed)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QTextureGlyphCache::textureMapForGlyph(unsigned int, QFixed) const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QPixmapData::toImage(QRect const&) const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `qt_painterPathFromVectorPath(QVectorPath const&)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QTextureGlyphCache::subPixelPositionForX(QFixed) const'

    ReplyDelete
  36. /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QMetaType::registerTypedef(char const*, int)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QStaticTextItem::~QStaticTextItem()'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QImageTextureGlyphCache::glyphMargin() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QListData::detach_grow(int*, int)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QGraphicsSystem::platformExtension()'
    ../../lib/libopencv_highgui.so.2.4.0: undefined reference to `QRadioButton::minimumSizeHint() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QWindowSurface::QWindowSurface(QWidget*, bool)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QGraphicsSystem::createPixmapData(QPixmapData*)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QBezier::toPolygon(double) const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `vtable for QImageTextureGlyphCache'
    ../../lib/libopencv_highgui.so.2.4.0: undefined reference to `QListData::detach(int)'
    /usr/lib/i386-linux-gnu/libQtTest.so.4: undefined reference to `QElapsedTimer::msecsSinceReference() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QImage::constBits() const'
    /usr/lib/i386-linux-gnu/libQtTest.so.4: undefined reference to `QElapsedTimer::start()'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QImageData::convertInPlace(QImage::Format, QFlags)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `qt_isExtendedRadialGradient(QBrush const&)'
    ../../lib/libopencv_highgui.so.2.4.0: undefined reference to `QCheckBox::minimumSizeHint() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `typeinfo for QImageTextureGlyphCache'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QPaintEngineEx::drawPixmapFragments(QPainter::PixmapFragment const*, int, QPixmap const&, QFlags)'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QImageTextureGlyphCache::resizeTextureData(int, int)'
    /usr/lib/i386-linux-gnu/libQtTest.so.4: undefined reference to `QElapsedTimer::elapsed() const'
    /usr/lib/i386-linux-gnu/libQtOpenGL.so.4: undefined reference to `QPaintEngineEx::drawStaticTextItem(QStaticTextItem*)'
    collect2: error: ld returned 1 exit status
    make[2]: *** [bin/opencv_perf_imgproc] Error 1
    make[1]: *** [modules/imgproc/CMakeFiles/opencv_perf_imgproc.dir/all] Error 2
    make: *** [all] Error 2


    How can I remove and re-install that thing.
    And How can I solve import-cv and
    undefined symbol.

    Thank you very much again

    ReplyDelete
  37. Hey, after going through some 10 tutorials, finally got one that works. thanks a lot. Btw do you also have some instruction on how to install in Mac OS X. Please let me know if you have any. Great work

    ReplyDelete
  38. hi,
    i have the some issues with he part :

    >>Finally, make sure that your programs can link to the OpenCV library >>in run-time by adding the following line at the end of your
    >>/etc/ld.so.conf:

    >>/usr/local/lib

    my problem is, when i go inside to /etc/ld.so.conf by vi
    i get this error:

    << "/etc/ld.so.conf" E212: Can't open file for writing

    -- kindly provide a solution , thanks in advance

    ReplyDelete
  39. cvUpdateBGStatModel() function is not working in the ubuntu 12.04LTS. opencv version is 2.3.1
    compiler doesn't return any error and BG model is NOT updating. what to do???? plz reply

    ReplyDelete
  40. How to get MJPG stream video from android IPWebcam using opencv
    I am using the IP Webcam program on android and receiving it on my PC by WiFi. What I want is to use opencv in Visual Studio, C++, to get that video stream, there is an option to get MJPG stream by the following URL: http://MyIP:port/videofeed How to get it using opencv?

    ReplyDelete
  41. Please direct questions about using OpenCV to the OpenCV mailing list. This post is about compiling and installing OpenCV.

    Thank you

    ReplyDelete
  42. Thanks alot for tutorial, very simple to do, btw i get following error when executing opencvtest:

    [----------] Global test environment tear-down
    [==========] 109 tests from 85 test cases ran. (186339 ms total)
    [ PASSED ] 108 tests.
    [ FAILED ] 1 test, listed below:
    [ FAILED ] Core_InputOutput.misc

    Can anybody help me, tried to google it but no many explanations what is this, btw, I could successfully compile facedetection girl.
    Regards Denis.

    ReplyDelete
  43. Hi Laz !
    Can you tell me how to build Opencv-2.4 on windows 7 64bituse Microsoft visual Studio 2010 ?
    Thanks!

    ReplyDelete
    Replies
    1. Hi,

      there's plenty of posts to help you on that. Do a Google search.

      Delete
  44. Hey guys!

    Could you tell me how to extract the video capture as an output? If not, how to extract the frames captured?

    Thank you very much in advanced for your help!

    ReplyDelete