GrabDuck

Installing OpenCV on Debian Linux

:

OpenCV_DebianIn this post I will describe the process of installing OpenCV (both versions 2.4.2 and 2.4.3) on Debian Linux (especially Debian-6). After installing, we will do some tests to verify the installation and also see some examples. As I was trying to install OpenCV on Debian I found that although there is lot of information on similar topics (see the references at the end), I still had to dig around a bit to understand the process completely. Some of the excellent guides are now outdated. Also, I found a few that have all the “what to do” but not “why to do”. Then as I started jotting down my own notes into my OneNote notebook, I realized that I should share these notes with others who are interested. I hope to update this document as I find more useful information in future.

It is recommended [Reference 2] that you update and upgrade your current system before installing OpenCV. You can do this from the terminal using the following two commands:

sudo apt-get update
sudo apt-get upgrade

The following packages may be installed using the terminal [sudo apt-get install package-name] or using the synaptic package manager. The first 3 packages are NOT optional and must be installed. The commands are shown after the names of the packages:

2.1. The first 3 packages are required and not optional:

  1. build-essential (GCC 4.4.x or higher C/C++ compiler )
  2. cmake 2.6 or higher (control software compilation)
  3. pkg-config (manage compile and link flags for libraries, used at the configuration stage)
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install pkg-config

2.2. The following packages are optional:

  1. libgtk2.0-dev (development files for GTK+ graphical user interfaces library. This is the default GUI backend for highgui on OSes other than Windows and MacOSX)
  2. python-dev (Python 2.6 or later with developer packages)
  3. python-numpy (Numpy)

Use the following line of code to install all the 3 packages simultaneously.

sudo apt-get install libgtk2.0-dev python-dev python-numpy

The packages below are used to read and write images in the respective formats (note, that if you don’t install these packages, OpenCV will use the default versions supplied with OpenCV. Also, openexr is not supplied, by default, with OpenCV)

  1. libpng12-0,  libpng12-dev, libpng++-dev, libpng3 (libpng library including development and runtime files)
  2. libpnglite-dev, libpngwriter0-dev, libpngwriter0c2
  3. zlib1g-dbg, zlib1g, zlib1g-dev
  4. pngtools
  5. libjasper1, libjasper-dev, libjasper-runtime
  6. libjpeg8, libjpeg8-dbg, libjpeg62, libjpeg62-dev,  libjpeg-progs (NOTE: The reason of installing libjpeg62-dev development package instead of libjpeg8-dev is that libtiff4-dev depends on libjpeg62-dev and libjpeg8-dev conflicts with libjpeg62-dev)
  7. libtiff4, libtiff4-dev, libtiffxx0c2, libtiff-tools
  8. ffmpeg, libavcodec-dev, libavcodec52, libavformat52, libavformat-dev (ffmpeg multimedia player, server, encoder, codec library, demuxer library. For more information see FFMPEG)
  9. libswscale0, libswscale-dev (ffmpeg video scaling library)
  10. openexr,libopenexr-dev, libopenexr6 (HDR image file development tools)

Use the following line of code to install all the packages mentioned above.

sudo apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev libpngwriter0-dev libpngwriter0c2 zlib1g-dbg zlib1g zlib1g-dev pngtools libjasper-dev libjasper-runtime libjasper1 libjpeg8 libjpeg8-dbg libjpeg62 libjpeg62-dev libjpeg-progs libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev libswscale0 libswscale-dev openexr libopenexr6 libopenexr-dev

The packages below are used to add video capturing, video decoding and video encoding capabilities to highgui. The output of the cmake will show which of the packages have been detected and will be used in highgui.

  1. libgstreamer0.10-0-dbg, libgstreamer0.10-0,  libgstreamer0.10-dev, libgstreamer-plugins-base0.10-dev (gstreamer streaming media framework)
  2. libxine1-bin, libxine-dev, libxine1-ffmpeg (xine video/media player and core libraries)
  3. libunicap2, libunicap2-dev, libucil2, libucil2-dev (uniform interface to video capture devices)
  4. libdc1394-22-dev, libdc1394-22, libdc1394-utils (high-level programming interface for IEEE1394 digital cameras)
  5. libv4l-0, libv4l-dev (video for linux support libraries)

