Skip to content


Installing OpenCV 2.2 in Ubuntu 11.04

Many people have used my previous tutorial about installing OpenCV 2.1 in Ubuntu 9.10. In the comments of that post, I noticed great interest for using OpenCV with Python and the Intel Threading Building Blocks (TBB). Since new versions of OpenCV and Ubuntu are available, I decided to create a new post with detailed instructions for installing the latest version of OpenCV, 2.2, in the latest version of Ubuntu, 11.04, with Python and TBB support.

First, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, 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 libjpeg62-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev

Now we need to get and compile the ffmpeg source code so that video files work properly with OpenCV. This section is partially based on the method discussed here.

cd ~
wget http://ffmpeg.org/releases/ffmpeg-0.7-rc1.tar.gz
tar -xvzf ffmpeg-0.7-rc1.tar.gz
cd ffmpeg-0.7-rc1
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libxvid --enable-x11grab --enable-swscale --enable-shared
make
sudo make install

The next step is to get the OpenCV 2.2 code:

cd ~
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.2/OpenCV-2.2.0.tar.bz2
tar -xvf OpenCV-2.2.0.tar.bz2
cd OpenCV-2.2.0/

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 Python and TBB with OpenCV, here is where we set that. Just execute the following line at the console to create the appropriate Makefile. Note that there is a dot at the end of the line, it is an argument for the cmake program and it means current directory.

cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON .

Check that the above command produces no error and that in particular it reports FFMPEG as 1. If this is not the case you will not be able to read or write videos. Also, check that Python reports ON and Python numpy reports YES. Also, check that under Use TBB it says YES. If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again. You should see something similar to this:

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

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.

There is a final step to configure Python with OpenCV. You need to copy the file cv.so into the correct place. You can do that by just executing the following command:

sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so

Now you have OpenCV 2.2 installed in your computer with Python and TBB support.

Let’s check some demos included in OpenCV.
First, let’s see some C demos:

cd ~/OpenCV-2.2.0/samples/c
chmod +x build_all.sh
./build_all.sh

Some of the training data for object detection is stored in /usr/local/share/opencv/haarcascades. You need to tell OpenCV which training data to use. I will use one of the frontal face detectors available. Let’s find a face:

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

Note the scale parameter. It allows you to increase or decrease the size of the smallest object found in the image (faces in this case). Smaller numbers allows OpenCV to find smaller faces, which may lead to increasing the number of false detections. Also, the computation time needed gets larger when searching for smaller objects.

You can also detect smaller objects that are inside larger ones. For example you can search for eyes inside any detected face. You can do that with the nested-cascade parameter:

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


Feel free to experiment with other features like mouth or nose for example using the corresponding cascades provided in the haarcascades directory.

Now let’s check some C++ demos:

cd ~/OpenCV-2.2.0/samples/cpp
make

Now all the C++ demos are built in ~/OpenCV-2.2.0/bin. Let’s see a couple of them. For example, a simulated chessboard calibration:

~/OpenCV-2.2.0/bin/calibration_artificial


In OpenCV 2.2, the grabcut algorithm is provided as a C++ sample. This is a very nice segmentation algorithm that needs very little user input to segment the objects in the image. For using the demo, you need to select a rectangle of the area you want to segment. Then, hold the Control key and left click to select the background (in Blue). After that, hold the Shift key and left click to select the foreground (in Red). Then press the n key to generate the segmentation. You can press n again to continue to the next iteration of the algorithm.

~/OpenCV-2.2.0/bin/grabcut ~/OpenCV-2.2.0/samples/cpp/lena.jpg

This image shows the initial rectangle for defining the object that I want to segment.

Now I roughly set the foreground (red) and background (blue).

When you are ready, press the n key to run the grabcut algorithm. This image shows the result of the first iteration of the algorithm.

Now let’s see some background subtraction from a video. The original video shows a hand moving in front of some trees. OpenCV allows you to separate the foreground (hand) from the background (trees).

~/OpenCV-2.2.0/bin/bgfg_segm ~/OpenCV-2.2.0/samples/c/tree.avi

And finally, let’s see Python working with OpenCV:

cd ~/OpenCV-2.2.0/samples/python/

Let’s run the kmeans.py example. This script starts with randomly generated 2D points and then uses a clustering method called k-means. Each cluster is presented in a different color.

python kmeans.py

Now let’s see the convexhull.py demo. This algorithm basically calculates the smallest convex polygon that encompasses the data points.

python convexhull.py

Python scripts can also be executed directly like the following example. This script reads a video file (../c/tree.avi) within pyhton and shows the first frame on screen:

./minidemo.py


Have fun with OpenCV in C, C++ or Python…

Posted in Image Processing, Programming.


