Friday, 21 June 2013

OpenCV installation notes

One reason I started reading the OpenCV tutorial because I hope to postpone the 5 hour long boring job of installing the OpenCV software in my RPi.  I though if I read the CV tutorial too difficult, then I would forget installing the CV thing and delete all the files from my RPi.

But the, the first chapter of the tutorial is about installing CV in Linux and Eclipse etc.  So I think should not avoid this first thing and read carefully the following.

There are too many new terms I never met before - SWIG, Synaptic Manager, SVN, ...  I am too lazy to wiki or google again these new things.  So I think I just go back to the install Open CV in RPi tutorial and blindly following the instructions there.

.END


The OpenCV Tutorials, Release 2.3  1.1 Installation in Linux

These steps have been tested for Ubuntu 10.04 but should work with other distros.

Required packages

• GCC 4.x or later. This can be installed with

sudo apt-get install build-essential

• CMake 2.6 or higher

• Subversion (SVN) client

• GTK+2.x or higher, including headers

• pkgconfig

• libpng, zlib, libjpeg, libtiff, libjasper with development files (e.g. libpjeg-dev)

• Python 2.3 or later with developer packages (e.g. python-dev)

• SWIG 1.3.30 or later (only for versions prior to OpenCV 2.3)

• libavcodec

• libdc1394 2.x

All the libraries above can be installed via Terminal or by using Synaptic Manager

Getting OpenCV source code

You can use the latest stable OpenCV version available in sourceforge or you can grab the latest snapshot from the SVN repository:

Getting the latest stable OpenCV version

• Go to http://sourceforge.net/projects/opencvlibrary

• Download the source tarball and unpack it

Getting the cutting-edge OpenCV from SourceForge SVN repository

Launch SVN client and checkout either

1. the current OpenCV snapshot from here: 

https://code.ros.org/svn/opencv/trunk

2. or the latest tested OpenCV snapshot from here: 
http://code.ros.org/svn/opencv/tags/latest_tested_snapshot

In Ubuntu it can be done using the following command, e.g.:

cd ~/<my_working _directory>

svn co https://code.ros.org/svn/opencv/trunk


Building OpenCV from source using CMake, using the command line

1. Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated Makefiles, project files as well the object filees and output binaries

2. Enter the <cmake_binary_dir> and type

cmake [<some optional parameters>] <path to the OpenCV source directory>

For example

cd ~/opencv

mkdir release

cd release

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX= /usr/local

3. Enter the created temporary directory (<cmake_binary_dir>) and proceed with:

make

sudo make install





1.2 Using OpenCV with gcc and CMake

Note: We assume that you have successfully installed OpenCV in your workstation.

The easiest way of using OpenCV in your code is to use CMake. A few advantages (taken from the Wiki):

1. No need to change anything when porting between Linux and Windows

2. Can easily be combined with other tools by CMake( i.e. Qt, ITK and VTK )

• If you are not familiar with CMake, checkout the tutorial on its website.

Steps

Create a program using OpenCV

Let’s use a simple program such as DisplayImage.cpp shown below.

#include <cv.h>
#include <highgui.h>

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

Create a CMake file

Now you have to create your CMakeLists.txt file. It should look like this:

project( DisplayImage )

find_package( OpenCV REQUIRED )

add_executable( DisplayImage DisplayImage )

target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Generate the executable

This part is easy, just proceed as with any other project using CMake:

cd <DisplayImage_directory>

cmake .

make

Result

By now you should have an executable (called DisplayImage in this case). You just have to run it giving an image location as an argument, i.e.:

./DisplayImage lena.jpg

You should get a nice window as the one shown below:

...

.END

No comments:

Post a Comment