OpenCV on Ubuntu Linux
hello
This page contains a "step-by-step" guide to getting opencv installed on an
Ubuntu system. Information here came from several sources, including the
helpful members of the OpenCV
yahoogroup mailing list.
These have been tested with the Kubuntu variant of Ubuntu
These pages have been updated in June 2007 with instructions for Ubuntu Dapper, in September 2007 for Ubuntu Feisty and July 2008 for Ubuntu Hardy
If you are running Ubuntu 6.06 (Dapper) please see the updated instructions here
If you are running Ubuntu 7.04 (Feisty) please see the updated instructions here
If you are running Ubuntu 8.04 (Hardy) please see the updated instructions here
Warning Because of the changing nature of open source software, some of these instructions are likely to be obsolete. In particular, be aware that ffmpeg has gone through a number of versions some of which do not work with OpenCV/Ubuntu. Fortunately, as from Ubuntu 7.10 it is not necessary to install ffmpeg from source but rather just libraries such as libavcodec, ...
Therefore, these pages are more a historical record of installations on different versions
of Ubuntu with the latest one being the one we are more confident it works!
We have tried the officially supported version of this, i.e. runnning KDE 3.x instead of 4.x
Note that installation on version 7.10 follows the same procedure, but I never got round documenting it
Things seem to be getting easier!
Thanks to Leeds University for their guide on using OpenCV
- sudo apt-get install build-essential to get compiler, ld, make, ....
- Install the other necessary packages: sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev
- Now download and untar version 1.0.0 of OpenCV from http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16948
- Now do
- ./configure --prefix=/opt/opencv --enable-apps --enable-shared --with-ffmpeg --with-gnu-ld --with-x --without-quicktime CXXFLAGS=-fno-strict-aliasing
- Check that the above produces no error and that in particular it reports ffmpeg as yes. If this is not the case there is no point in continuing!
- make (if you have tried making other configurations you could also do a make clean before make)
- sudo make install
- sudo vi /etc/ld.so.conf.d/opencv.conf and add the line /opt/opencv/lib. Then sudo ldconfig
- sudo vi /etc/bash.bashrc and add the lines:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/opencv/lib/pkgconfig
export PKG_CONFIG_PATH
(this setting will be activated if you launch a new console/terminal or if you re-login)
- So far so good? Now try running some sample programs. Donwload and untar this. Then:
./build-all.sh
edge fruits.jpg (it shows that you can open and display a jpg file)
cvplayer-v2 SEQ-003-C5_X4.mpg (it shows you can open and display an mpg video file, with ffmpeg)
You might want to use this distribution because it has corrected bugs from previous ones, supports your hardware better, etc.
Thanks to Leeds University for their guide on using OpenCV
- Install subversion and all the basic compilation packages such as g++ and gcc etc.
- Download ffmpeg with: svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg and then
- ./configure --enable-shared --prefix=/opt/ffmpeg (the prefix bit is optional but I prefer to store special development packages like this one on their own
directory so it is simpler to locate, configure, remove)
- make
- sudo make install
- The above will install ffmpeg in /opt/ffmpeg.
Now sudo vi /etc/ld.so.conf.d/ffmpeg.conf and add the line /opt/ffmpeg/lib to it. Then do sudo ldconfig
- Important: Modify file /opt/ffmpeg/include/ffmpeg/avformat.h by adding the following lines to it (otherwise you will get a compilation error when compiling OpenCV (see here for more details):
#define INT64_C
#define __STDC_CONSTANT_MACROS
#include <stdint.h>
- Install the other necessary packages: sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev
- Now download and untar version 1.0.0 of OpenCV from http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16948
- Now do
- ./configure --prefix=/opt/opencv --enable-apps --enable-shared --with-ffmpeg --with-gnu-ld --with-x --without-quicktime CXXFLAGS=-fno-strict-aliasing CFLAGS=-I/opt/ffmpeg/include CPPFLAGS=-I/opt/ffmpeg/include LDFLAGS=-L/opt/ffmpeg/lib
- Check that the above produces no error and that in particular it reports ffmpeg as yes. If this is not the case there is no point in continuing!
- make (if you have tried making other configurations you could also do a make clean before make)
- sudo make install
- sudo vi /etc/ld.so.conf.d/opencv.conf and add the line /opt/opencv/lib. Then sudo ldconfig
- Edit (as root) /etc/bash.bashrc and add the lines:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/opencv/lib/pkgconfig
export PKG_CONFIG_PATH
(this setting will be activated if you launch a new console/terminal or if you re-login)
- So far so good? Now try running some sample programs. Donwload and untar this. Then:
./build-all.sh
edge fruits.jpg (it shows that you can open and display a jpg file)
cvplayer-v2 SEQ-003-C5_X4.mpg (it shows you can open and display an mpg video file, with ffmpeg)
You might want to use this distribution because it has longer support, some packages seem to be more stable than later ones and for me at least it runs faster than 7.04!
Note that there is an older section (struck out), which I have kept as we recently had a case where it was necessary to install the older version of ffmpeg!
- Install subversion and all the basic compilation packages such as g++ and gcc etc.
- Download ffmpeg with: svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg and then
- ./configure --enable-shared --prefix=/opt/ffmpeg (the prefix bit is optional but I prefer to store special development packages like this one on their own
directory so it is simpler to locate, configure, remove)
- make
- sudo make install
- The above will install ffmpeg in /opt/ffmpeg.
Now sudo vi /etc/ld.so.conf and add the line /opt/ffmpeg/lib to it. Then do sudo ldconfig
- Important: Modify file /opt/ffmpeg/include/ffmpeg/avformat.h by adding the following lines to it (otherwise you will get a compilation error when compiling OpenCV (see here for more details):
#define INT64_C
#define __STDC_CONSTANT_MACROS
#include <stdint.h>
- Install the other necessary packages: sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev
- Now download and untar version 1.0.0 of OpenCV from http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16948
- Now do
- ./configure --prefix=/opt/opencv --enable-apps --enable-shared --with-ffmpeg --with-gnu-ld --with-x --without-quicktime CXXFLAGS=-fno-strict-aliasing CFLAGS=-I/opt/ffmpeg/include CPPFLAGS=-I/opt/ffmpeg/include LDFLAGS=-L/opt/ffmpeg/lib
- Check that the above produces no error and that in particular it reports ffmpeg as yes. If this is not the case there is no point in continuing!
- make (if you have tried making other configurations you could also do a make clean before make)
- sudo make install
- sudo vi /etc/ld.so.conf and add the line /opt/opencv/lib. Then sudo ldconfig
- Edit (as root) /etc/bash.bashrc and add the lines:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/opencv/lib/pkgconfig
export PKG_CONFIG_PATH
(this setting will be activated if you launch a new console/terminal or if you re-login)
- So far so good? Now try running some sample programs. Donwload and untar this. Then:
./build-all.sh
edge fruits.jpg (it shows that you can open and display a jpg file)
cvplayer-v2 SEQ-003-C5_X4.mpg (it shows you can open and display an mpg video file, with ffmpeg)
- First download an older version of ffmpeg from here (I found that newer versions generate all sort of errors when compiling OpenCV)
- ./configure --enable-shared
- make
- sudo make install (note: this will compile with gcc 4)
The above will install ffmpeg in /usr/local/lib. Add /usr/local/lib to /etc/ld.so.conf
- Install the other necessary packages as explained above e.g. libgtk2.0-dev, libavcodec-dev, libavformat-dev, libjpeg62-dev and libtiff4-dev
- Now download and untar version 1.0.0 of OpenCV from http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16948
- Important! Replace the file configure with this one (after downloading it change its name to "configure" and make it executable)
- Now do ./configure -n and make sure that ffmpeg is shown as "yes" (it should be, the doctored configure file bypasses checks that would otherwise fail).
- Now do
- sudo mkdir /opt/opencv (this is a quirk of mine, I like adding software where I can easily check/remove it later)
- ./configure --prefix=/opt/opencv
- make (if you have tried making other configurations you could also do a make clean before make)
- sudo make install
- Edit (as root) /etc/ld.so.conf adding the line /opt/opencv/lib
- sudo ldconfig
- Edit (as root) /etc/bash.bashrc and add the lines:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/opencv/lib/pkgconfig
export PKG_CONFIG_PATH
(this setting will be activated if you launch a new console/terminal or if you re-login)
- So far so good? Now try running some sample programs. Donwload and untar this. Then:
./build-all.sh
edge fruits.jpg (it shows that you can open and display a jpg file)
cvplayer-v2 SEQ-003-C5_X4.mpg (it shows you can open and display an mpg video file, with ffmpeg)
Installing OpenCV from the cvs version for Ubuntu 5
First, there are a few dependencies to sort out. You will need to install the following packages - using Synaptic.
- cvs - so you can get the files
- If you don't use cvs normally, type touch ~/.cvpass to create an empty password file (or else you'll get error messages)
- gcc - so you can compile them
- g++ - "
- libgtk2.0-dev
- Various image libraries - libimlib2 and libimlib2-dev get most of
them. OpenCV requires libpng, libjpeg and libtiff with the -dev
versions so check they are there too...
Second, you can't install OpenCV or FFmpeg from Synaptic (well you can,
but they won't deal with video files properly) so uninstall them.
Then...
- Obtain FFmpeg from the FFmpeg CVS repository:
- http://ffmpeg.sourceforge.net/ is the FFmpeg homepage.
Note: This is no longer available, try: svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
- The current release version of FFmpeg (0.4.9-pre1) does not work with GCC4, so the options are to either revert to an older and less strict compiler, or follow these instructions and get it working with the new shiny compiler.
- The command to get the code out of CVS is
cvs -z9 -d:pserver:anonymous@mplayerhq.hu:/cvsroot/ffmpeg co ffmpeg
- Build FFmpeg as a shared library
- In the FFmpeg directory, do:
- ./configure --enable-shared
If you get any error messages here, check the dependencies (see above)
- make
- sudo make install
- Get hold of OpenCV
- Get hold of OpenCV from CVS
cvs -z9
-d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/opencvlibrary co
opencv
- Install OpenCV
- ./configure
At the end of the configure process, a list of options will be displayed with either "no" or "yes" next to them. Check that FFmpeg is OK here - if it isn't, try
- Checking that you've not installed the libraries via synaptic: do a search for ffmpeg and check that livavformat-dev hasn't been installed
- Check that when you installed FFmpeg from source you used the --enable-shared switch
Don't bother proceeding with the installation unless configure tells you that ffmeg is OK (unless, of course, you don't mind working with static images rather than video, in which case you might as well use the synaptic installer)
- Run make by typing make
- Check there are no error messages
- sudo make install
- Add the line /usr/local/lib to the file /etc/ld.so.conf, as these libraries have been installed there rather than in /usr/lib.
- Finally, run ldconfig
Sergio A Velastin (with thanks to Alberto Colombo for guiding me on what was at times a painful process!)