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. Unfortunately, the newer version of OpenCV, 2.0, which is full of new features is not yet available in the repositories of Ubuntu.
Here are the steps that I used to successfully install OpenCV 2.0 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
1 | 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.0 code:
3 | tar -xvf OpenCV-2.0.0. tar .bz2 |
Now, you need to configure how you want to compile OpenCV 2.0. In my case I used /usr/local as the installation directory and I also activated the video support for reading and writing videos using OpenCV (via ffmpeg).
1 | ./configure --prefix=/usr/ local -- enable -apps -- enable -shared --with-ffmpeg --with-gnu-ld --with-x --without-quicktime CXXFLAGS=-fno-strict-aliasing |
Check that the above command produces no error and that in particular it reports ffmpeg as yes. 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.0:
Now you have to configure the library. First, open the opencv.conf file with the following code:
1 | 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:
Run the following code to configure the library:
Now you have to open another file:
1 | sudo gedit /etc/ bash .bashrc |
Add these two lines at the end of the file and save it:
1 | PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/ local /lib/pkgconfig |
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.0 installed in your computer.
Let’s check some demos included in OpenCV:
3 | cp /usr/ local /share/opencv/samples/c/* openCV_samples |
All 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:
1 | ./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.
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).