OpenCV on Ubuntu

From STAB Resources

Jump to: navigation, search


Contents

What is OpenCV?

OpenCV (Open Source Computer Vision) is a library of programming functions for real time computer vision. As the name clearly implies, the library is free to use for everyone. Some common application areas of OpenCV include:

The library was originally written in C and later versions include code in C++ as well. OpenCV is a cross platform library and runs on Android, Linux, Mac OS, Windows. This short tutorial will guide you through the setup of OpenCV on Linux. Specifically Ubuntu 10.04 using synaptic.

All the steps followed are known to work on Ubuntu 10.04.

Pre-requisites

Before you install OpenCV you need a C++ Compiler like g++ already installed. So if you do not have g++ install it from synaptic or type the following command in the terminal:

sudo apt-get install g++

For displaying images OpenCV requires GTK+ 2.0 or higher. Type the following on your terminal:

dpkg -l | grep libgtk

If the output contains lines like libgtk2.0-0 etc. then you are good to install OpenCV. Else you'll first have to install GTK+ 2.0 libraries. You can do it through synaptic or type the following command:

sudo apt-get install libgtk2.0-dev

Once you have got both g++ and GTK+ installed, you can proceed to the actual OpenCV installation.

Installing OpenCV

1. Go to Synaptic Package Manager (System > Administration > Synaptic Package Manager)

2. Search for 'opencv' and install the following packages:

You will find the above or similar set of packages for installation. Install all of them.

3. Apply the changes in the Package Manager and let it install the libraries.

That should install OpenCV on your computer. Try building the following example code to test it out.

A Test Program

1. Create a new folder and create a file hello.cpp in the folder. Then using your favourite text editor, edit the file and copy the following code into it.

#include <cv.h> /* required to use OpenCV */
#include <highgui.h> /* required to use OpenCV's highgui */
#include <stdio.h>
 
int main(int argc, char *argv[]) {
 IplImage* img=0; /* pointer to an image */
 printf("Hello\n");
 if(argv[1] != 0)
  img = cvLoadImage(argv[1], 0); // 1 for color
 else
  printf("Enter filename\n");
 if(img != 0) {
  cvNamedWindow("Display", CV_WINDOW_AUTOSIZE); // create a window
  cvShowImage("Display", img); // show image in window
  cvWaitKey(0); // wait until user hits a key
  cvDestroyWindow("Display");
 }
 else
  printf("File not found\n");
return 0;
}

2. Next check the path where OpenCV and other library files are stored.

pkg-config --cflags opencv

The output should be -I/usr/include/opencv

pkg-config --libs opencv
should give the output as -lcxcore -lcv -lhighgui -lcvaux -lml

These paths are needed to compile openCV programs.

3. To compile the code and run type the following into the command prompt:

g++ -I/usr/include/opencv -lcxcore -lhighgui -lm hello.cpp
./a.out img

img is the path to any image on your computer with the extension. You should be able to see “Hello” and the image in a different window.

Another test program

If you get the above code running, try this another test code. I've saved the file as test.cpp.

#include<iostream>
#include<opencv/cv.h>
#include<opencv/highgui.h>
 
using namespace std;
using namespace cv;
 
int main()
{
	Mat rgb=imread("pic.jpg",1);
	Mat hsv;
	cvtColor(rgb,hsv,CV_RGB2HSV);
 
	imshow("original",rgb); //showing hsv will give same result
 
	Mat rgb_channels[3];
	Mat hsv_channels[3];
 
	split(rgb,rgb_channels);
	imshow("red",rgb_channels[0]);
	imshow("green",rgb_channels[1]);
	imshow("blue",rgb_channels[2]);
 
	split(hsv,hsv_channels);
	imshow("hue",rgb_channels[0]);
	imshow("saturation",rgb_channels[1]);
	imshow("value",rgb_channels[2]);
 
	cvWaitKey(0);
}

This code outputs the file pic.jpg (Make sure you put this file in the same folder as the .cpp file) and also splits it into different channels as per the RGB and HSV schemes.

Compile and Run the above code as mentioned earlier. You should see several windows pop up.

Making Life Simpler

As a last step, we'll simplify the big command and make it short for our convenience. Goto your home directory. Edit the file .bashrc using gedit or any other text editor. Add the following line to the file:

alias gcv="g++ -I/usr/include/opencv -lcv -lcxcore -lcvaux -lhighgui -lm"

and save it. Restart the terminal. Now you can compile and run your programs as simply as

gcv hello.cpp
./a.out

The best part of OpenCV is its extensive documentation - check out the OpenCV2.0 Reference.

Personal tools
Namespaces
Variants
Actions
Navigation
Clubs
Toolbox