Tag Archives: install

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.



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.


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.


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

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


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( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "Original matrix A:", matA );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "U:", svdA.u );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "D:", svdA.w );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "V (transposed):", svdA.vt );
    myMatPrint( <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a>, "U D V':", matAhat );
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a> &lt;&lt; "Error (L2 norm): " &lt;&lt; dError &lt;&lt; endl;


// Main

int main( void )
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html">cout</a> &lt;&lt; "Testing the SVD class..." &lt;&lt; endl;
    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

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


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.


Ubuntu 10.10 USB Creator Error – Vesamenu.C32: Not A COM32R Image

I recently installed ubuntu 10.10 netbook remix on my Eeepc 1000H and it sure looks and works beautifully. Having another computer on which I wanted to install ubuntu 10.04, I used the usb startup disk creator on the Eeepc running ubuntu 10.10 to make a ubuntu 10.04 bootable usb flash drive. When i plug the now ubuntu 10.04 bootable usb into another machine to begin installing, I get this error:

SYSLINUX 4.01 debian-20100714 EDD Copyright (C) 1994-2010 H. Peter Anvin et alUnknown keyword in configuration file: gfxbootvesamenu.c32: not a COM32R imageboot:vesamenu.c32: not a COM32R image

Here is how i got around it: Press the TAB key at the boot: prompt I got presented all the available options as: boot:live live-install check memtest hd mainmenu help I now went ahead to type live-install at the boot prompt & press the Enter key.

boot: live-install

To try out using the Live-cd, just type:

boot: live

I was glad to get this working. I did not have access to any other machine running a different version of ubuntu with which i could make a bootable usb drive. Enjoy! use content & acknowledge the

source as http://kjohnah.blogspot.com

HOWTO: Install and use the latest FFmpeg and x264


FFmpeg is a versatile tool to encode and decode a multitude of video and audio formats. x264 encodes high-quality H.264 video.

Although FFmpeg and x264 are available in the Ubuntu repositories, you may need to compile from source. For example, the ffmpeg-user mailing list requires that you use the latest FFmpeg before asking for help. You may also like to have the bleeding-edge for encoding videos. Also, FFmpeg in the Ubuntu repository may not support necessary encoders, decoders, and formats.

Choose your Ubuntu
0.The instructions on the page are for Ubuntu Maverick Meerkat 10.10. Separate instructions are also available for older, supported releases:

Install the Dependencies
1. Uninstall x264, libx264-dev, and ffmpeg if they are already installed. Open a terminal and run the following (you can usually paste into a terminal with shift+ctrl+v). Copy and paste the whole code box for each step.

sudo apt-get remove ffmpeg x264 libx264-dev

2. Get all of the packages you will need to install FFmpeg and x264 (you may need to enable the universe and multiverse repositories):

sudo apt-get update
sudo apt-get install build-essential subversion git-core checkinstall yasm texi2html \
    libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev \
    libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libvorbis-dev libvpx-dev \
    libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev

Install x264
3. Get the current source files, compile, and install x264.

git clone git://git.videolan.org/x264.git
cd x264
sudo checkinstall --pkgname=x264 --pkgversion "2:0.`grep X264_BUILD x264.h -m1 | \
    cut -d' ' -f3`.`git rev-list HEAD | wc -l`+git`git rev-list HEAD -n 1 | \
    head -c 7`" --backup=no --deldoc=yes --fstrans=no --default

Install FFmpeg
4. Get the most current source files, compile, and install FFmpeg. Run “./configure –help” to see what other features you can enable/disable. Nightly FFmpeg snapshots are also available.

svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc \
    --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb \
    --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis \
    --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab
sudo checkinstall --pkgname=ffmpeg --pkgversion "4:SVN-r`LANG=C svn info | \
    grep Revision | awk '{ print $NF }'`" --backup=no --deldoc=yes --fstrans=no \
hash x264 ffmpeg ffplay

Install qt-faststart (optional)
5. This is a useful tool included with FFmpeg that rearranges a file with H.264 video, “such that the moov atom is in front of the data, thus facilitating network streaming”. Basically, it allows web movies to start playing before they are completely downloaded. Usage: qt-faststart input.foo output.foo.