113 Responses

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

  1. Gus S. Calabrese says

    Have you also installed and used …… HighGUI ?

    thanks for all the support you have given OpenCV developers………
    99guspuppet

  2. samontab says

    Thanks… Well, HighGUI is the basic cross platform UI used in OpenCV, so yes.. I have used it… you can see it in the screenshots :)

  3. Dexter says

    THANK YOU!!! for this “how to” using Ubuntu 11.04 and OpenCV 2.2.

    I followed your instructions and now have OpenCV 2.2 library available to Python.

    I did notice I got an error with the facedetect example: “could not load cascade”

    I did verify that the specified .xml file was indeed present in the specified location.

    the other examples worked fine though.

    woo hoo!

  4. Ítalo Augusto says

    Perfect! It helped me alot! Thank you so much!

  5. Ozelui says

    Thanks a lot, I’m develop my university project with Qt and OpenCV and you gave me the solution to compile the program succefully.

  6. Iceman says

    Thanks a lot, everything done!!! but I’m trying to run some samples in Ubuntu on Code Blocks and it tells me: error while loading shared libraries: cv.so : cannot open shared object file: No such file or directory
    can anyone help, please????????!!!!!!!!

  7. samontab says

    @iceman
    It seems that you missed a couple of steps in the tutorial. Remember that you have to open a new console, or logout and login again to make it work.

    If the problem persists, please re install OpenCV carefully following the tutorial step by step and it should work.

    I just installed codeblocks to see if there is something wrong with it, but the samples worked perfectly… This means that you missed a couple of steps in the tutorial :)

  8. samontab says

    @dexter

    Great to hear that you got it working…

    About the error in the example, double check that the parameter that you are giving to the sample is correct. The only problem I can think of is that maybe the quotes got changed when you copied the command… to make sure, manually type the quote symbol ( ” ) at the beginning and end of the path…

  9. vegham says

    Thanks, this worked great on Xubuntu 11.04 with OpenCV 2.2.0.

  10. Dexter says

    I notice the cmake, make, and make install steps spew out miles and miles of information….is this captured in a log file somewhere?

    I KNOW I saw lots of warnings and errors…but I can’t read that fast.

  11. samontab says

    By default it is just written in the console. You could repeat the process and add the following to keep a file with that text:

    make > makeLog.txt

    That will write everything in the makeLog.txt file instead…. then you can read that file….

    Although if it compiled, there were no errors, just warnings…

  12. Bibek says

    Thank you…It works for me after trying a lot of other procedure :)

  13. Bibek says

    But can you please help me how can I make it work in Urbi? I am trying with ‘loadModule’ but it is not helping and always says “file not found!”

  14. Milind Padalkar says

    I had been eagerly awaiting this post. All your tutorials to install OpenCV are of great help. Thank you Sir. :-)

  15. Charan Prakash says

    Hi I am new to linux and openCV so please dont mind if the question is too trivial. I have installed openCV and tried the sample programs as you have mentioned and it is working fine. Now I have my own C++ program written using open CV. How do I compile it and get it running?

  16. samontab says

    Let’s start from the beginning then. If your C++ code is in a source file called main.cpp, you normally would do something like this to compile it:

    g++ -o myApp main.cpp

    This will produce the executable called myApp. You can then run it with this command:
    ./myApp

    To compile with the OpenCV libraries, you can do something like this:
    g++ -o myApp main.cpp `pkg-config --libs --cflags opencv`

    For larger projects or convenience, you may consider using a Makefile. After you create that file you can build your project by just typing make.

  17. Charan Prakash says

    Thank you so much that helped!!!!
    One last question. I am using a program that calling another. so is there any way I can put all these programs in one folder and and get a single excecutable file. I dont know how ta create a makefile!

  18. samontab says

    You would need the source code of the other program in order to merge both of them into a single executable.
    If you don’t have the source code, you can always call the external executable from your code using the system function. Something like this:

    #include "cstdlib"
    int main()
    {
    system("ls");
    return 0;
    }

    In the previous example, I am executing the ls program inside my program. The ls program lists the files in the current directory. So when you compile and execute that code, you will get the same result as calling ls directly. You just need to change ls with the path to your executable.

  19. Charan Prakash says

    Thank you!!

    I also have another issue. When I try to run the camshift demo code, there is an error that says,
    could not initialize capturing…
    I have a dell studio15 with a built in cam and it is working fine with skype onlinux

  20. samontab says

    That’s too specific, and I don’t have that hardware, but I’m guessing that you may need to install different drivers or libraries like video for linux (v4l2)… I can’t help you more than that though…

  21. stani says

    Hi,

    very nice howto. Did you try to install opencv2.3? What changes should be considered to get opencv2.3 running? I tried it and it endet up with an error during the “make”-step.

  22. samontab says

    A couple of days after I posted this tutorial they released the new version…

    I installed 2.3 without any problems though… maybe in the future I will update this guide to it…. before 2.4 comes out hopefully :)

  23. Dexter says

    I installed OpenCV 2.2 on my xubuntu 10.04 laptop, and THIS time all your examples work. Thee facedetect example was notable in NOT working on my Ubuntu 11.04 Desktop PC. I’ve yet to copy and try the python examples I’ve collected.

    Maybe it’s just increased awareness of what’s happening during the installation process.
    I did notice that gstreamer is required, and it seems to work better installing that with the Synaptic Package Manager. (it was already installed, but i added the -dev and -dgb packages as well this time)

    Also the config/make/install process for ffmpeg-0.7-rc1 warns me that certain things are missing. libusb0.1, libdc1394, libraw1394 and libraw1394-2

    Some of these I was able to find on the internet and install, some not.

  24. Dexter says

    I’m going back to my Ubuntu 11.04 desktop PC, retracing the steps of config/make/install
    for ffmpeg and OpenCV 2.2.

    I saved the output of the config for ffmpeg-0.7-rc1 to a text file, and
    buried in all that information I see:

    libdc1394 support no
    libopencv support no

    is this something I need to fix?

    One other question…your instructions for OpenCV 2.2 say (among other options)

    cmake -D WITH_V4L=OFF

    Why is that? My webcam is UVC, don’t I need V4L support? Or something like that?
    I gather there is something newer called V4L2. Can I use that instead/also?

  25. TheGhost says

    Thank you for your guide sebastian.

    Do the examples below use TBB automatically ?
    Or are there any special examples which make use of TBB ?

  26. samontab says

    You are very welcome.

    TBB functionality is embedded in some of the functions of the library. By just using these functions, you will be automatically using TBB. There is no need to change your code.
    If you need to know exactly which functions implement TBB, you can take a look at the source code of OpenCV and search for TBB usage.

  27. mahmud says

    Thank you. It’s work.

  28. Gama says

    Thanks a lot !! I spent a whole week trying to install OpenCv correctly until i found this guide, really great job! thanks!

  29. Viktor says

    Hello! Thank you for instruction. I have successfully installed OpenCV 2.3 on Kubuntu 11.04.
    But I have one problem. Webcam isn’t recognized in any openCV samples.
    In Skype, Cheese and other applications it works.

  30. Viktor says

    I solved my problem reinstalling OpenCV by official instruction:
    http://opencv.willowgarage.com/wiki/InstallGuide%20%3A%20Debian

  31. suji says

    Thanks for the post..I have installed opencv 2.2 in ubuntu11.04 and all the above examples work fine. But its not detecting my webcam. I am unable to use camera capture. With opencv 2.0 in ubuntu 10.10, it was working properly. Can you suggest any solution?.. and one more thing, is there any good tutorial for opencv-python?

  32. suji says

    Thank you Viktor.Just now I saw your comment, and installed opencv2.3 with the help of the link you provided. Its working with my webcam. And again, can anyone suggest some tutorial for opencv-python interface other than its documentation?

  33. Hrushikesh says

    Great post, Thanks a ton for your help and support regarding this.

    Regards,
    Hrushikesh

  34. xeon says

    can i use the same steps for installing opencv2.3 ? if not is there any good reference for installing opencv2.3 on linux?

  35. samontab says

    You should be able to install it using the same method.

  36. Eric says

    Thank suji and Viktor, I finally could use my webcam. I also thank samontab, this tutorial is perfect. But I think V4L should be on, or I cannot use my webcam.(Maybe this is my case) In the end, I’m very grateful for all your help.

  37. Gabe says

    I would like to link static libraries when compiling my OpenCV projects. What would I have to do to accomplish this?

    Thanks for the great tutorial so far

  38. Sumanth Kumar says

    thanks for sharing..i have suceessfully installed Open CV 2.3.0 with the help of this tutorial ..soon will be working on it..

  39. Cristhiant says

    I have a problem when y try to run the kmeans.py example. This is the message:

    Traceback (most recent call last):
    File “kmeans.py”, line 3, in
    import cv
    ImportError: No module named cv

    Could someone please help me?

  40. samontab says

    You missed at least one step. Try following the tutorial again step by step. I guess you missed this part:
    sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so

  41. Cristhiant says

    Thanks for your tutorial, very useful. I have successfully installed OpenCV, Thank you dude, Keep in touch, seeya…

  42. rudychip says

    thanks. It’s work. OpenCV 2.3 with Ubuntu 11.04

  43. balakrishna says

    Thanks buddy :), it’s working fine :)

  44. tylerthemiler says

    Thanks! This works in general, but for some reason, certain methods do not work:
    >>>import cv
    >>> cv.cvCreateImage
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: ‘module’ object has no attribute ‘cvCreateImage’

    Whereas:
    >>> cv.Canny

    Why do only some functions work??

    tylerthemiler

  45. samontab says

    mmm… I have read that the Python interface may be not complete yet… other than that, I do not really know…

  46. eloane says

    Great explanation. Worked fine for me.

  47. varma says

    thanks man……great work

  48. pierre-andré says

    Hi,

    Your post is very useful thanks.
    I’m asking one question, have you ever try to use OpenCV on an embedded platform? And if it the case wich one?
    I’m trying to do it on an PandaBoard and an IrisBoard but I have problems with the vidéos. When I try programs using pictures OpenCV works, but when i use vidéos, I have this warning: no accelerated colors space found for yuv to rgb.
    Do you know what dose it mean?
    I have try to use others video format, but I have the same warning.

  49. Vinod says

    I got the response to
    cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON .
    as
    - Built as dynamic libs?: YES
    – Compiler: /usr/bin/c++
    – C++ flags (Release): -Wall -pthread -march=i686 -ffunction-sections -O3 -DNDEBUG -fomit-frame-pointer -ffast-math -msse -msse2 -mfpmath=387 -DNDEBUG
    – C++ flags (Debug): -Wall -pthread -march=i686 -ffunction-sections -g -O0 -DDEBUG -D_DEBUG -ggdb3
    – Linker flags (Release):
    – Linker flags (Debug):

    – GUI:
    – GTK+ 2.x: YES
    – GThread: YES

    – Media I/O:
    – ZLib: YES
    – JPEG: TRUE
    – PNG: TRUE
    – TIFF: TRUE
    – JPEG 2000: TRUE
    – OpenEXR: YES
    – OpenNI: NO
    – OpenNI PrimeSensor Modules: NO
    – XIMEA: NO

    – Video I/O:
    – DC1394 1.x: NO
    – DC1394 2.x: YES
    – FFMPEG: YES
    – codec: YES
    – format: YES
    – util: YES
    – swscale: YES
    – gentoo-style: YES
    – GStreamer: NO
    – UniCap: NO
    – PvAPI: NO
    – V4L/V4L2: FALSE/FALSE
    – Xine: NO

    – Other third-party libraries:
    – Use IPP: NO
    – Use TBB: YES
    – Use ThreadingFramework: NO
    – Use Cuda: NO
    – Use Eigen: YES

    – Interfaces:
    – Python: YES
    – Python interpreter: /usr/bin/python2.7 -B (ver 2.7)
    – Python numpy: YES
    – Java: NO

    – Documentation:
    – Sphinx: NO
    – PdfLaTeX compiler: NO
    – Build Documentation: NO

    – Tests and samples:
    – Tests: YES
    – Performance tests: YES
    – Examples: YES

    – Install path: /usr/local

    – cvconfig.h is in: /home/vinod/Programs/OpenCV23/OpenCVSVN/opencv
    – —————————————————————–

    CMake Warning at CMakeLists.txt:1802 (message):
    The source directory is the same as binary directory. “make clean” may
    damage the source tree

    – Configuring done
    – Generating done
    – Build files have been written to: /home/vinod/Programs/OpenCV23/OpenCVSVN/opencv

    FFMPG = YES instead of 1.
    How can I correct this.

  50. samontab says

    It should work as well.

  51. Vinod says

    Ran the camshiftdemo in the bin folder.
    The following message came:
    ———————-
    This is a demo that shows mean-shift based tracking
    You select a color objects such as your face and it tracks it.
    This reads from video camera (0 by default, or the camera number the user enters
    Usage:
    ./camshiftdemo [camera number]

    Hot keys:
    ESC – quit the program
    c – stop the tracking
    b – switch to/from backprojection view
    h – show/hide object histogram
    p – pause video
    To initialize tracking, select the object with mouse

    This is a demo that shows mean-shift based tracking
    You select a color objects such as your face and it tracks it.
    This reads from video camera (0 by default, or the camera number the user enters
    Usage:
    ./camshiftdemo [camera number]

    Hot keys:
    ESC – quit the program
    c – stop the tracking
    b – switch to/from backprojection view
    h – show/hide object histogram
    p – pause video
    To initialize tracking, select the object with mouse
    ***Could not initialize capturing…***
    Current parameter’s value:
    1 [ ] ( -1 – by default) – camera number
    ————————-
    Are you able to capture video from webcam.

  52. Vinod says

    To enable video capture instead of WITH_V4L=OFF , WITH_V4L=ON must be there in the cmake variable list.

  53. Adam Saltiel says

    This is the best tutorial I have found to build OpenCV on Linux.
    The latest two versions of opencv and ffmpeg prove to work for me.

    On an Intel dual core 2.8 with no Nvidia (on board graphics) I have found the wrong combination of ffmpeg and OpenCV results in errors on make here

    [ 15%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_ffmpeg.o

    with output like this

    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

    The following works:-
    I have used
    ffmpeg-0.8.5 -
    wget http://ffmpeg.org/releases/ffmpeg-0.8.5.tar.gz
    tar -xvzf ffmpeg-0.8.5.tar.gz
    cd ffmpeg-0.8.5
    ./configure –enable-gpl –enable-version3 –enable-nonfree –enable-postproc –enable-libfaac –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libtheora –enable-libvorbis –enable-libxvid –enable-x11grab –enable-swscale –enable-shared

    and OpenCV-2.3.1a -
    wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2

    To test some paths have to be adjusted, of course.
    e.g. in the following commands, obviously -
    cd ~/OpenCV-2.2.0/samples/c
    chmod +x build_all.sh
    ./build_all.sh

    I have also not been able to locate cv.so yet (I have cv2.so) but have not test python yet, so maybe OK without following command?
    sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so

    I have the following two directories -
    /usr/local/share/opencv/samples/c
    /usr/local/share/OpenCV/haarcascades

    So the path in the test below has been altered, again obviously.
    /opencv-magic/OpenCV-2.3.1/samples/c$ ./facedetect –cascade=”/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml” –scale=1.5 lena.jpg

  54. Adam Saltiel says

    Should say, this is a fresh install of Ubuntu 11.10 alpha
    I will come back after further tests.

  55. Adam Saltiel says

    I have run through all the python tests and all OK. Seems that the line
    sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so
    is not necessary with these latest builds.

  56. mahesh says

    hi i m maheshmhegade@gmail.com .
    i m new to opencv i want to read a perticular frame from a given .avi video (say i want to read a 210th frame of “fun.avi” file) how can i read and retrive frame using cvquery function.or using if any fuction
    thank you

  57. samontab says

    There are some functions in highgui to deal with that but they don’t work for me at least…
    The easiest workaround that I have found to solve your problem is to query a frame 210 times, and return that image.
    If that is too slow for you, you can also save the individual frames as images, for example video001_frame00001.jpg, video001_frame00002.jpg, etc. That way, you can access any frame at any moment.

    highgui is only for simple video usage (mainly for playing and recording videos), if you want to do more advanced video editing, you could use ffmpeg, mencoder, mplayer, etc. Or even post processing a video with avidemux for example…

  58. Tj.Wallas_ says

    Thank you so much!
    Very useful tutorial.

  59. Andrew says

    Thanks for the great tutorial. It worked well. I’ve recently reinstalled the Ubuntu and went with the newest version (11.10). While trying to build OpenCV 2.3.1, I get the message during linking:

    Linking CXX shared library ../../lib/libopencv_highgui.so
    /usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet’ can not be used when making a shared object; recompile with -fPIC
    /usr/local/lib/libavcodec.a: could not read symbols: Bad value
    collect2: ld returned 1 exit status
    make[2]: *** [lib/libopencv_highgui.so.2.3.1] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make: *** [all] Error 2

    Do you have any tips to resolve it? I’m on x64 Intel and x64 Ubuntu, if that makes a difference.

  60. Jeremy says

    Thank you so much! I went through so much pain and anguish trying to get openCV on my machine. This tutorial helped a bunch!

  61. NewtoOpencv says

    HELP.!
    I got error for proceed until the step
    “Now, you are ready to compile and install OpenCV 2.2:
    make
    sudo make install

    I follow all the step above and works very well. But there are error after i type “make”.

    /usr/include/c++/4.6/typeinfo:42:37: error: expected ‘}’ before end of line
    /usr/include/c++/4.6/typeinfo:42:37: error: expected declaration before end of line
    make[2]: *** [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.o] Error 1
    make[1]: *** [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/all] Error 2
    make: *** [all] Error 2

    How can i fix it? Thanks!

  62. samontab says

    Hard to tell… it could be many things…

    I recommend you to re install the OS and start from scratch following every line of the tutorial. It is the easiest way to make it work without a problem.

  63. NewtoOpencv says

    I tried several times…

    I’m using ubuntu 11.10. It is cause the problem?

    After i type make the output is

    [ 2%] Building CXX object modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.o
    In file included from /home/gp2/OpenCV-2.2.0/modules/core/src/precomp.hpp:55:0,
    from /home/gp2/OpenCV-2.2.0/modules/core/opencv_core_pch_dephelp.cxx:1:
    /home/gp2/OpenCV-2.2.0/modules/core/include/opencv2/core/core.hpp:354:13: error: ‘ptrdiff_t’ does not name a type

    Thanks for help again!

  64. samontab says

    It may be the problem.

    If you use another OS, the libraries and programs that are installed may be different. They may have different dependencies, or even work differently.
    In general, if you use an OS with small differences, like 11.04 and 11.10, it should be OK to follow the same tutorial but with small changes. The problem in that scenario is that you need to know what you are doing, and not follow the steps blindly.
    If you just need to get it working ASAP, install 11.04 from scratch and follow this tutorial.

  65. MartinSt says

    Hi to all.
    @NewtoOpencv
    please let yourself visit this link:
    https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/791527

    if I’d understood, we discuss about problems with newer g++ version?

  66. ton says

    this is a great tutorial … and very much thankfull for your knowledge sharing .. keep it up buddy.

  67. andol says

    i compiled the opencv using this tutorial, very useful
    but is there any update for newer versions of opencv?

  68. samontab says

    You may need to change some bits here and there, but if you know what you are doing, you should be able to install any newer openCV version based on this tutorial.

  69. Samarth says

    hi,
    great blog! I read in the ‘about’ section that you also work on IP cameras. Could you guide me how to capture a MJPEG stream from an IP camera to OpenCV?
    Thanks!

  70. samontab says

    Hello,

    I usually create a function, like getFrame(), that returns an OpenCV image and deals with everything I need to do to get an image from the connected camera. This can be just OpenCV highgui functions for webcams or more specific code for IP cameras. Then, I just use that function on the main loop of the program.
    That way, I can easily change the camera to another one and the program still works the same.

  71. Jigar says

    Hey thank you for your great help regarding installation of OpenCv 2.2 on ubuntu 11.04

  72. rupesh says

    how to install and make it working for c and c++ not for python….

  73. samontab says

    The post explains how to install the library for those three languages. Once you install it you can use it with C, C++ and Python.

  74. abyss says

    Hi, this is a extremely prefect tutorial I never saw before.But I have a small problem with the “TBB”. I use the function “cmake -D WITH_TBB=ON .”, but the TBB still with ‘no’ state. Is that my 11.04 ubuntu caused by this?.

  75. samontab says

    Make sure that you have correctly installed the TBB development library before you compile opencv

  76. Anon says

    For those having problems following this on Ubuntu 11.10:

    If you are getting errors on making OpenCV then I believe the fix is to make a small modification to the includes for cxcore.hpp however I think the easier solution is to install OpenCV-2.3.0 rather than OpenCV-2.2.0.

    I followed this guide exactly except replacing 2.2.0 with 2.3.0 and everything seems fine.

  77. Adrian Carrio says

    I wanted to have OpenCV 2.2 or later in Linuxmint. This post helped me to compile ffmpeg-0.8.5 and OpenCV-2.3.1a under Linuxmint 11, although I could not make it work for Linuxmint 12. Thanks to both Sebastian Montabone and Adam Saltiel.

  78. Josh says

    @samontab

    Dude.. You saved my day. Thank u very much.. :)

  79. samontab says

    Glad I could help :)

  80. vivek87799 says

    hai i tried installing opencv and got some errore can u please help

    [ 49%] Built target opencv_lapack
    [ 49%] Building CXX object src/cxcore/CMakeFiles/cxcore_pch_dephelp.dir/cxcore_pch_dephelp.o
    In file included from /home/vivek/OpenCV-2.0.0/include/opencv/cxcore.h:2123:0,
    from /home/vivek/OpenCV-2.0.0/src/cxcore/_cxcore.h:60,
    from /home/vivek/OpenCV-2.0.0/release/src/cxcore/cxcore_pch_dephelp.cxx:1:
    /home/vivek/OpenCV-2.0.0/include/opencv/cxcore.hpp:169:13: error: ‘ptrdiff_t’ does not name a type
    In file included from /home/vivek/OpenCV-2.0.0/include/opencv/cxcore.hpp:2243:0,
    from /home/vivek/OpenCV-2.0.0/include/opencv/cxcore.h:2123,
    from /home/vivek/OpenCV-2.0.0/src/cxcore/_cxcore.h:60,
    from /home/vivek/OpenCV-2.0.0/release/src/cxcore/cxcore_pch_dephelp.cxx:1:
    /home/vivek/OpenCV-2.0.0/include/opencv/cxoperations.hpp:1916:15: error: ‘ptrdiff_t’ does not name a type
    /home/vivek/OpenCV-2.0.0/include/opencv/cxoperations.hpp:2465:31: error: ‘ptrdiff_t’ does not name a type
    In file included from /home/vivek/OpenCV-2.0.0/include/opencv/cxcore.hpp:2244:0,
    from /home/vivek/OpenCV-2.0.0/include/opencv/cxcore.h:2123,
    from /home/vivek/OpenCV-2.0.0/src/cxcore/_cxcore.h:60,
    from /home/vivek/OpenCV-2.0.0/release/src/cxcore/cxcore_pch_dephelp.cxx:1:
    /home/vivek/OpenCV-2.0.0/include/opencv/cxmat.hpp: In member function ‘void cv::Mat::locateROI(cv::Size&, cv::Point&) const’:
    /home/vivek/OpenCV-2.0.0/include/opencv/cxmat.hpp:356:5: error: ‘ptrdiff_t’ was not declared in this scope
    /home/vivek/OpenCV-2.0.0/include/opencv/cxmat.hpp:356:5: note: suggested alternatives:
    /usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h:156:28: note: ‘std::ptrdiff_t’
    /usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h:156:28: note: ‘std::ptrdiff_t’
    /home/vivek/OpenCV-2.0.0/include/opencv/cxmat.hpp:356:15: error: expected ‘;’ before ‘delta1’
    /home/vivek/OpenCV-2.0.0/include/opencv/cxmat.hpp:358:9: error: ‘delta1’ was not declared in this scope
    /home/vivek/OpenCV-2.0.0/include/opencv/cxmat.hpp:367:31: error: ‘delta2’ was not declared in this scope
    make[2]: *** [src/cxcore/CMakeFiles/cxcore_pch_dephelp.dir/cxcore_pch_dephelp.o] Error 1
    make[1]: *** [src/cxcore/CMakeFiles/cxcore_pch_dephelp.dir/all] Error 2
    make: *** [all] Error 2

  81. samontab says

    it seems that you are installing a different version, 2.0. Try again following the steps carefully and it should work.

  82. rkprabhu says

    ne idea how to get opencv n python installed in fedora11???

  83. kimi says

    hi, thank you for the tutorial, i want to ask you please, how to build gpu projects (samples hog for example)? i use ubuntu 11.10 and Opencv 2.3.1.
    thank you

  84. Arturo says

    I got the following error message as I tried to install OpenCV 2.3.1 on Ubuntu 12.04 :

    The following packages have unmet dependencies:
    libjpeg-turbo8-dev : Conflicts: libjpeg62-dev but 6b1-2ubuntu1 is to be installed
    E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.

    This was after typing in the first sudo pat-get install build-essential ….

  85. pavan says

    I got this while making opencv2.2

    Linking CXX executable ../../bin/opencv_createsamples
    ../../lib/libopencv_highgui.so.2.2.0: undefined reference to `cvCreateCameraCapture_V4L(int)’
    collect2: ld returned 1 exit status
    make[2]: *** [bin/opencv_createsamples] Error 1
    make[1]: *** [modules/haartraining/CMakeFiles/opencv_createsamples.dir/all] Error 2
    make: *** [all] Error 2

    plz help me

  86. pavan says

    Linking CXX executable ../../bin/opencv_createsamples
    ../../lib/libopencv_highgui.so.2.2.0: undefined reference to `cvCreateCameraCapture_V4L(int)’
    collect2: ld returned 1 exit status
    make[2]: *** [bin/opencv_createsamples] Error 1
    make[1]: *** [modules/haartraining/CMakeFiles/opencv_createsamples.dir/all] Error 2
    make: *** [all] Error 2

  87. pavan says

    root@pavan:/home/pavan/OpenCV-2.2.0/samples/c# ./facedetect –cascade=”/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml” –nested-cascade=”/usr/local/share/opencv/haarcascades/haarcascade_eye.xml”

    This program demonstrates the haar cascade recognizer
    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 %s
    2.2.0

    Processing 1 –cascade=/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml
    from which we have cascadeName= /usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml
    Processing 2 –nested-cascade=/usr/local/share/opencv/haarcascades/haarcascade_eye.xml
    Capture from CAM 0 didn’t work
    In image read

    help me…!

  88. xeon says

    Hello has anyone ever successfully compiled opencv2.3.1 library on linux with cuda enabled? i downloaded the cuda toolkit 4.1and the nvidia driver as well, however compilation fails at 87% . I dont really know how to proceed at this point. I would highly appreciate any advice. thak you.

    below is the error message :

    [ 87%] Building CXX object modules/gpu/CMakeFiles/opencv_gpu.dir/src/element_operations.o
    /home/sayid/OpenCV-2.3.1/modules/gpu/src/element_operations.cpp: In function ‘void cv::gpu::absdiff(const cv::gpu::GpuMat&, const cv::gpu::GpuMat&, cv::gpu::GpuMat&, cv::gpu::Stream&)’:
    /home/sayid/OpenCV-2.3.1/modules/gpu/src/element_operations.cpp:298: error: ‘nppiAbsDiff_8u_C4R’ was not declared in this scope
    /home/sayid/OpenCV-2.3.1/modules/gpu/src/element_operations.cpp:302: error: ‘nppiAbsDiff_32s_C1R’ was not declared in this scope
    make[2]: *** [modules/gpu/CMakeFiles/opencv_gpu.dir/src/element_operations.o] Error 1
    make[1]: *** [modules/gpu/CMakeFiles/opencv_gpu.dir/all] Error 2
    make: *** [all] Error 2

  89. m says

    HI
    I’ve installed opencv, When I use “pkg-config –libs opencv” in terminal I get
    “/usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so ”
    rather than ”
    -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -
    lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib
    -lopencv_legacy -lopencv_flann

    Do you know whats the problem?

  90. Christian says

    Hellow, thank’s for the tutorial, but I have a little problem with the library. When I try to capture images from my webcam, I just can’t do it. I’m using the cvCaptureFromCAM function and cvCreateCameraCapture, but the device doesn’t work. I think that the problem is from the instalation, any idea?

    Thank you again…

  91. samontab says

    You can try with -D WITH_V4L=ON on the make procedure.

    (V4L stands for Video for Linux)

  92. Christian says

    Quite nice! This instruction solved mi problem, besides this package apt-get install libv4l-0 libv4l-dev. Thank you once again…

  93. Lukas says

    I can only agree with the many people above:

    Thank you!

  94. samontab says

    I’m glad that I can help :)

  95. STuDent says

    Thank for the information in this blog. I’m a student trying to install OpenCV on Ubunto 11.04
    But I’m stuck because make process in OpenCV build isn’t working.
    Error comment is like this.
    Linking C static library ../lib/libzlib.a
    [ 3%] Building CXX…. blarblar..
    /root/OpenCV-2.2.0/modules/core/include/opencv2/core/core.hpp::354:13:error:’ptrdiff_t” does not name a type…..

    Please reply ㅠ_ㅠ

  96. STuDent says

    Oh.. I miss somthing..
    I mistook that I erased the directory ‘ root/ffmpeg’
    Is this the reason OpenCV build isn’t working??

  97. samontab says

    It is hard to say, but given the output you are showing me, I can tell that you are not following the instructions exactly as they are because I never used a root directory.

    Try following more carefully the instructions and it should work.

  98. Shivangi says

    Hello, your tutorial helped a lot. But i am not able to understand how to link two opencv files as i have to create a big project. The makefile also produces error

  99. samontab says

    Hi,

    You should make libraries instead of executables on each project and then link them together to get your final executable, using both projects.
    You can do that manually or editing the cmake files.