Use the following line of code to install the packages used for video processing.

sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libucil2 libucil2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils libv4l-0 libv4l-dev

2.3. Other optional packages:

  1. swig (integrate C and C++ code with other languages like Python)
  2. bzip2 (this usually comes along with the Debian distributions)

You can also install other optional packages (depending on your needs) such as

  • libtbb-dev : parallelism library (TBB) for C++ – development files
  • libqt4-dev and libqt4-opengl-dev : Qt 4 development files and Qt 4 OpenGL library development files. To use Qt (Qt 4.6 or later) as the HighGUI backend (instead of GTK). In such case, use WITH_QT=ON flag with Cmake when configuring OpenCV.
  • texlive-latex-extra and python-sphinx: LaTeX supplementary packages (TeX Live) and sphinx tool for producing documentation.
  • libeigen2-dev : lightweight C++ template library for linear algebra
  • yasm : modular assembler with multiple syntaxes support
  • CUDA Toolkit with GPU support for Nvidia GPUs.
  • IPP 7 or later — OpenCV doesnot require IPP, but can be configured to use IPP to make color conversion, Haar training and DFT functions run faster.

For the purpose of this guide, I assume that your current working directory is “opencv”. This is where you are going to download the source code.

At the time of this writing, the latest stable release is OpenCV-2.4.2. You may download the source tarball (OpenCV-2.4.2.tar.bz2) to your current working directory (opencv) from SourceForge  either by using the browser or by using the following command in current working directory:

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

Note: If you try to build the above version of OpenCV on Debian 6 using the standard instructions that are available, you will most likely get some build errors (explained later at the appropriate place) due to some errors in the ZLIB library that comes along with OpenCV. Resolving this error is very simple. Also, the said error has been fixed in the latest 2.4.3 release of OpenCV, i.e. OpenCV-2.4.3.tar.bz2 which you can download from here. Also, you may download it using the command

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

Once you have downloaded the tarball, extract it using the command

tar xvjf OpenCV-2.4.2.tar.bz2

if you have downloaded 2.4.2, or

tar xvjf OpenCV-2.4.3.tar.bz2

if you have downloaded 2.4.3rc. The above command will extract the source code into a directory called “OpenCV-2.4.2” or “OpenCV-2.4.3” respectively.

5.1. “cd” to OpenCV-2.4.2 (or OpenCV-2.4.3) directory.

cd ~/opencv/OpenCV-2.4.2 (or cd ~/opencv/OpenCV-2.4.3)

5.2. Create a directory called “release” within the above directory and “cd” to the release directory.

mkdir release
cd release

5.3. Configure OpenCV build by running Cmake

This step generates a makefile (to be used by make later) that defines which parts of OpenCV needs to be compiled.

The syntax for the cmake commad is:

cmake [<some optional parameters>] <path to the OpenCV source directory>

If you are configuring OpenCV-2.4.2 and you want to install OpenCV with Python support, you can use the following command:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_ZLIB=ON -D BUILD_PYTHON_SUPPORT=ON ~/opencv/OpenCV-2.4.2

Note: The switch that must be turned ON in order to avoid build errors using the OpenCV-2.4.2 is the “BUILD_ZLIB=ON” option.

