Installing and Getting Started with OpenCV on Ubuntu

Here is how to get OpenCV 2.1 going on Linux (Ubuntu). It is written for beginners that are not familiar with the Unix/Linux development environment. It might be useful to you.

Introduction

OpenCV, originally from Intel and now maintained by open source heroes at the Willow Garage robotics company. It is free and open. It contains a ton of image processing and computer vision algorithms and the data structures necessary for those algorithms.

As of the time I'm writing, the current version of Ubuntu is 10.04 and the current version of OpenCV is 2.1. To get them running together, you need to install from source. But don't worry, it's not too hard with cmake.

Preliminaries

You should install the Intel TBB libraries to enable parallel processing:

% sudo apt-get install libtbb-dev

if you're not running Ubuntu 10.04 this probably won't work. You'll have to install the right version of TBB from source. See the OpenCV Install Guide for details.

You'll definitely want cmake and pkg-config:

% sudo apt-get install cmake-gui pkg-config

There are a whole bunch of other libraries that OpenCV can use if you've installed them, including gtk2 for GUI stuff, libjpeg, libtiff, libjasper, libpng, and zlib for image formats, ffmpeg, libgstreamer, libxine, and unicap for video, and libdc1394 for firewire cameras. For each of these you need to use apt-get or synaptic to install the “Development” packages.

For ffmpeg:

% sudo apt-get install libavformat-dev libswscale-dev

Build and install

Once you've got all the preliminary things in place, you can go to the OpenCV Install Guide and follow the link to download OpenCV 2.1.

Go to the directory that the package got downloaded to and unpack it:

% cd ~/Desktop ; tar xjvf OpenCV-2.1.0.tar.bz2

Next run the cmake GUI:

% cmake-gui

