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 https://code.ros.org/svn/opencv/trunk/opencv


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
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ../opencv/
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 program1.cc which uses an opencv header do the following.

Code: Select all
g++ `pkg-config --cflags --libs opencv` program1.cc -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 );
  cvWaitKey(-1);
  cvReleaseImage( &img );
  cvDestroyWindow( "Example1" );

  return 0;
}
s5s
 
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:
http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

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 || !image.data )
    {
      printf( "No image data \n" );
      return -1;
    }

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

  waitKey(0);

  return 0;
}


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

Code: Select all
CC = g++

CFLAGS= -c -Wall

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



3. Test the program

Build your sample program:
Code: Select all
make


Put an HelloWorld.jpg image file in your project folder and run:
Code: Select all
./DisplayImage HelloWorld.jpg
janneman
 
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

fashionable