1. Abstract

The purpose of this document is to help a reader to get started with Computer Vision library OpenCV on Linux system. OpencCV is a multi-platform library, but this article will be focused only on OpenCV using Linux operating system ( although, just the installation of the OpenCV library and video camera is platform-specific, all examples in this article should compile on any platform where OpenCV is properly installed such as Mac OS, MS Windows and etc.). Reader will be guided through a step-by-step guide on how to install and use some of the basic functions of OpenCV library such as displaying images, playing a video or using a video camera to process a video input stream.

Conventions used in this article:

$ - execution on the command line by a non-privileged user

# - execution on the command line by a superuser

the actual command to be executed on the command line or code of program to be compiled

OUTPUT:output produced on the command line by command execution

NOTE: general notes and additional information

2. Introduction

In simple words a Computer Vision is a scientific field which attempts to provide a sight to the machine. This scientific field has expanded rapidly in recent years. Among researchers this growth is because of many improvements of vision algorithms and among the computer vision hobbyists this is due to the cheaper hardware components and processing power. OpenCV library plays a great role in the Computer Vision field as it helps greatly to reduce cost and preparation time of computer vision research environment needed by university students, hobbyists and professionals. OpenCV also provides a simple to use functions to get the work done in a simple, effective and elegant manner. OpenCV was started by Intel, and later it was transformed to an open source project now available on SourceForge.net. OpenCV library has multi-platform availability, and it is partially written in C++ and C language. Despite the fact that this library is available on many Linux distributions from its relevant package repositories, in this article we will attempt to install and use OpenCV library compiled from a source code downloaded from SourceForge.net web site.

more support is available for OpenCV 2.0.0 version than for former 1.0.0 version

This article will start with installation of OpenCV on Debian 5.0 ( Lenny ). Later a reader will be guided through a number of examples on how to use OpenCV to display an image, play a video and use camera to capture the video input stream.

3. Installation

The following section will describe an installation process of OpenCV library by building a binaries from a source code available from SourceForge.net. The installation procedure demonstrated here was tested on Debian 5.0 ( Lenny ) and Ubuntu 9.10 ( Karmic Koala ). The actual installation procedure should be similar or exactly same for most Linux distributions apart of the first step where package dependencies are installed from relevant Debian and Ubuntu distribution repositories. On RPM linux system you sould consult your Red Hat package management ( RPM ) tool for alternatives to OpenCV prerequisites described in the next section.

3.1. Prerequisites

First, what needs to be done is the installation of required prerequisites required by OpenCV library. The list of dependencies can be slightly modified according to your needs:

3.2. Obtaining OpenCV source code

Current version of OpenCV library at the time of writing is a version 2.0.0. You can download an OpenCV source code by pointing your web browser to OpenCV-SourceForge.net or use the wget command to acquire a source code directly on the command line:

3.3. Extract OpenCV source code

Whether you used web browser or wget utility to download source code of OpenCV library you should end up with OpenCV-2.0.0.tar.bz2 tarball in your current working directory. The next step is to extract source files with the tar command. The following command will extract all files into OpenCV-2.0.0 directory:

$ tar xvjf OpenCV-2.0.0.tar.bz2

New OpenCV-2.0.0 directory ( approx. 67MB ) should be now available in your current working directory and will contain all necessary source files for a compilation.

CMAKE_INSTALL_PREFIX=/usr/local : directory to be used as a installation destination

BUILD_PYTHON_SUPPORT : enable python support

NOTE: cmake utility by default does not provide a way to uninstall your project from a system. If you have a need to uninstall OpencCV from you system you should make appropriate changes before you proceed with compilation.

Navigate to OpenCV-2.0.0 directory containing a source code:

$ cd OpenCV-2.0.0/

Create and navigate to a new directory to be used by cmake. I this case, the directory name is same as project type, "release":

$ mkdir release; cd release

Use cmake to create a configuration files with configuration flags described above:

NOTE: CMAKE_INSTALL_PREFIX flag can be set to any desired installation path

When the execution of cmake command did not produce any errors, then we are ready to compile a source code.:

NOTE: There will be a number of warning messages shown on your terminal during a build process. These warning messages can be ignored, unless they do affect your preferred OpenCV environment settings!

$ make

If no errors were displayed on the terminal and the progress dialog reached [100%] during the build process, we are ready to install OpenCV libraries. The installation is optional as long as your environmental variable LD_LIBRARY_PATH is linked to an appropriate OpenCV built directory. If you wish to install OpenCV into /usr/local as set by cmake flags above, execute a following command:

# make install

Export correct path to LD_LIBRARY_PATH environment variable and use ldconfig to dynamically link to an OpenCV library:

$ export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH# ldconfig

If you do not wish to install OpenCV library you should just export a correct path to OpenCV library build directory to let your system know where the library is located. Suppose that your new release directory is located at ~/OpenCV-2.0.0/release then your export path will look like this:

4.1. An Image conversion

Let's start with something really simple and that is 7 lines of code to convert image between following image types:

Windows bitmaps - BMP, DIB

JPEG files - JPEG, JPG, JPE

Portable Network Graphics - PNG

Portable image format - PBM, PGM, PPM

Sun rasters - SR, RAS

TIFF files - TIFF, TIF

The following program will accept two command line arguments, source image and destination image. Source image will be stored as an image type specified by destination image file extension. Save the following code in a file called image-conversion.c :

Source code of our new program is ready and here comes the compilation part. Assuming that you have saved your first OpenCV program as image-conversion.c you can compile your program with the following command:

