Linux

How to install OpenCV 2.0 on Ubuntu 9.10 Karmic Koala

OpenCV is a widely used computer vision library for research and development. I've tried using instructions for installing it from the official manual, but it failed miserably. It seems that 2.0 code is incompatible with the latest incarnation of ffmpeg. Couple minutes in googling and I had it working.

UPDATE The following process describes building the latest stable version from the sources. There is an easier way tin install it by using a third-party repository. Configure it in with add-apt-repo and install.

Here is what needs to be done if you want to build OpenCV from sources:

Get all the prerequisites:

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev
sudo apt-get build-dep libswscale-dev swig

Get OpenCV 2.0, and unpack it somewhere.

Fix ffmpeg links. Make sure you are root (sudo -s) and do the following:

mkdir /usr/include/ffmpeg
ln -s /usr/include/libavcodec/avcodec.h /usr/include/ffmpeg/avcodec.h
ln -s /usr/include/libavformat/avformat.h /usr/include/ffmpeg/avformat.h
ln -s /usr/include/libavformat/avio.h /usr/include/ffmpeg/avio.h
ln -s /usr/include/libavutil/avutil.h /usr/include/ffmpeg/avutil.h

Configure OpenCV. In the folder you've unpacked it to run:

./configure --prefix=/usr/local/opencv --enable-apps --enable-shared --enable-swscale --enable-gpl --with-swig
sudo ln -s /usr/include/libswscale/swscale.h /usr/include/ffmpeg/swscale.h

Make and install it:

make
sudo make installYou could also test it by running make test

Configure your system to look for opencv libraries:

echo /usr/local/opencv/lib > /etc/ld.so.conf.d/opencv.confReload the libraries and make sure opencv shows up now
ldconfig -v | grep opencvYou may get warnings on unaccessible lib paths, that is ok. You are looking for a line that says "/usr/local/opencv/lib" 

Set-up pkg-config:

echo 'export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv/lib/pkgconfig' >> /etc/bash.bashrc
echo 'export PYTHONPATH=/usr/local/opencv/lib/python2.6/site-packages/opencv' >> /etc/bash.bashrcRestart your terminal to get the new paths

Test by building samples:

cd ~/Programs/OpenCV-2.0.0/samples/c
. build_all.sh

You're done.