From ImageWiki

Jump to: navigation, search

This page contains a very soft and gentle introduction to OpenCV version 1.0.0. The OpenCV library is a powerful Image Processing and Computer Vision library written in C. The OpenCV homepage is here, and the project had a nice wiki as well.

In general, OpenCV is quite well-documented, so this page only provides some very simple examples to get you started. Due to the size of the OpenCV library, the documentation can be somewhat convoluted so you have to know what you are looking for. But don't worry, you'll quickly pick that up.

The OpenCV Structure

OpenCV consists of three core parts

  • cxcore provides a set of basic data structures. This includes 'images', 'matrices', 'points', and so forth. Besides this, cxcore also contains basic drawing functionality that allows you to draw lines, circles, etc, in images.
  • cv contains functions for image processing and computer vision. That is, functions for edge detection, camera calibration, optical flow, clustering and so forth.
  • highgui contains functions for building graphical user interfaces and for reading and writing images. That is, it contains functions that allows you to show images, and acquite them from either the disc or from a camera.

How to use OpenCV on Linux

One of the major strengths of OpenCV is its ease of use. Each of the three core parts of OpenCV has one header file that you should include in your C program. These are called: "cxcore.h", "cv.h" and "highgui.h".

To compile OpenCV programs you can use the pkg-config program. This simple provides you with the necesary compiler and linker flags. The command

pkg-config --cflags opencv

prints the list of arguments needed to allow the compiler to find the header files, while

pkg-config --libs opencv

prints the commands needed for linking. This makes it very easy to write Makefile's for your project. The following simple example shows how (you'll need to adapt it to your own programs).

# Compile and link flags
CXX          =  g++
CFLAGS       = -Wall -g

# Compilation (add flags as needed)
CXXFLAGS    += `pkg-config opencv --cflags`

# Linking (add flags as needed)
LDFLAGS     += `pkg-config opencv --libs`

# Name your target executables here
all         = test

# Default target is the first one - so we will have it make everything :-)
all: $(all)

    rm -f $(all) *.o

# Program dependencies (.o files will be compiles by implicit rules)
test: test.o MyOtherClass.o

Example 1

In the first example we'll grab images from a camera and show it on the screen. This is a very simple, yet quite usable, OpenCV program. The program can be found the Robot Eksperimentariums SVN under Examples/opencv/Eksempel1 and consists of the file and a corrsponding Makefile.

#include "highgui.h"

int main() 
    // Get an OpenCV camera handle
    CvCapture *cam = cvCaptureFromCAM(-1);

    // Initialise the GUI
    const char *window = "Eksempel 1";
    cvNamedWindow(window, CV_WINDOW_AUTOSIZE);

    // The main loop
    while (cvWaitKey(4) == -1) {
        IplImage *im = cvQueryFrame(cam);
        cvShowImage(window, im);

    // Clean up

    return 0;
Personal tools