wget downloaded and saved an image image.png into your current directory, and we can now attempt to convert this image to any image type listed above. The following command will convert image type PNG to JPG. Assuming that program compilation did not produce any errors and your binary file is saved as image-conversion you can convert between two image types with following command:

$ ./image-conversion image.png image.jpg

To confirm that image was converted, a file command can be used to display a file type for a given file as an argument:

When you look at the compilation command once more you can observe that a pkg-config utility had been used to retrieve a location of an OpenCV library with the use of --cflags option as well as to get all dependencies using --libs option. Therefore, an alternative command to the one above without pkg-config utility can be constructed to look something like this:

Our program is dependent on OpenCv's highgui.h library and therefore including -lcvaux -lml -lcxcore and -lcv dependencies into a compilation command is not necessary. A shortened version of compilation command will look like this:

From now on, it is up to you how you compile following examples in this article. Keep in mind that the first compile command including pkg-config will be able to compile all examples. However, it may produce a binary with excessive dependencies.

4.2. Display an Image

At this point, we have been able to convert an image type and confirm its meta description by file command. It is time to display an image on the screen and visually confirm that it was converted correctly. The following example program will display an image on the screen:

Execution of this display-image program with an image.jpg produced in preceding section will display this image on the screen:

$ display-image image.jpg

OUTPUT:

4.3. Gaussian smooth

You can also attempt to create a simple image transformation using the gaussian smooth method. Add a following line into your display-image code before a cvShowImage function call:...

cvNamedWindow( argv[1], CV_WINDOW_AUTOSIZE );

cvSmooth( img, img, CV_GAUSSIAN, 9, 9 );

cvShowImage( argv[1], img );...and add as a first line to your program ' #include "cv.h" ' directive.

This will incorporate a gaussian smooth method centered on each pixel with 9 x 9 area into the output image. After compilation and execution a following output will be presented:OUTPUT:

4.4. Play video

This section includes a program code which will create a simple video player using OpenCV library. Sample video, tree.avi can be found in your OpenCV-2.0.0 directory where you have extracted its source files ( OpenCV-2.0.0/samples/c/tree.avi ) :

// create a window with appropriate size. Windows name is determined by file name// supplied as an argumentcvNamedWindow( argv[1], CV_WINDOW_AUTOSIZE );// open videog_capture = cvCreateFileCapture( argv[1] );// set read position in units of frames and retrieve total number of framesint frames = (int) cvGetCaptureProperty(

g_capture, CV_CAP_PROP_FRAME_COUNT);

// do not create treackbar if video does not include an information// about number of framesif( frames!=0 ) {

Execute your new OpenCV program and as an argument supply a video file:

$ ./video-player ~/OpenCV-2.0.0/samples/c/tree.avi

OUTPUT:

4.5. Input from a video camera

The aim of this section is to provide some simple tips on how to configure a camera on a linux system and how to confirm that your video camera is recognized by your system correctly. When your camera is ready, you will be presented with a simple program which is capable to display a video using a video camera as an input.

For this article I have used a Logitech, Inc. QuickCam Pro 9000 camera. Installation of this camera on Debian 5.0 or Ubuntu 9.10 ( Karmic Koala ) system was simple Plug & Play procedure. Here are some hints on how to confirm that your camera had been recognized by your system:NOTE: your output will be different !

lsusb command reveals a camera type plugged into your system. Output of lsusb command does not necessary means that your camera is now ready to use. Let's see if some modules are associated with video:

This looks very promising. My camera is using uvcvideo module. If you do not see any ouptut or you see only output not related to your camera device you may need to recompile your kernel or install an appropriate module.

Now we need to find a device file corresponding with your camera. To do that we use xawtv utility:

NOTE: if xawtv command is not avaialable you need to install xawtv package.

The device file assciated with my camera is /dev/video0 . You may also see an error on your terminal saying: open /dev/video0: Permission denied. To fix this problem you need to make your self a part of a group "video". Now test your camera with a following command:

$ xawtv -c /dev/video0

If you had some issues in some of the previous steps, here are some links, which may assist you to troubleshoot your problem:

Notice that a function cvCreateCameraCapture() did not take any specific device file or argument. In this case OpenCV will start using the first available camera in your system. Compile and execute this program and if everything up to this point went well you should see yourself on your screen.

4.6. Write avi file from a camera

The last example will attempt to read an input from a camera and write it to a file. In the meantime, the program will also display a window with a camera input video stream. The Video input is saved to a file supplied as an argument on the command line. The codec used is specified by FOURCC ( Four Character Code ) MJPG which in this case is Motion JPEG. This sample program is very basic and there is a plenty of room for improvement:

#include <cv.h>#include <highgui.h>main( int argc, char* argv[] ) {

CvCapture* capture = NULL; capture = cvCreateCameraCapture( 0 );

IplImage *frames = cvQueryFrame(capture);

// get a frame size to be used by writer structureCvSize size = cvSize ( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),

Assuming that you saved and compiled this programm as "save-camera-input" you can strat recording a video to a video-file.avi with this command: NOTE: Return to an image conversion section above, if you need help on how to compile this OpenCV program.

$ ./save-camera-input video-file.avi

5. Conclusion

This article should give a you a good start to OpenCV library from an installation perspective. Examples presented do not have much to do with Computer Vision itself, but rather they provide a good testing ground for your OpenCV installation. Even from these simple OpenCV examples it is also clear that OpenCV is a highly civilized library since with just couple lines of OpenCV code you can achieve great results. Your comment on this article is highly appreciated as it may have a great impact on article quality. There is more comming about OpenCV, so stay tuned by subcribing to linuxconfig.org RSS feed ( top left corner ).