The following command is what I used (to configure OpenCV with Python, OpenEXR, Gstreamer, video capture and v4l video libraries:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_ZLIB=ON -D WITH_V4L=ON -D WITH_GSTREAMER=ON -D WITH_OPENEXR=ON -D WITH_UNICAP=ON -D BUILD_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..

Note: In the above command, “~/opencv/OpenCV-2.4.2” has been replaced with just “..” (two dots) at the end of the command. It is an argument to cmake to indicate that the source directory is the parent directory (of the current “release” directory).

If you are configuring OpenCV-2.4.3, you don’t need to use the “BUILD_ZLIB=ON” switch:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_V4L=ON -D WITH_GSTREAMER=ON -D WITH_OPENEXR=ON -D WITH_UNICAP=ON -D BUILD_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..

You should expect to see something similar to the following at the end of a successful configuration step:

"Cmake configuration"

Portion of the CMake configuration output step.

Generally, you should not see any errors in the above step. If you do, make sure that you have installed all the required packages. Also ensure that you have the following flags in the output of the CMake configuration step set to “YES”:

  • GTK+ 2.x:   YES
  • FFMPEG:   YES

If you are interested in working with video, ensure that the following are also set to “YES”:

  • GStreamer:  YES
  • V4L/V4L2: YES

5.4. Compile using GCC from the command line (within the “release” directory) by running “make”:

make

This is where you may see an “error” if you did not use “BUILD_ZLIB=ON” option during the configuration using cmake for OpenCV-2.4.2.  (If you are interested to know more about this error you can see here.)

From the command line (within the “release” directory), execute the following command

sudo make install

Note: It is NOT necessary to “install” OpenCV if you are using CMake (and, therefore, OpenCVConfig.cmake) for your projects. In fact it is not recommended to actually install OpenCV using “make install” especially if you plan to use different OpenCV versions. If you don’t install, then use OpenCV from where you built it [Reference 4].

7.1. Configure the system wide library search path:

The path to the OpenCV libraries (/usr/local/lib) is specified by creating a file called “opencv.conf” in “/etc/ld.so.conf.d/”. In order to do so, append the line “/usr/local/lib” at the end of the file “opencv.conf” (the file could be empty) using the following command, and as shown in the following figure, and then save it:

sudo gedit /etc/ld.so.conf.d/opencv.conf
"OpenCV search path configuration"

Configuration of OpenCV search path

Now, to configure the library, run the following command:

sudo ldconfig -v

In this case, running the ldconfig command creates the necessary links and cache to the most recent shared libraries found (OpenCV libraries in our case)  in the directories specified in the file /etc/ld.so.conf/opencv.conf.

Note: (alternatively you could do the following)

(I haven’t verified this step) Instead of creating the file “opencv.conf”, modifying it, and then running ldconfig, you could also do the following [Reference 3]:

Export the path to LD_LIBRARY_PATH environment variable, and dynamically link to the OpenCV library using ldconfig [Reference 3] (do this ONLY if you didn’t do sudo gedit /etc/ld.so.conf.d/opencv.conf followed by ldconfig)

export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
sudo ldconfig

Also, should you choose to avoid installing OpenCV library, you need to explicitly direct your system to the OpenCV library build directory by executing the following commands (assuming that you built OpenCV in the directory “~/opencv/OpenCV-2.4.3/release/”):

export LD_LIBRARY_PATH=~/opencv/OpenCV-2.0.3/release/:$LD_LIBRARY_PATH
sudo ldconfig

7.2. Setup the PKG_CONFIG_PATH variable

Now open the “bash.bashrc” file using gedit as follows:

sudo gedit /etc/bash.bashrc

Add the following two lines to the “bash.bashrc” file and save the file:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
"bash.bashrc file"

bash.bashrc file with the two added lines

That brings us to the end of the OpenCV installation on Debian-6. Before, testing the installation, it is highly recommended, that you restart the computer.

 8.1. Test C samples:

We shall test the OpenCV installation by building  and running the OpenCV samples:

cd ~/opencv/OpenCV-2.4.3/samples/c

We would like to build all the examples in the directory by using the script “build_all.sh”. However, before doing so, we need to ensure that the script is executable.

chmod +x build_all.sh
./build_all.sh

Now we will try some examples:

First one, is going to be the “delaunay” example. Run the example using the following command in the terminal (assuming that we are in the same directory)

./delaunay

Output:

"delaunayOutput"

Output of the sample code delaunay.c

Second, let’s try the “morphology” example:

./morphologyc

Output:

morphologyoperation

Output of the sample morphologyc.c

See some more examples in References 2 & 3.

8.2. Compiling our own simple c program:

Before compiling using pkg-config, test the following commands:

pkg-config --cflags opencv
pkg-config --libs opencv

The output of the first command should be:

-I/usr/local/include/opencv -I/usr/local/include

And the output should be something similar to the following for the second command:

/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

Our simple C code:

This is a very simple c code to display an image:

/* first.c :Code to display an image using OpenCV */
#include "opencv2/highgui/highgui.hpp"
int main(int argc, char** argv){

// load the image
  IplImage* img = cvLoadImage(argv[1],-1);
// create a window to display the image
  cvNamedWindow(argv[1],CV_WINDOW_AUTOSIZE);
// display the image
  cvShowImage(argv[1],img);
// wait for keystroke
  cvWaitKey(0);
// release the image object
  cvReleaseImage(&img);
// destroy the window
  cvDestroyWindow(argv[1]);
}

I had placed the code in the directory ~/opencv/mycprogs/basic/

The code was compiled using the following command:

gcc `pkg-config --cflags --libs opencv` -o first1 first.c

Note for new users: Please be aware that there are backquote (`) symbols (generally found on the same U.S. computer keyboard key as the tilde) in the above command and not single-quote (‘) or apostrophe symbol! Using a single-quote will mostly spit the following error:

"gcc: pkg-config --cflags --libs opencv: No such file or directory"

On successful compilation, you can run the program as shown below:

./first1 lena.jpg

You should expect to see the following output:

lena

Output of our simple program to display an image

Note: Using pkg-config to get all dependencies to compile ensures that you will be able to compile almost all code written using the OpenCV libraries. However, it my produce a binary with excessive dependencies [Reference 3].  As it is shown in [Reference 3], you can see a list of the OpenCV based share libraries which are loaded dynamically if the above code is compiled using the pkg-config by running the following command:

gcc `pkg-config --cflags --libs opencv` -o first1 first.c
ldd first1 | grep local

Output of the above command:

libopencv_calib3d.so.2.4 => /usr/local/lib/libopencv_calib3d.so.2.4 (0xb772e000)
libopencv_contrib.so.2.4 => /usr/local/lib/libopencv_contrib.so.2.4 (0xb7657000)
libopencv_core.so.2.4 => /usr/local/lib/libopencv_core.so.2.4 (0xb7455000)
libopencv_features2d.so.2.4 => /usr/local/lib/libopencv_features2d.so.2.4 (0xb73b7000)
libopencv_flann.so.2.4 => /usr/local/lib/libopencv_flann.so.2.4 (0xb734d000)
libopencv_gpu.so.2.4 => /usr/local/lib/libopencv_gpu.so.2.4 (0xb72fd000)libopencv_highgui.so.2.4 => /usr/local/lib/libopencv_highgui.so.2.4 (0xb72b1000)
libopencv_imgproc.so.2.4 => /usr/local/lib/libopencv_imgproc.so.2.4 (0xb703c000)
libopencv_legacy.so.2.4 => /usr/local/lib/libopencv_legacy.so.2.4 (0xb6f26000)
libopencv_ml.so.2.4 => /usr/local/lib/libopencv_ml.so.2.4 (0xb6eaf000)
libopencv_nonfree.so.2.4 => /usr/local/lib/libopencv_nonfree.so.2.4 (0xb6e99000)
libopencv_objdetect.so.2.4 => /usr/local/lib/libopencv_objdetect.so.2.4 (0xb6e22000)
libopencv_photo.so.2.4 => /usr/local/lib/libopencv_photo.so.2.4 (0xb6e06000)
libopencv_stitching.so.2.4 => /usr/local/lib/libopencv_stitching.so.2.4 (0xb6d8d000)
libopencv_ts.so.2.4 => /usr/local/lib/libopencv_ts.so.2.4 (0xb6cf7000)
libopencv_video.so.2.4 => /usr/local/lib/libopencv_video.so.2.4 (0xb6cb7000)
libopencv_videostab.so.2.4 => /usr/local/lib/libopencv_videostab.so.2.4 (0xb6c84000)

As you can see all the opencv libraries (including the ones that we really don’t require for this program) have been linked. As a result of this the executable file size is unnecessarily large. Although, it is not that critical for desktop based applications, it is important to be aware of this, especially if would like to use your code for some embedded application.

The above is equivalent to compiling the code by manually providing all the libraries, like so:

gcc -o first1 first.c&nbsp; -I/usr/local/include/opencv -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -lopencv_gpu -lopencv_nonfree -lopencv_photo -lopencv_stitching -lopencv_ts -lopencv_videostab

You could also compile the code by manually providing the just the needed libraries as follows:

gcc -o first first3.c -I/usr/local/include/opencv -lopencv_highgui

Finally, if we compare the file sizes of the executable(s) produced by the above 3 methods, we will find that the size of the executable produced by handpicking the libraries is the least (Well, no surprise there). Note that I have named the output files as first1, first2 and first3 respectively.

"first.c comp"

Comparison of file sizes for the different type of compilation methods.

8.3. Test Python samples:

Change directory to ~/opencv/OpenCV-2.4.3/samples/python/ and then run

python convexhull.py
convexhull.pyOutput

Output of python sample convexhull.py

(Note: You need to press “ESC” button to stop the loop and exit the convexhull program.)

Now, lets write a small python code, and call it first.py

#!/usr/bin/python
# Import the CV module
import cv2.cv as cv
# Load the image
img = cv.LoadImage('baboon.jpg',cv.CV_LOAD_IMAGE_COLOR)
# Create a named window
cv.NamedWindow('BaboonImage',cv.CV_WINDOW_AUTOSIZE)
# Display the image
cv.ShowImage('BaboonImage',img)
# Wait for keystroke
cv.WaitKey(0)
#
cv.DestroyWindow('BaboonImage')

Before running the code, you need to first make this script an executable using the following command:

chmod 755 first.py

Also, make sure you have some image (in this case it is baboon.jpg) in the same directory. Then, from the terminal you can use either of the following two ways to run the script:

python first.py

 

./first.py

(both commands should work)

The output of the above script is shown below:

baboon

Output of first.py

8.3. Trouble shoot python related problems:

Try to “import” the cv module within python as shown below in the python interactive mode:

"python test"

Try to import OpenCV modules

According to [Reference 6], python may return an error like “No module named cv” because python module is installed in /usr/local/lib/python2.6/site-packages, but on Debian (and on Ubuntu), Python only looks in /usr/local/python2.6/dist-packages. The following block-quote is from [Reference 6]

 You can fix it using three ways (Use only one of those, the first is the best):

  • move the cv.so file from the site-packages to the dist-packages :
sudo mv /usr/local/lib/python2.6/site-packages/cv.so /usr/local/lib/python2.6/dist-packages/cv.so
  • add to /usr/local/lib/python2.6/dist-packages os.path in python :
>>>import sys
>>>print sys.path
>>>sys.path.append("/usr/local/lib/python2.6/site-packages")
  • Using bash : export $ PYTHONPATH=/usr/local/lib/python2.6/site-package. Add this line to your ~/.bashrc if you want it to be permanent.

Also, in case, you chose not to install OpenCV, then put cv.so and cv2.so (called cv.pyd and cv2.pyd on Windows) to opencv/samples/python directory, or rather add the directory containing cv.so/cv.pyd to PYTHONPATH [Reference 4], e.g.

export PYTHONPATH=~/projects/opencv/release/lib:$PYTHONPATH

In case, there is still some problem running python code, then please refer to the section “Making Python work” in [Reference 6].

That brings us to the end of this post. I hope you will find this guide helpful.

  1. “Installation in Linux”, OpenCV v2.4.3 documentation. Link: http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_install/linux_install.html
  2. “Installing OpenCV 2.4.1 in Ubuntu 12.04 LTS”, Sabastian Montabone, June 24, 2012. Link: http://www.samontab.com/web/2012/06/installing-opencv-2-4-1-ubuntu-12-04-lts/
  3. “Introduction to Computer Vision with the OpenCV Library on Linux”, Lubos Rendek, January 2011. Link: http://linuxconfig.org/introduction-to-computer-vision-with-opencv-on-linux
  4. “OpenCV installation Guide”, Willowgarage.com. Link: http://opencv.willowgarage.com/wiki/InstallGuide
  5. “Installing OpenCV on Linux”, Willowgarage.com. Link: http://opencv.willowgarage.com/wiki/InstallGuide_Linux
  6. “OpenCV Installation Guide on Debian and Ubuntu”, Willowgarage.com. Link: http://opencv.willowgarage.com/wiki/InstallGuide%20%3A%20Debian
  7. “A Comprehensive Guide to Installing and Configuring OpenCV 2.4.2 on Ubuntu,” by  Osman Eralp, June 6, 2012. Link: http://www.ozbotz.org/opencv-installation/
  8. “How to compile OpenCV based programs in Linux,” Willowgarage.com. Link: http://opencv.willowgarage.com/wiki/CompileOpenCVUsingLinux