Select the unpacked OpenCV archive directory (e.g., /home/username/Desktop/OpenCV-2.1.0) as the source code directory and a separate directory (e.g., /home/mdailey/Desktop/OpenCV-Build as the build directory, then click the “Configure” button. Select the “Unix Makefiles” option with “default native compilers.”

Change any build flags you want then click the “Configure” button again. If all goes well, click on the “Generate” button. If all goes well again, you are ready to build:

% cd OpenCV-Build
% make
% sudo make install

Compiler warnings are OK, but watch out for any errors along the way.

Finding Documentation

When you install OpenCV, the documentation, including the reference manuals in HTML and a lot of sample programs, is automatically installed in /usr/local/share/opencv/ directory.

When you're doing OpenCV programming, you will find it very useful to refer to the reference documentation. You should bookmark the HTML pages in your Web browser, either the local versions or the ones at Willow Garage.

Building a sample program

Here we'll go step-by-step through writing and running a simple program assuming you did installed OpenCV as described above.

Makefile

We'll use the newer-style C++ API to OpenCV. The “classic” C API is nice and easy to use as well, but most folks say they prefer C++ these days.

First, we need a makefile to build the code for us so we don't have to type hairy g++ command lines.

Below is a makefile to automatically compile a sample program stored in the source code file opencv-test.cpp. Take the make instructions below and put in a file called Makefile in some project directory outside of the OpenCV installation directory.

SRC = opencv-test.cpp
TARG = opencv-test

CC = g++

CPPFLAGS = -g -Wall -Wno-unused-function `pkg-config --cflags opencv`

LDFLAGS = `pkg-config --libs opencv`

OBJ = $(SRC:.cpp=.o)

all: $(TARG)

clean:
        rm -f *~ *.o $(TARG)

opencv-test.cpp

Now here's some C++ source code that uses OpenCV to take the SVD of a matrix. Put it in a file called opencv-test.cpp:

#include <iostream>
#include <iomanip>
#include <cv.h>
 
using namespace cv;
using namespace std;
 
// Simple function to print out a matrix
 
void myMatPrint( ostream &outStream, string stringLabel, Mat &mat )
{
    outStream << stringLabel << endl;
    for (int iRow = 0; iRow < mat.rows; iRow++)
    {
        for (int iCol = 0; iCol < mat.cols; iCol++)
        {
            outStream << setw(12) << mat.at<double>(iRow,iCol);
        }
        outStream << endl;
    }
}
 
// Test code to take the SVD of a matrix and check its correctness
 
void testSvd( void )
{
    // The data for the matrix we want to take the SVD of
 
    double aData[] = { 0.078286, 0.152800, 0.483599, 0.132373,
                       0.809265, 0.040108, 0.420195, 0.468431,
                       0.188109, 0.479326, 0.245392, 0.217179,
                       0.345795, 0.915636, 0.618565, 0.786891 };
 
    // 4x4 double-precision matrix initialized from aData[]
 
    Mat matA(4, 4, CV_64F, aData);
 
    // Perform the SVD: A = U D V'
 
    SVD svdA(matA);
 
    // Check result
 
    Mat matD = Mat::eye(matA.rows, matA.rows, CV_64F);
    for (int iDiag = 0; iDiag < matA.rows; iDiag++)
    {
        matD.at<double>(iDiag, iDiag) = svdA.w.at<double>(iDiag, 0);
    }
    Mat matAhat(4, 4, CV_64F);
    matAhat = svdA.u * matD * svdA.vt;
 
    double dError = norm( matA, matAhat );
 
    // Print out results
 
    myMatPrint( cout, "Original matrix A:", matA );
    myMatPrint( cout, "U:", svdA.u );
    myMatPrint( cout, "D:", svdA.w );
    myMatPrint( cout, "V (transposed):", svdA.vt );
    myMatPrint( cout, "U D V':", matAhat );
    cout << "Error (L2 norm): " << dError << endl;
 
    return;
}
 
// Main
 
int main( void )
{
    cout << "Testing the SVD class..." << endl;
    testSvd();
    return 0;
}

Compiling and running

Now you can compile your program just by running make in the same directory as the Makefile and C source:

% make
g++  -g -Wall -Wno-unused-function `pkg-config --cflags opencv` `pkg-config --libs opencv`  opencv-test.cpp   -o opencv-test
%

Now, you're (almost) ready to run the program. Just type

% ./opencv-test

at the command line. The first time you try this with a per-user installation, you'll probably get the error message

./opencv-test: error while loading shared libraries: libml.so.2.1: cannot open shared object file: No such file or directory

the reason is that the Linux loader is looking for the shared library (DLL in Windows parlance) libml.so.2.1 but can't find it since it's in a non-standard directory (/usr/local/lib if you followed the instructions above).

The solution is to tell the dynamic linker where your DLLs are. Add the line

export LD_LIBRARY_PATH=/usr/local/lib

to your ~/.bashrc and (you only have to do this when .bashrc is changed), source it:

% source ~/.bashrc

To check that your library path is set correctly, use the env command:

% env | grep LD_L
LD_LIBRARY_PATH=/usr/local/lib

OK, now we can just run ./opencv-test without trouble. If everything is working, you should see the matrix A print out, the SVD of A (U, D, and VT), the reconstruction of A from U, D and V, and finally the norm of the difference, which was 2.13994e-15 in my test:

% ./opencv-test
Testing the SVD class...
Original matrix A:
    0.078286      0.1528    0.483599    0.132373
    0.809265    0.040108    0.420195    0.468431
    0.188109    0.479326    0.245392    0.217179
    0.345795    0.915636    0.618565    0.786891
U:
   -0.245183   0.0184143    0.968008  -0.0500616
   -0.471097   -0.875102   -0.104562  -0.0364773
   -0.328201    0.230509   -0.134376   -0.906142
   -0.781177    0.425115   -0.184309    0.418414
D:
     1.74815
    0.698022
    0.321647
    0.137764
V (transposed):
   -0.418902    -0.53139   -0.503545   -0.537204
   -0.739783    0.669684  -0.0562768   -0.032817
   -0.304207   -0.278107    0.861844   -0.295532
   -0.429768   -0.437954  -0.0223628    0.789301
U D V':
    0.078286      0.1528    0.483599    0.132373
    0.809265    0.040108    0.420195    0.468431
    0.188109    0.479326    0.245392    0.217179
    0.345795    0.915636    0.618565    0.786891
Error (L2 norm): 2.13994e-15
%

Conclusion

You've seen how to install the OpenCV library and how to write and run OpenCV programs under Linux, Ubuntu Edgy in this case. Comments and improvements are welcome.

Matthew Dailey 2010/06/07 13:04

Discussion

pcoder, %2011/%01/%16 %14:%Jan:

On my fresh Maverick installation, I got the following error while configuring with CMake

CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found.   Please set CMAKE_CXX_COMPILER to a valid compiler path or name.

I had to run the following command before I could proceed with the installation

sudo apt-get install build-essential
 
Prashant Singh, %2011/%02/%17 %16:%Feb:

I also got the same error !…. Thanks for the information, its resolved now !….

 
mvcoder, %2011/%03/%02 %13:%Mar:

While Doing Make in the OpenCV-Build Folder I got the Following error :

[ 0%] Building CXX object 3rdparty/flann/CMakeFiles/flann.dir/util/saving.o c++: -pg and -fomit-frame-pointer are incompatible make[2]: * [3rdparty/flann/CMakeFiles/flann.dir/util/saving.o] Error 1 make[1]: * [3rdparty/flann/CMakeFiles/flann.dir/all] Error 2 make: *** [all] Error 2

I tried to search it on the internet and also there is no problem or error in the previous steps …

Can you please help me to get rid of this problem….

 
 
opencv/tutorial/install/ubuntu.txt · Last modified: 2010/06/08 03:32 by mdailey
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki