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.

1. Before installing the Prerequisites:

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

2. Install the Prerequisites:

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.

3. Download the OpenCV source from SourceForge:

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

4. Extract OpenCV source code

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. Building OpenCV from source using CMake command line

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

6. Installing OpenCV libraries

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. Configuring OpenCV path

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. Test OpenCV installation

 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.

9. References:

  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
About these ads

50 thoughts on “Installing OpenCV on Debian Linux

  1. Hello ,
    Thanks alot for your comment , I successfully install opencv 2.4.3 on my desktop linux.
    Hope a huge help for your help .

  2. Pingback: opencv on linux « Jaysin's Blog

  3. Pingback: installing opencv on linux « Jaysin's Blog

  4. Dear Indranil, your tutorial is excellent! Please consider sending it to OpenCV.org . I am sure it will be of great help for the community. Thanks again!

  5. Pingback: Tutorials - Setup OpenCV for Android, iOS, Linux, and Windows

  6. Pingback: Linux OpenCV Kurulumu | Bilgisayar Bilimi, Yazilim Gelistirme, Linux ve Computer Vision

  7. %> gcc `pkg-config –cflags –libs opencv` -o first1 first.c
    /tmp/ccueGiRt.o: In function `cvRound’:
    first.c:(.text+0×19): undefined reference to `lrint’
    /tmp/ccueGiRt.o: In function `cvDecRefData’:
    first.c:(.text+0xa5c): undefined reference to `cvFree_’
    first.c:(.text+0xacd): undefined reference to `cvFree_’
    /tmp/ccueGiRt.o: In function `cvGetRow’:
    first.c:(.text+0xbc3): undefined reference to `cvGetRows’
    …..this list goes on for a bit.

    Any idea why this is happening ?

    Also when running ./build_all.sh below is all that could be compiled.
    compiling adaptiveskindetector.cpp
    compiling bgfg_codebook.cpp
    compiling blobtrack_sample.cpp
    compiling facedetect.cpp
    compiling find_obj.cpp
    compiling find_obj_calonder.cpp
    compiling find_obj_ferns.cpp
    compiling latentsvmdetect.cpp
    compiling mser_sample.cpp
    compiling mushroom.cpp
    compiling one_way_sample.cpp
    compiling smiledetect.cpp
    compiling tree_engine.cpp

    I got the folloing error for the ones that could not compile:
    /usr/bin/ld: /tmp/ccJYFiTb.o: undefined reference to symbol ‘lrint@@GLIBC_2.1′
    /usr/bin/ld: note: ‘lrint@@GLIBC_2.1′ is defined in DSO /lib/i386-linux-gnu/libm.so.6 so try adding it to the linker command line
    /lib/i386-linux-gnu/libm.so.6: could not read symbols: Invalid operation
    collect2: error: ld returned 1 exit status

    [http://stackoverflow.com/questions/9934549/very-strange-linker-behavior] but if im honest i don’t understand the answer.

    • Hi Richard,
      I am not sure about this particular problem. Here are some things to check though — make sure you have updated you system, and installed all the prerequisites such as build-essentials, cmake and pkg-config. Also, were there any errors while compiling OpenCV using ‘make’? If so then, make sure that “BUILD_ZLIB=ON” is used while configuring OpenCV using cmake.

    • Hi dhanash,
      I just tried the command to verify that the source locations are fine. I was able to download and install properly. Can you check if you have the proper permission to access the folder to which you are trying to download? Also, as I have mentioned in the post, please create a folder, such as “opencv” in a place where you have access, such as /home/user_name/.
      Hope that helps.

  8. Pingback: Instalando Ubuntu 13.04 – 64 bits, OpenCV, Scikit-Learn y otros programas | Italo Farfán

  9. Pingback: Installing OpenCV 2.2.0 on Ubuntu 12.4. | Trần Thị Thanh Huyền

  10. To install OpenCV using the terminal on Ubuntu:
    $ su -
    # apt-get update
    # apt-get install build-essential
    # apt-get install libavformat-dev
    # apt-get install x264 v4l-utils ffmpeg
    # apt-get install libcv2.3 libcvaux2.3 libhighgui2.3 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev

    OpenCV should be installed in /usr/local/ Installation Directory

    You may also want to compile and view examples
    $ cp -r /usr/share/doc/opencv-doc/examples .
    $ cd examples
    $ cd c
    $ sh build_all.sh

    Source: http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html

  11. Indranil, thank you for great tutorial it is really helpful. Just want to add for newbies in cmake:
    If you don’t have enabled options:
    GTK+ 2.x: YES
    FFMPEG: YES
    you can enable it in ./release/CMakeCache.txt

  12. Hey Taras,
    While i installed opencv,i got this:: FFMPEG: NO, GStreamer:NO, V4L/V4L2:NO/YES. Can u plzz explain in detail that after opening ./release/CMakeCache.txt , how to enable these options .

  13. Hello,
    First of all, thanks for this amazing tutorial!

    I’ve been trying to use OpenCV on Debian for days, and this is the first time i’ve reached the moment where i can try to compile something :’)

    To be honest, i’ve never used Linux before last week, and i’m not really good in C coding, but i have to use OpenCv for a professionnal project.

    So i try to compile your first example (lena.jpg)

    I create in OpenCV-2.4.3 a directory, and in it, i put the C code, and lena.jpg

    But :

    root@debian:~/OpenCV-2.4.3/Alex# ./first1 lena.jpg

    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 /root/OpenCV-2.4.3/modules/highgui/src/window.cpp, line 602
    terminate called after throwing an instance of ‘cv::Exception’
    what(): /root/OpenCV-2.4.3/modules/highgui/src/window.cpp:602: 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

    Abandon”

    I didn’t have any problem compiling (with gcc `pkg-config –cflags –libs opencv` -o first1 first.c), and libgtk2.0-dev and pkg-config are updated…

    I’ve been looking for anwers on the net, without success.

      • Hello Indranil,

        My bad.

        Doing “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 OpenCV-2.4.3″ I can see that GTK+2.x is set to YES, but all Video I/O are set to NO.

        But when i try to install ffmpeg packages, it tells me that it is “Impossible to find the package libpngwriter0-dev, libpngwriter0c2, libavcodec52, libavformat52, libswscale0, libavcodec52, libavformat52″.

        However, i did upgrade, and update. Do i have to use sources list? :s

      • Alexandre,

        Although these packages were supposed to be optional, it might be better to just install them. Use

        “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”

        and then redo cmake. Hope it solves the problem.

  14. Hello Indranil,

    I tried to follow your instructions but i wasn’t able to install these differents packages.
    (I don’t know if it was an issue form the sources list or something else…)

    So, I changed my VM and used Ubuntu, following official OpenCV instructions for Linux.

    During the installation, I met the same issue with FFMPEG, but this time, thanks to you, I solved it by installing the needed packages.

    To finish, I must explain that I couldn’t compile examples (samples) :
    “libopencv_core.so.2.3: cannot open shared object file: No such file or directory”
    but I solved it doing this : “export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib”
    and now it seems to be working :)

    Thanks Indranil for your advice and for your availability, you helped me a lot!

  15. HELP!!! PLEASE!!!!!
    when i try to install it using su root it has this error
    CMake Error: The source directory “/root/OpenCV-2.4.2″ does not exist.
    other wise when i try to install it using normal user mode it says
    CMake Error: : System Error: Permission denied
    what i should do?

    • I assume that you have been successful up to and including Step 5. Can you please provide little more information, such as where (directory path) did you download and build OpenCV ? Also, if you have successfully built it, you do not have to install OpenCV, you can use it without installing it. The only constrain is that you will have to use OpenCV from the same directory that you built it.

  16. Good day! Do you know if they make any plugins to protect against
    hackers? I’m kinda paranoid about losing everything I’ve worked hard on.
    Any recommendations? – SEO Services.

    • Hi,
      Although I am not aware of any specific plugins to protect against hackers, I believe the wordpress.com platform, which I am using, is RELATIVELY secure. The reason I used “relatively” is because there is no way to protect against a skilled and determined hacker!

  17. Hi Indranil,
    I want to install opencv with code blocks IDE on Debian Linux(as Debian Linux is “RASPBIAN” installed in raspberry pi ).Kindly suggest me what tutorial I need to follow.

  18. Pingback: Install texlive2013 and opencv2.4.9 in the setting of Debian operating system | Sci Empire

  19. I don’t even know hoow I stopped uup right here, but I believed thhis
    put up used to be good. I do not recognise who you might
    bee but certainl you’re goijg to a well-known blogger should you aren’t already.
    Cheers!

  20. You have said that without installation also OpenCV can be used. I am running on server and I dont want to install. What is the command for running some build.make file?

  21. Thanks for the tutorial. This tutorial is far better than others for installing opencv on linux.
    However, i cant run samples. there header errors after run ./build_all.sh script. I hope you can help me.
    2 of very long errors are:

    compiling contours.c
    contours.c:1:39: fatal error: opencv2/imgproc/imgproc_c.h: No such file or directory
    compilation terminated

    compiling convert_cascade.c
    convert_cascade.c:1:43: fatal error: opencv2/imgproc/imgproc_c.h: No such file or directory
    compilation terminated

    same goes with other .h and .hpp file. I believe i have problem with configuring the path (step7). I follow this step:
    sudo gedit /etc/ld.so.conf.d/opencv.conf and fill with /usr/local/lib.
    and i notice in my /usr/local/lib, there only 2 folder which are site_ruby and python2.7
    And i decide to change the path to /usr/lib because theres alot of folder in this path including pkgconfig folder but the result is still the same.
    I also tried changed the path to /USR/LOCAL/LIB. In this path also have pkgconfig and python2.7 folder with other files.

    All changes i proceed with:
    sudo gedit /etc/bash.bashrc
    i edit with same path as opencv.conf:
    PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
    export PKG_CONFIG_PATH

    and everytime i changed it, i reboot and the result still the same. How i want to know the right path.
    My OpenCV-2.4.3 folder at /home/debian. If there anything wrong with this.

    • Hi, I haven’t done this in a while. So I am not really very confident. However, it seems to me that the linker is not able to find all the dependencies. Are you sure you have done the following exactly as mentioned above:

      “`
      sudo gedit /etc/ld.so.conf.d/opencv.conf
      sudo ldconfig -v
      “`

      and then 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
      “`

      • Yes sir, Im very sure that I complete that step. I even try several times.
        May I know why to edit the path with /usr/local/lib?
        What suppose /usr/local/lib contains?
        Thank You sir for fast response.

  22. Pingback: Pengantar Pemrograman OpenCV C++ di Linux | Ade Malsasa Akbar

  23. Pingback: Install texlive2013 and opencv2.4.9 in the setting of Debian operating system | emgao

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s