Howto install OpenCV on linux

Share your own howto's etc. Not for support questions!

Howto install OpenCV on linux

Postby s5s » 2010-12-25 03:49

1. This howto describes a little bit more accurately how to install opencv on linux. The original howto can be found here.

2. First we need to install necessary dev files. A lot of the image format libraries are not necessary but I'm installing them anyway. You can see what's optional and what's not here.

Code: Select all
aptitude install libpng++-dev libjpeg62-dev libtiff4-dev libjasper-dev \
    libavcodec-dev cmake subversion gir1.0-gtk-2.0 libavformat-dev \
    libswscale-dev libdc1394-22-dev libv4l-dev libgstreamer0.10-dev \
    libgtk2.0-dev pkg-config

Next we want to get the current svn snapshot. Run this from the directory you want to have the folder containing the source code. Once this command has finished (it takes some time) you will find a new folder called opencv-main in your current directory.

NB: the official linux guide has an old directory. I will update it after the holidays.

Code: Select all
svn co

Next we want to compile and install. It's a good idea to compile the object files into a different directory from the source code. Therefore, we make a new directory in opencv-main, call it bin.

Code: Select all
mkdir bin
cd bin
sudo make install
sudo ldconfig -v

3. To compile programs you will need the program pkg-config (already installed it above). The program is used to ease the amount of typing you need to do when compiling with external libraries. It handles both includes and library references. To compile which uses an opencv header do the following.

Code: Select all
g++ `pkg-config --cflags --libs opencv` -o program1

Here is an example program which works. The program accepts an image as an argument and opens it.

Code: Select all
#include <highgui.h>

int main(int argc, char* argv[])
  IplImage* img = cvLoadImage( argv[1] );
  cvNamedWindow( "Example1", 0 );
  cvShowImage( "Example1", img );
  cvReleaseImage( &img );
  cvDestroyWindow( "Example1" );

  return 0;
Posts: 1
Joined: 2010-12-25 03:19

Re: Howto install OpenCV on linux

Postby janneman » 2013-05-25 22:38

HOW TO Compile your opencv code on debian wheezy

All tutorials I could find ask you to download and compile the opencv sources. This is rather unnecessary, so here is the easy way:

1. Install some packages

We need:
    the opencv development files, which are installed with the libopencv-dev package
    the pkg-config package
    g++, which is installed with the build-essential package
    and the libcanberra-gtk-module package to get rid of a warning when running the sample program.

Code: Select all
sudo apt-get install libopencv-dev pkg-config build-essential libcanberra-gtk-module

2. Compile a sample program

For example the following code (copied below for your comfort) from:

Code: Select all
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main( int argc, char** argv )
  Mat image;
  image = imread( argv[1], 1 );

  if( argc != 2 || ! )
      printf( "No image data \n" );
      return -1;

  namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
  imshow( "Display Image", image );


  return 0;

No need for cmake. The following Makefile is all we need:

Code: Select all
CC = g++

CFLAGS= -c -Wall

   $(CC) `pkg-config --cflags --libs opencv` DisplayImage.cpp -o DisplayImage

3. Test the program

Build your sample program:
Code: Select all

Put an HelloWorld.jpg image file in your project folder and run:
Code: Select all
./DisplayImage HelloWorld.jpg
Posts: 5
Joined: 2010-01-24 16:48

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 4 guests