Continuing the Discussion

  1. Installing OpenCV 2.1 in Ubuntu – Sebastian Montabone linked to this post on June 17, 2011

    [...] UPDATE: Install OpenCV 2.2 in Ubuntu 11.04 with Python and TBB support here. [...]

  2. Tutorial para configurar a biblioteca OpenCV no compilador CodeBlocks Linux – Ubuntu 10.10 « Guilherme P. Cardim linked to this post on June 27, 2011

    [...] http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/ Publicado em Tudo, Tutoriais. Deixar um comentário » LikeBe the first to like this post. [...]

  3. OpenCV 2.2 with Natty , make problems | Coders & Admins linked to this post on July 8, 2011

    [...] I’m new with Linux and Ubuntu .I don’t if this is the correct section for this thread. I need some help with making OpenCV 2.2 , I’m using this guide to install it : http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/ [...]

  4. Playing video with OpenCV 2.2, Python, and wxWidgets « Brett Walenz linked to this post on August 12, 2011

    [...] For installing these things on Ubuntu 11.04, you can follow the guide. [...]

  5. OpenCV on Ubuntu 11.04 | 累積の奧義 linked to this post on August 20, 2011

    [...] samples 目錄裡的程式了。 要如何測試,請看下面這2篇,也是很NICE的文章: http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/ [...]

  6. voidnoise.co.uk » Ubuntu 11.04 & OpenCV 2.2 linked to this post on August 26, 2011

    [...] for opencv 2.2 to install hassle free. Sabastian Monabone explains the process better than I could here. Written by voidnoise in: Uncategorized | Tags: OpenCV, [...]

  7. Installing OpenCV 2.2 (and above?) in Ubuntu 11.04 x86-64 « Mr. Barry's Tech Blog linked to this post on August 29, 2011

    [...] guide, but it didn't work for me in Ubuntu 11.04 x86-64.  Well, he had posted another guide for OpenCV 2.2 in Ubuntu 11.04, which seemed to work for me, with a few minor [...]

  8. Installing and configuring OpenCV 1.1 Eclipse on ubuntu 10.04 and higher » Tj.Wallas | Zone.X. - Tech-Reviews-Blog linked to this post on October 19, 2011

    Installing and configuring OpenCV 1.1 Eclipse on ubuntu 10.04 and higher…

    During our computer vision course this semester we were strictly required to use OpenCV version 1.1 in our programming assignments. Since this is a very outdated version, there are some problems with ……

  9. Tutorials by garag - Pearltrees linked to this post on December 7, 2011

    [...] Installing OpenCV 2.2 in Ubuntu 11.04 – Sebastian Montabone [...]

  10. Installing OpenCV 2.3.1 on Ubuntu 11.10 « Ryan Hartman's Blog linked to this post on December 13, 2011

    [...] can install OpenCV (Open Source Computer Vision) on Ubuntu 11.10.  I based this guide off of a post by Sebastian [...]

  11. ubuntu 10.04 opencv ffmpeg « Lost Ferry linked to this post on December 31, 2011

    [...] just follow this guide [...]

  12. install ffmpeg and opencv 2.3.1 | iDetect Technologies linked to this post on January 9, 2012

    [...] is how you can install OpenCV  on Ubuntu 11.10.  I based this guide off of a post by Sebastian Montabone and another post from Osman Eralp. Step 1: We need to install all of the [...]

  13. The pain of having a 64 bit Linux laptop and OpenCV | In my humble opinion… linked to this post on February 8, 2012

    [...] libraries by myself. But that’s not alwasy as easy as it looks like. After try, nor one, two, three, four, but six different methods, I got OpenCV libraries working on my machine with Python [...]

  14. OpenCV + Ubuntu | MitchTech linked to this post on March 21, 2012

    [...] is largely based on the wonderful OpenCV tutorials from Sebastian Montabone and Osman Eralp. Before you start, make sure to have removed ffmpeg and x264 (if [...]



Some HTML is OK

or, reply to this post via trackback.