cd ~/ffmpeg
make tools/qt-faststart
sudo checkinstall --pkgname=qt-faststart --pkgversion "4:SVN-r`LANG=C svn info | \
    grep Revision | awk '{ print $NF }'`" --backup=no --deldoc=yes --fstrans=no \ 
    --default install -D -m755 tools/qt-faststart /usr/local/bin/qt-faststart

That’s it for installation. You can keep the ~/x264 and ~/ffmpeg directories if you later want to update the source files to a new revision. See Updating FFmpeg and x264 below for more details.

Using FFmpeg and x264
The easiest method for high quality video encoding is by using the libx264 presets that are included with FFmpeg. I recommend reading the FFmpeg x264 encoding guide before trying these presets so you have a better idea of what to use. You can add options such as frame size (for example: -s 640×480) or tweak my examples to customize your encode. You can see a current list of all libx264 presets(such as ipod or lossless) or after installation look in the /usr/local/share/ffmpeg directory.

One-pass CRF (Constant Rate Factor) using the slow preset. One-pass CRF is good for general encoding and is what I use most often. Adjust -crf to change the quality. Lower numbers mean higher quality and a larger output file size. A sane range is 18 to 28.

ffmpeg -i input.wmv -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -crf 22 \
    -threads 0 output.mp4

Two-Pass encode using the fast presets. Two-pass encoding is used when you are targeting a specific bitrate and/or final output file size.

ffmpeg -i input.avi -pass 1 -vcodec libx264 -vpre fast_firstpass -b 512k -bt 512k
    -threads 0 -f rawvideo -an -y /dev/null &amp;&amp; ffmpeg -i input.avi -pass 2 -acodec libfaac \
    -ab 128k -ac 2 -vcodec libx264 -vpre fast -b 512k -bt 512k -threads 0 output.mp4

iPod 640×480 using the slow and ipod640 presets. This example uses -aq to create VBR audio:

ffmpeg -i input.ogv -acodec libfaac -aq 100 -ac 2 -vcodec libx264 -vpre slow \
    -vpre ipod640 -crf 26 -map_meta_data 0:0 -vf scale=640:-1 -threads 0 output.mp4

Lossless H.264 in MKV container

ffmpeg -i input.foo -vcodec libx264 -vpre lossless_medium -threads 0 -acodec copy output.mkv

Updating FFmpeg and x264
Development of FFmpeg and x264 is active and an occasional update can give you new features and bug fixes. To update FFmpeg and x264 you will need to remove the packages, make distclean, update the source, recompile, and install. To update x264:

sudo apt-get remove ffmpeg x264 libx264-dev libvpx
cd ~/x264
make distclean
git pull

Now compile x264 as shown earlier in the guide starting with the x264 ./configure line. Now update FFmpeg:

cd ~/ffmpeg
make distclean
svn update

Finish the installation starting with the FFmpeg ./configure line.

Reverting Changes Made by This Guide
To remove FFmpeg/x264 and other packages added for this guide:

sudo apt-get remove x264 ffmpeg qt-faststart build-essential subversion git-core \
    checkinstall yasm texi2html libfaac-dev libjack-jackd2-dev libmp3lame-dev \
    libsdl1.2-dev libtheora-dev libvorbis-dev libvpx-dev libx11-dev libxfixes-dev \
    libxvidcore-dev zlib1g-dev

Lastly, delete the ffmpeg and x264 directories in your home folder.

Additional Resources

If You Need Help
Feel free to ask your questions here and I’ll try to answer you. Helpful information includes your Ubuntu version, names of any third-party repositories or PPAs you are using, FFmpeg command, and the complete FFmpeg output if applicable.

Recent Updates

2010-11-02: So apparently I lied and added LAME to the “retired” 9.04/8.10 guide. Also cleaned up the Hardy guide and finally added LAME to that guide too.

2010-11-01: Added libjack-jackd2-dev as a dependency to the Maverick guide to enable JACK as an input device.

2010-10-27: Retired the Ubuntu 9.04 (Jaunty Jackalope) and Ubuntu 8.10 (Intrepid Ibex) guides. Added x264 lossless example.