Wednesday, May 16, 2012

OpenCV 2.4 on Ubuntu 12.04

Edit (August 2013): Just used these steps to install OpenCV 2.4.6.1 on Ubuntu 12.10 without problems.

OpenCV 2.4.0 was released on May 2012 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:



116 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. Hi, it is possible install opencv 2.4 in ubuntu 11.04 with the above steps? thanks.

    ReplyDelete
  10. 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
    Replies
    1. "Then I added (at the LIBS +=) -L/usr/local/lib/" how you did that. Can you please elaborate more I am a beginner and got the same problem.

      Delete
    2. did you solve the issue? Because I have the same error?

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

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

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

      Delete
  13. thanks Laz...

    Great and clear tutorial...

    ReplyDelete
  14. Great and simple tutorial
    thanks LAZ

    ReplyDelete
  15. 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
  16. Yoo !

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

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

    ReplyDelete
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. Please,tell me your method:Use opencv and qt everywhere to build your project for KIT ARM (example: friendlyARM mini2440,tiny210...).Thanks

    ReplyDelete
  24. 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
  25. Thank you very much for the post!! I had no problems installing opencv following this guide.

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

    ReplyDelete
  27. 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
  28. This comment has been removed by the author.

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

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

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

    Kindly Help

    ReplyDelete
  31. @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
  32. 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
  33. 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
  34. 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
  35. /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
  36. 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
  37. 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
  38. 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
  39. 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
  40. Please direct questions about using OpenCV to the OpenCV mailing list. This post is about compiling and installing OpenCV.

    Thank you

    ReplyDelete
  41. 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
  42. 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
  43. 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
  44. Thanks for the tutorial, finally it's work :)
    but, if i wanna use opencv on codeblocks, how is the step? i have already trying the tutorial from opencv site, its not work properly, do you have any idea?
    and
    if i want to remove/uninstall all installed program even all the library, how is the command? my friend ask it to me.

    thanks 4 adv

    ReplyDelete
    Replies
    1. and 1 more, i'm newbie in opencv, i have tried to make a simple program like this (copy from site) :
      #include
      #include
      int main() {

      cv::Mat image = cv::imread("image.jpg");
      cv::namedWindow("test one");
      cv::imshow("test one", image);
      cv::waitKey(500);
      return 1;
      }
      then i call it using :
      g++ -o tes tes.cpp 'pkg-config opencv --cflags --libs'

      why it show error : no such file or directory? am i wrong? please give me solution..

      thanks :)

      Delete
  45. Hi, thank you for the instructions. When I get to ./opencv_test_core, I just get "Illegal instruction (core dumped). This is with opencv-2.4.5 on Ubuntu 12.10. Could you help me and tell me what I might be doing wrong? thank you!

    ReplyDelete
  46. All went well including executing your program. But I couldn't get the examples in /samples/c and samples/cpp to compile. I thought going to /samples/c and typing ./build.sh would do. But all I got was a series of undefined references (Ex: "undefined reference to cv::_OutputArray::_OutputArray(cv::Mat&)').
    I note the following:
    "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"
    How should I do this?

    ReplyDelete
  47. Laz, great tutorial.

    When i try and build and run a file with :
    g++ 'pkg-config opencv --cflags' FILE.cpp -o FILE 'pkg-config opencv --libs'

    i get an error of:
    pkg-config opencv --cflags : no such file or directory
    pkg-config opencv --libs : no such file or directory

    any suggestions? I have checked my bash.bashrc and my opencv.pc and opencv.conf files. They all seem to point to the correct locations.

    Thanks,
    Eric

    ReplyDelete
  48. Thank you Sir!

    I have been working on getting OpenCV working for 4 days with no success until I found your tutorial!

    Here let me buy you an internet beer!

    Thanks A TON!!

    ReplyDelete
  49. This seems to install 32bits libraries. How about 64 libraries? Or will the 32 bits libraries be fine (no conflict with other libs) on a 64 bits linux?

    ReplyDelete
  50. Great contribution very very thanks!!!

    ReplyDelete
  51. my installation and building was ok and i got the test result like the following:
    [==========] 109 tests from 85 test cases ran. (102235 ms total)
    [ PASSED ] 109 tests.

    My problem is to run your sample code. I got the followings errors:

    g++ faceDetect.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -o faceDetect
    /usr/bin/ld: error: cannot find -lopencv_core
    /usr/bin/ld: error: cannot find -lopencv_imgproc
    /usr/bin/ld: error: cannot find -lopencv_highgui
    /usr/bin/ld: error: cannot find -lopencv_objdetect

    thanks in advance

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

    ReplyDelete
    Replies
    1. Hey Groby,

      I am not sure what your problem is. Can you open windows in general (maybe you are ssh'ed in, without X forwarding). Can you test the QT4 installed properly? You could install the demos:

      sudo apt-get install qt4-demos

      and run:

      qt-demo

      see if QT is working at all. Also, try compiling and installing again using GTK. This way at least you will know what the problem is.

      -- Laz

      Delete
    2. well, thanks Lazaros, you're right. I execute it in ssh before. But after I installed qt4-demos. The command “qt-demo” seems doesn't work however. It tells me:
      -bash: qt-demo: command not found

      And I tried to run the examples in folder "samples" in source code.However It can run normally and display the picture. So It explains the qt has installed successfully.

      When I running in general windows, some errors shown below:
      pi@raspberrypi ~/opencv_samples/faceDetect_sample $ ./faceDetect

      This program demonstrates the cascade recognizer. Now you can use Haar or LBP features.
      This classifier can recognize many ~rigid objects, it's most known use is for faces.
      Usage:
      ./facedetect [--cascade= this is the primary trained classifier such as frontal face]
      [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]
      [--scale=
      [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
      Hit any key to quit.
      Using OpenCV version 2.4.0

      ERROR: Could not load classifier cascade
      Usage: facedetect [--cascade=]
      [--nested-cascade[=nested_cascade_path]]
      [--scale[=
      [filename|camera_index]

      what I should do next? Thanks!

      Delete
  53. Hi,

    I followed the steps and tried to run the sample code. But it gives me error saying some function is nto declared in this scope. Like the following:

    ‘cvCaptureFromCAM’ was not declared in this scope
    faceDetect.cpp:100:59: error: ‘cvCaptureFromAVI’ was not declared in this scope
    faceDetect.cpp:110:32: error: ‘cvNamedWindow’ was not declared in this scope
    faceDetect.cpp:118:13: error: ‘IplImage’ was not declared in this scope
    faceDetect.cpp:118:23: error: ‘iplImg’ was not declared in this scope
    faceDetect.cpp:118:54: error: ‘cvQueryFrame’ was not declared in this scope
    faceDetect.cpp:122:35: error: ‘IPL_ORIGIN_TL’ was not declared in this scope
    faceDetect.cpp:136:36: error: ‘cvReleaseCapture’ was not declared in this scope

    There are many more. Could you help me why I am receiving this error? has anyone got this type of error? What is the solution?

    ReplyDelete
  54. do we have to give the image as input or it is generated automaticaly?

    ReplyDelete
    Replies
    1. You have to give it as an input. It's provided in the zip file up there.

      Delete
  55. Worked like a charm! Thank you very much Laz!

    ReplyDelete
  56. Thank You so much, very perfect tutorial...

    ReplyDelete
  57. I got this error while make [ 3%] Built target opencv_core
    [ 4%] Built target opencv_ts
    [ 10%] Built target opencv_imgproc
    [ 14%] Built target opencv_highgui
    [ 17%] Built target opencv_perf_core
    [ 19%] Built target opencv_test_core
    Linking CXX executable ../../bin/opencv_perf_imgproc
    /usr/lib/libxine.so.1: undefined reference to `gzopen64@ZLIB_1.2.3.3'
    collect2: 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

    ReplyDelete
  58. Thanks you for this tutorial!!!. It was really useful to me!.

    Best Regards from Argentina!

    ReplyDelete
  59. Made a script to automatically download/configure/compile/install OpenCV: https://gist.github.com/anonymous/08ed8d6a5fb5c192e0da just save and execute.

    ReplyDelete
  60. hi i am using 2.4.7 .i have following error

    ./faceDetect --cascade="haarcascade_frontalface_alt.xml" --nested-cascade="haarcascade_eye.xml" lena.jpg

    This program demonstrates the cascade recognizer. Now you can use Haar or LBP features.
    This classifier can recognize many ~rigid objects, it's most known use is for faces.
    Usage:
    ./facedetect [--cascade= this is the primary trained classifier such as frontal face]
    [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]
    [--scale=
    [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
    Hit any key to quit.
    Using OpenCV version 2.4.7

    Processing 1 --cascade=haarcascade_frontalface_alt.xml
    from which we have cascadeName= haarcascade_frontalface_alt.xml
    Processing 2 --nested-cascade=haarcascade_eye.xml
    Processing 3 lena.jpg
    before named window
    init done
    opengl support available
    after
    In image read
    Before detectanddraw
    bf detect multi
    after
    detection time = 198.554 ms
    Segmentation fault (core dumped)
    make: *** [run] Error 139

    ReplyDelete
  61. thanks man i am struggling from morning on it.....realy very nice tutorial.. :)

    ReplyDelete
  62. Thank you very much Laz..
    It is really a useful article.. Truely speaking, I was trying to install opencv in Ubuntu for more than 10 days but each time I failed.. Then found your tutorial and I am done with installation without any problem,, :)
    Can you explain how to configure OpenCV with CodeBlocks? Waiting for your response..
    Thanks.

    ReplyDelete
    Replies
    1. Hey Sorath, I'm happy the tutorial was useful. I've never used CodeBlocks, but I'm sure you could find a nice walkthrough.

      Delete
  63. have a problem with the CMake part of your instructable
    when i enter your configuration details i get this fault
    victor@victor-desktop:~/OpenCV-2.4.0/build$ cmake -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..
    CMake Error: The source directory "/home/victor" does not appear to contain CMakeLists.txt.
    Specify --help for usage, or press the help button on the CMake GUI.
    victor@victor-desktop:~/OpenCV-2.4.0/build$

    how can i make cmake look for the make list in the correct directory

    ReplyDelete
  64. Lazaros
    I have cracked my problem and all is working now i have saved your web page for future reference hope you don't mind. it a good tutorial you should do some more for configuring codeblocks IDE
    thanks AussiVic

    ReplyDelete
  65. hi....i m new to ubuntu and my project is based on opencv and i need to install opencv for it and i installed ubuntu 12.04lts few days back is their a need to install packages for it and what should i do to install opencv installation and if i need to download packages how can get those packages like cmake git,...etc..cto the terminal???can anyone help me.......

    ReplyDelete
    Replies
    1. Hi Paddu,

      this walkthrough is exactly about that. It guides you through installing dependencies and then compiling and installing OpenCV. Give it a try

      Delete
  66. how to do make install without psuedo permission coz when i gave just make install i got the error which stated " CMake Error at cmake_install.cmake:36 (FILE):
    file cannot create directory: /usr/local/include/opencv2. Maybe need
    administrative privileges."

    ReplyDelete
    Replies
    1. You need sudo because you don't have permission to write to the default installation directory otherwise. You could install as normal user if you changed the installation path. See this: http://goo.gl/WuE6JH

      Delete
  67. After succesfully installing, can I delete the source folder, ie the folder that initially contained the files for installation/
    Since, it occupies nearly 1.5GB of space.

    Other than the samples, are any others in the source folder, important ?

    ReplyDelete
  68. Yes, you can delete the source.

    ReplyDelete
  69. Best guide I have been through many guides trying to install opencv, this is hands downs the best, thanks a million.

    Regards
    Chris

    ReplyDelete
  70. How to resolve the below error.......am tried to test the sample face detection program....many thanks for the document and help.

    user@cu-cs-vm:~/OpenCV-2.4.0/build/bin/faceDetect_sample$ make
    g++ faceDetect.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -o faceDetect
    ./faceDetect --cascade="haarcascade_frontalface_alt.xml" --nested-cascade="haarcascade_eye.xml" lena.jpg

    This program demonstrates the cascade recognizer. Now you can use Haar or LBP features.
    This classifier can recognize many ~rigid objects, it's most known use is for faces.
    Usage:
    ./facedetect [--cascade= this is the primary trained classifier such as frontal face]
    [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]
    [--scale=
    [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
    Hit any key to quit.
    Using OpenCV version 2.4.0

    Processing 1 --cascade=haarcascade_frontalface_alt.xml
    from which we have cascadeName= haarcascade_frontalface_alt.xml
    Processing 2 --nested-cascade=haarcascade_eye.xml
    Processing 3 lena.jpg
    before named window
    OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file /home/user/OpenCV-2.4.0/modules/highgui/src/window.cpp, line 598
    terminate called after throwing an instance of 'cv::Exception'
    what(): /home/user/OpenCV-2.4.0/modules/highgui/src/window.cpp:598: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow

    Aborted (core dumped)
    make: *** [run] Error 134

    ReplyDelete
    Replies
    1. Hi Nilendra,

      did you try to do what the message suggests?

      "...install libgtk2.0-dev and pkg-config, then re-run cmake or configure script."

      Delete
  71. Dude, hi!

    I'm trying to install OpenCV 2.4.9 on Ubuntu 14.04

    I downloaded the compressed file,
    created a /release dir in opencv- folder, then ran

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D MAKE_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_OPENGL=ON ..

    And I even tried the above with the 'BUILD_NEW_PYTHON_SUPPORT' changed to 'BUILD_opencv_python' as stated here: http://answers.opencv.org/question/10651/cv2so-missing-after-opencv-installed/

    And I added /usr/local/lib to /etc/ld.so.conf
    and then I ran ldconfig

    But still in Python, import cv2 doesn't work - "cv2 not found"

    Ultimately I had to do
    sudo apt-get install python-opencv
    and then that installed opencv 2.4.8 seamlessly.


    ReplyDelete
  72. But what I was asking was, I want to install the latest version. And 2.4.9 isn't working in Python. Its opencv_test_core test works fine. But only Python says it can't find cv2 module

    ReplyDelete
    Replies
    1. Hi ronnie,

      I haven't looked at ubuntu 14.04 yet. I'm planning to see what's changed and update this post within a few days

      Delete
    2. Hi, any update on the Ubuntu 14.05 cv2 issue?

      Delete
  73. Hi nice tutorial! But i am having a probelm when i get to the stage where i type 'make'- i get the error - make: *** No targets specified and no makefile found. Stop.

    Help!

    Cheers

    steve

    ReplyDelete
    Replies
    1. That means you didn't run cmake or it didn't succeed

      Delete
  74. I've done everything from this tutorial, but I don't know how to add openCV to my netbeans project. On Windows netbeans the solution was to add "-Djava.library.path="/home/MyUserName/opencv-2.4.9" to "properties->run"1

    But on ubuntu 14.04 that doesn't help. Plz help me.

    ReplyDelete
  75. HELP! SOURCE DIRECTORY DOES NOT APPEAR TO CONTAIN CMakeLists.txt !!!


    Hi i'm having trouble with the installation. I'm using Ubuntu 12.04.
    I reached the Compile And Install step.

    Note: tar -xvf OpenCV-2.4.0.tar.bz2 didnt work for me. It said Cannot open: No such file or directory. Yes I had downloaded it. But it was in the downloads folder. So I manually extracted it from there.

    Now my problem is..I get the error Cmakelists.txt couldn't be found.

    clive@clive-Aspire-4755:~$ cd OpenCV-2.4.0/
    clive@clive-Aspire-4755:~/OpenCV-2.4.0$ cd build
    clive@clive-Aspire-4755:~/OpenCV-2.4.0/build$ cmake -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..
    CMake Error: The source directory "/home/clive" does not appear to contain CMakeLists.txt.
    Specify --help for usage, or press the help button on the CMake GUI.


    Can anyone help me? I tried changing directories too.

    ReplyDelete
  76. In that above testing i got
    [----------] Global test environment tear-down
    [==========] 126 tests from 95 test cases ran. (96713 ms total)
    [ PASSED ] 125 tests.
    [ FAILED ] 1 test, listed below:
    [ FAILED ] Core_globbing.accuracy

    1 FAILED TEST

    ReplyDelete
  77. Hello!
    This guide is about installing the OpenCV 2.4
    I want to install the new version i.e. OpenCV 2.4.8 on Ubuntu. Will these steps work for the newer version?
    If anyone tried, then please help.
    Thanks.

    ReplyDelete
    Replies
    1. Most likely yes. OpenCV dependencies don't change that much between minor versions. But follow this guide instead, which has been tested with v.2.4.9:

      http://karytech.blogspot.com/2014/05/opencv-249-on-ubuntu-1404.html

      Delete
    2. That's Great Mr. Laz!
      Your work is really very highly appreciated. Your tutorials are proving to be a road map for the beginners. Keep sharing!
      Stay Blessed. :)
      One more thing I want to know, is Ubuntu 14.04 a stable version? Can i use it instead of 12.04?
      Thanks.

      Delete
    3. Thank you very much, Sorath.

      > One more thing I want to know, is Ubuntu 14.04 a stable version?
      Yes, it's a stable and long term support (LTS) release so you should use it. Follow the updated guide here:
      http://karytech.blogspot.com/2014/05/opencv-249-on-ubuntu-1404.html

      Delete
    4. Thank You very much for your prompt response Laz!
      I have to use OpenCV over Odroid Microcomputer, which uses XUbuntu 13.10. I am familiar with Ubuntu but not with XUbuntu. I have also googled about Installing the OpenCV over XUbuntu but I have not found any available tutorial. Can I use this one? I am not sure will this work..
      Can You answer my question Laz?
      Thanks.

      Delete
  78. Really helpful page, thanks a lot!

    ReplyDelete
  79. Hi i am getting some error while executing facedetect package to test the compilation and run but after running make following error came
    faceDetect.cpp:53:74: error: no matching function for call to ‘cv::String::compare(int, size_t&, const char*&, size_t&) const’
    if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
    ^
    In file included from /usr/local/include/opencv2/core/base.hpp:56:0,
    from /usr/local/include/opencv2/core.hpp:54,
    from /usr/local/include/opencv2/objdetect.hpp:47,
    from /usr/local/include/opencv2/objdetect/objdetect.hpp:48,
    from faceDetect.cpp:1:
    /usr/local/include/opencv2/core/cvstd.hpp:783:5: note: candidate: int cv::String::compare(const char*) const
    int String::compare(const char* s) const
    ^
    /usr/local/include/opencv2/core/cvstd.hpp:783:5: note: candidate expects 1 argument, 4 provided
    /usr/local/include/opencv2/core/cvstd.hpp:790:5: note: candidate: int cv::String::compare(const cv::String&) const
    int String::compare(const String& str) const
    ^
    /usr/local/include/opencv2/core/cvstd.hpp:790:5: note: candidate expects 1 argument, 4 provided
    faceDetect.cpp:55:25: error: ‘class cv::String’ has no member named ‘assign’



    ReplyDelete