OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis.
OpenCV 1.0 can be easily installed in Ubuntu via the repositories. You can install OpenCV 2.0 by following one of my previous posts http://www.samontab.com/web/2010/03/installing-opencv-2-0-in-ubuntu/
Unfortunately, the newer version of OpenCV, 2.1, which was released on April has a slightly different installation procedure. Since it contains many bug fixes and some nice new additions, I will show you how to install it.
UPDATE: Install OpenCV 2.2 in Ubuntu 11.04 with Python and TBB support here.
Here are the steps that I used to successfully install OpenCV 2.1 in Ubuntu 9.10. I have used this procedure for previous versions of Ubuntu as well with minor modifications (if any).
First, you need to install many dependencies, such as support for reading and writing jpg files, movies, 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 libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev
The next step is to get the OpenCV 2.1 code:
cd ~ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.1/OpenCV-2.1.0.tar.bz2/download tar -xvf OpenCV-2.1.0.tar.bz2 cd OpenCV-2.1.0/
In this version of OpenCV, the configure utility has been removed. Therefore you need to use Cmake to generate the makefile. Just execute the following line at the console. 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 .
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.
Now, you are ready to compile and install OpenCV 2.1:
make sudo make install
Now you have to configure the library. 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, open a new console, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.
Now you have OpenCV 2.1 installed in your computer.
Let’s check some demos included in OpenCV:
cd ~ mkdir openCV_samples cp OpenCV-2.1.0/samples/c/* openCV_samples cd openCV_samples/ 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.
In OpenCV 2.1, the grabcut algorithm is provided in the samples. 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.
./grabcut 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).
./bgfg_segm tree.avi
There are many other samples that you can try.











Hi Maarten,
The reason why this guide is not updated to OpenCV 2.2 yet is because the basic installation process is very similar in 2.1 and 2.2….
Although, OpenCV 2.2 is a great release and incorporates many new features such as support for android, gpu acceleration, a new framework for 2d features, etc. Because of these features, I might release an updated guide showcasing them… but I don’t know if I’ll have time to write it before they release the next version….
This method breaks my Ubuntu install every time. After doing this being it basically makes it so the login window (the one where you pick your user ID) no longer loads after the first reboot. I had to reinstall ubuntu twice because of this.
Thank u very much, helped me install, with the help of another site though “linuxconfig.org”
I installed with the help of your blog and since it was too good, I shared your link in my blog, with some suggestions, here’s the link for it
http://opencvuser.blogspot.com/2011/06/installing-opencv.html
I’ve been beating my head against the wall of “OpenCV is so simple to install” for about two weeks now.
I tried version 2.2….can’t get through the make without errors.
I tried the SVN “bleeding edge version”…same problem
Now I find your website, saying “OpenCV 2.1 is so easy to install”
I’m using Ubuntu 11.04 and following your directions precisely.
the make command halts here:
Linking C static library ../lib/libopencv_lapack.a
[ 42%] Built target opencv_lapack
[ 42%] Building CXX object src/cxcore/CMakeFiles/cxcore_pch_dephelp.dir/cxcore_pch_dephelp.o
/root/Desktop/OpenCV-2.1.0/OpenCV-2.1.0/src/cxcore/cxcore_pch_dephelp.cxx:1:72: error: one or more PCH files were found, but they were invalid
/root/Desktop/OpenCV-2.1.0/OpenCV-2.1.0/src/cxcore/cxcore_pch_dephelp.cxx:1:72: error: use -Winvalid-pch for more information
/root/Desktop/OpenCV-2.1.0/OpenCV-2.1.0/src/cxcore/cxcore_pch_dephelp.cxx:1:72: fatal error: /root/Desktop/OpenCV-2.1.0/OpenCV-2.1.0/src/cxcore/_cxcore.h: No such file or directory
compilation terminated.
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
I’ve been using cmake -i for an interactive setup, but I’m only guessing at many of the
proper options to specify.
your use of “cmake .” is a new experience for me, but it doesn’t seem to solve
any of the problems I’m having.
What is the bare minumum I need to get the OpenCV installed and working?
I intend to use Python to work with these libraries…but first I have to get past this
maddening hurdle.
Am I going to have to become a computer science major to be smart enough to simply install the program, let alone use it?
Please Please PLEASE I hope you can provide better guidance than I’ve gotten in numerous other places.
@dexter
Well, this guide was written for Ubuntu 9.10 and OpenCV 2.1. Since you are working with Ubuntu 11.04, maybe some steps are a little different. It shouldn’t be very difficult to adapt these instructions to make it work in 11.04, but if you are really desperate, try installing Ubuntu 9.10, and follow these instructions carefully step by step to install OpenCV 2.1. You shouldn’t have a problem. Actually I think 10.04 should work as well.
Also, note that you actually did not follow the instructions precisely because you are under the root user and you downloaded the code into the desktop, and my instructions say to download the code into the user directory… maybe try again following my instructions more precisely :)
All right. I tried it your way, from a non root login. Still getting errors. I’ve got a text file with all the miles and miles of warnings and errors. Care to have a look?
I have no idea what to do now. I could really use some help.
OK Dexter, I just published a post about installing OpenCV 2.2 in Ubuntu 11.04 from scratch. Take a look here:
http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/
Follow the instructions step by step and you should have no problems.
Excellent tutorial! I’m installing OpenCV 2.1 in Ubuntu 10.04 and everything works in 64 bit. However, in 32 bit, I’m getting an error which I think may be related to this issue, https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/663639. Their resolution is to recompile OpenCV with OpenCV’s lapack rather than defaulting to Ubuntu’s. Can you give instructions on how to do so? Or other ideas on this issue? Many thanks.
I haven’t tested this, but you can tell OpenCV that you want to compile with the internal lapack at the cmake step:
cmake -D BUILD_LAPACK=ON .
If that doesn’t work, you can always build the library yourself:
cd OpenCV-2.1.0/3rdparty/lapack/
cmake .
make
That will produce the library libopencv_lapack.a in the OpenCV-2.1.0/3rdparty/lapack/3rdparty/lib folder.
You can then compile your program with that library:
g++ main.cpp -LOpenCV-2.1.0/3rdparty/lapack/3rdparty/lib -lopencv_lapack
That should always work…
Thank you for the tutorial, I successfully installed OpenCV 2.1 and was able to do also the tutorials. I was wondering if I could do image processing using real time camera and make it control a system if ever the camera detect someone. I’m doing this for my thesis and I am hoping someone could help me on this. Thanks.
Sure you can…
You have to design the system first. It depends on many things, for example the field of view of the camera. The more controlled the environment is, the easier it is to detect people. You have to define what a person is in your system, a frontal face, a full profile view of the person, bird’s eye view, etc… What lens should you use?, prime?, variable focal length?, telephoto, wide angle?… do you need pan and tilt too?. Also, is this system going to detect a lot of people, or a few at the same time, what about occlusion?. Is the camera going to move, or not? what about the background?, does it interfere with your foreground?, does it change constantly?… is it indoor or outdoor?… can you use other sensors?, maybe stereo cameras?, lasers?, kinect? what about a movement sensor?… sometimes it is better to use other technology… remember that computers aren’t as good as humans in image processing, but given some constraints, some interesting stuff can be made… so… yes, you can detect people using OpenCV, but you have to be a little more specific than just that :)
Sebastian,
Thank for this great post!
So there isn’t a way to make OpenCV2.1 to work in Ubuntu11.04? Really need this version of OpenCV2.1 on Ubuntu 11.04 since version 2.2 doesn’t support by one of my other toolkit?
Yes, I have installed it on 11.04 before.
Try following the 2.1 tutorial, it should work OK with minor adjustments, if any. If you run into any trouble, ask in the comments…
Thanks! You saved my day!
I’m glad I could help
ur tutorial is great!! I tried a lot of other methods, which can not work. This is really helpful.. Thanks a lot
This is fantastic blog.
Thank you, I successfully installed OpenCV 2.1 :-D
thank you very much for this,
but i’ve a problem i wish you can help me solving it.
i have written my own program that uses cv.h and highgui.h program
but when i compile it i got huge errors like:
undefined reference to cvFree and somethings like that
i’ve follow all your step but when i try sudo ldconfig it outputs command not found
and then i switch to root by su and try ldconfig i get no output
so if u can help me on that i will appreciate it
Shazly, maybe you are running a different linux distro, or another version of Ubuntu because ldconfig should be available in your path in Ubuntu, but in your case it isn’t.
You can run it using the full path, for example:
sudo /sbin/ldconfigIf that doesn’t work, search where it is in your machine with one of these commands:
whereis ldconfigor
locate ldconfigAny of those codes should give you the correct location of ldconfig in your machine. Replace /sbin/ldconfig with the correct path and you should be good to go.
Hi,
Im getting an error with the reports for FFMPEG, when i run cmake . i got 0 for FFMPEG, How can i solve it? Please i really use some help
Thanks for your time
gracias me sirvio mucho, ahora estoy :) con opencv, me metere de lleno a programar
which compiler did you use for python and opencv?
I do not use any compiler for Python. For C++ (OpenCV) I use gcc.
Hi
I know its been a while since the last post on this thread, but still here goes.
I a trying to use OpenCV along with another piece of code (that I have not written). ffmpeg and boost libraries are also required which I have installed.
When I run the make script for the code, I get errors of the form
“ld: -lcv not found”
when I execute pkg-config –libs opencv I get the output
-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
Could you please tell me what is going wrong?
I have been breaking my head over this for almost a week now.
OpenCV changed how to include its libraries. That is why -lcv is not found anymore, it was used in previous versions of OpenCV.
You have to change the make script for your code to make it compatible with this and newer versions of OpenCV.
You should always use pkg-config to make your programs instead of hard coding the libraries (I guess -lcv is hardcoded in your make script, or if not, then there may be other OpenCV installations)
For example, if you have a source code in the file MyFile.cpp and you are using OpenCV in it, you should compile it this way:
g++ `pkg-config --cflags --libs opencv` -o MyApp MyFile.cppThat way, the libraries are included correctly. It does not matter if you have an older or newer version of OpenCV libraries with that compilation line, because it gets the correct linking every time given that pkg-config is correctly configured.
Change your make script to include pkg-config instead of hardocded libraries and it should work.
If you need them in separate places, here it is:
This line will give you the includes:
`pkg-config --cflags opencv`This line will give you the libraries and their paths:
`pkg-config --libs opencv`