Sie sind auf Seite 1von 67

Introduction to programming with OpenCV 2.

0
Mohamed EL ANSARI
Visiting Scholar
EMARO - Erasmus Mundus Master
Ecole Centrale de Nantes, Nantes Cedex, France
Associate Professor
Department of Computer Science, Faculty of Sciences,
University of Ibn Zohr
Agadir, Morocco

melansari@gmail.com
m.elansari@univ-ibnzohr.ac.ma

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

1 / 58

Outline

1
2
3
4
5
6
7

Introduction
GUI commands
Basic OpenCV structures.
Working with images
Working with matrices
Working with videos
Some OpenCV applications

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

2 / 58

Introduction

Description of OpenCV

What is OpenCV?
OpenCV (Open Source Computer Vision) is a library of programming
functions for real time computer vision.
It was founded at Intel in 1999. It is now under active development
and supported by Willow Garage.
OpenCV is free for both academic and commercial use.
Is extensively used in many companies and research centers
It has C++, C, Python and soon Java interfaces running on
Windows, Linux and Mac.
The library has more than 500 optimized algorithms.
It is used around the world (more than 2.5M downloads and 40K
people in the user group.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

3 / 58

Introduction

Description of OpenCV

What is OpenCV?

Was founded to advance the field of computer vision.


It gives everyone a reliable, real time infrastructure to build on.
It collects and makes available the most useful algorithms.

Uses range from interactive art, to mine inspection, stitching maps on


the web on through advanced robotics.
Was founded to advance the field of computer vision.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

4 / 58

Introduction

Description of OpenCV

What is OpenCV?

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

5 / 58

Introduction

Description of OpenCV

Useful OpenCV links

OpenCV wiki:
http://opencv.willowgarage.com/wiki/
User group (help):
http://tech.groups.yahoo.com/group/OpenCV/
OpenCV Code Repository:
http://sourceforge.net/projects/opencvlibrary/
Other links:
http://www.cs.iit.edu/ agam/cs512/lect-notes/opencv-intro/opencvintro.html
http://www.laganiere.name/opencv1Tut/

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

6 / 58

Introduction

Description of OpenCV

OpenCV Books
Learning OpenCV: Computer
Vision with the OpenCV Library.
OpenCV 2 Computer Vision
Application Programming
Cookbook.

Code examples from the book:


http://examples.oreilly.com/9780596516130/

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

7 / 58

Introduction

Description of OpenCV

OpenCV timeline

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

8 / 58

Introduction

Description of OpenCV

Features

Image data manipulation (allocation, release, copying, setting,


conversion).
Image and video I/O (file and camera based input, image/video file
output).
Matrix and vector manipulation and linear algebra routines (products,
solvers, eigenvalues, SVD).
Various dynamic data structures (lists, queues, sets, trees, graphs).
Basic image processing (filtering, edge detection, corner detection,
sampling and interpolation, color conversion, morphological
operations, histograms, image pyramids).

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

9 / 58

Introduction

Description of OpenCV

Features

Structural analysis (connected components, contour processing,


distance transform, various moments, template matching, Hough
transform, polygonal approximation, line fitting, ellipse fitting,
Delaunay triangulation).
Camera calibration (finding and tracking calibration patterns,
calibration, fundamental matrix estimation, stereo correspondence).
Motion analysis (optical flow, motion segmentation, tracking).
Object recognition (eigen-methods).
Basic GUI (display image/video, keyboard and mouse handling,
scroll-bars).
Image labeling (line, conic, polygon, text drawing)

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

10 / 58

Introduction

Description of OpenCV

OpenCV structure and content

The basic structure of OpenCV

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

11 / 58

Introduction

Description of OpenCV

OpenCV structure and content

cv - Main OpenCV functions.


Basic image processing and higher-level computer vision algorithms.

cxcore - Data structures and linear algebra support.


highgui - GUI functions.
I/O routines and functions for storing and loading video and images

Machine learning (ml) library Includes many statistical classifiers and clustering tools.

cvaux - Auxiliary (experimental) OpenCV functions.


Contains defunct areas and experimental algorithms. Less used as some
of its features migrated to CV.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

12 / 58

Introduction

OpenCV naming conventions

OpenCV naming conventions . . . . . . . . . . . . 1/2

Functions naming conventions: cvActionTargetMod(...)


Action = the core functionality (e.g. set, create)
Target = the target image area (e.g. contour, polygon)
Mod = optional modifiers (e.g. argument type)

Matrix data types:


CV_<bit_depth>(S|U|F)C<number_of_channels>
S = Signed integer
U = Unsigned integer
F = Float

E.g.: CV_8UC1 means an 8-bit unsigned single-channel matrix,


CV_32FC2 means a 32-bit float matrix with two channels.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

13 / 58

Introduction

OpenCV naming conventions

OpenCV naming conventions . . . . . . . . . . . . 2/2

Image data types: IPL_DEPTH_<bit_depth>(S|U|F)


E.g.: IPL_DEPTH_8U means an 8-bit unsigned image.
IPL_DEPTH_32F means a 32-bit float image.
Header files:
#include
#include
#include
#include

<cv.h>
<cvaux.h>
<highgui.h>
<cxcore.h> // unnecessary - included in cv.h

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

14 / 58

Introduction

Compilation instructions

Compilation instructions

Linux:
g++ hello-world.cpp -o hello-world -I /usr/local/include/opencv -L
/usr/local/lib -lm -lcv -lhighgui -lcvaux
Windows:
In the project preferences set the path to the OpenCV header files
and the path to the OpenCV library files.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

15 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples
First program: load and display an image
//Including needed header files
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv )
{
//Load an image
IplImage* img = cvLoadImage( argv[1] );
//Create a window
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
//Show the image
cvShowImage("Example1", img);
//Wait for a key
cvWaitKey(0);
//Release the image
cvReleaseImage( &img );
//Destroy all windows
cvDestroyWindow("Example1");
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

16 / 58

Introduction

Program examples

Program examples

Compiling:
g++ Program-1.cpp -o Program-1\
-I/usr/include/opencv -lhighgui
Output:
Running:
./Program-1 LombardStreet-SF.tif

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

17 / 58

Introduction

Program examples

Program examples

Compiling:
g++ Program-1.cpp -o Program-1\
-I/usr/include/opencv -lhighgui
Output:
Running:
./Program-1 LombardStreet-SF.tif

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

17 / 58

Introduction

Program examples

Program examples

Output:
Compiling:
g++ Program-1.cpp -o Program-1\
-I/usr/include/opencv -lhighgui
Running:
./Program-1 LombardStreet-SF.tif

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

17 / 58

GUI commands

Window management

Create a window:
int cvNamedWindow(
const char* name, // Window name
int flags = CV_WINDOW_AUTOSIZE // flag
);
Sets window size:
void cvResizeWindow(
const char* name, // Name of the window to be resized
int width, // New width
int height // New height
);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

18 / 58

GUI commands

Window management
Destroys a window:
void cvDestroyWindow(
const char* name // Name of the window
to be destroyed
);
Destroys all the HighGUI windows:
void cvDestroyAllWindows(void);
Sets window position:
void cvMoveWindow(
const char* name, // Name of the window to be moved
int x, // New x coordinate
int y ); // New y coordinate

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

19 / 58

GUI commands

Window management

Shows the image in the specified window:


void cvShowImage(
const char* name, // Name of the window
const CvArr* image ); // Image to be shown
Waits for a pressed key:
int cvWaitKey(
int delay=0 // Delay in milliseconds
);
Waits for key event infinitely (delay<=0) or for delay milliseconds.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

20 / 58

GUI commands

Loading and saving images

Loading and saving images


Loads an image from file:
IplImage* cvLoadImage(
const char* filename, //Name of file to be loaded
int flags=CV_LOAD_IMAGE_COLOR // Specifies colorness
and depth
of the loaded image
);
The colorness specifies whether the loaded image is to be converted to
3 channels (CV_LOAD_IMAGE_COLOR), 1 channel
(CV_LOAD_IMAGE_GRAYSCALE), or left as it was in the input file
(CV_LOAD_IMAGE_ANYCOLOR).
Formats supported: Windows bitmaps - BMP, DIB,JPG, PNG,
PBM,PGM,PPM, TIF,

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

21 / 58

GUI commands

Loading and saving images

Loading and saving images

Saves an image to the file:


int cvSaveImage(
const char* filename, // Name of the file
const CvArr* image // Image to be saved
);
The image format is chosen depending on the filename extension,
see cvLoadImage. Only 8-bit single-channel or 3-channel (with BGR
channel order) images can be saved using this function.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

22 / 58

GUI commands

Loading and saving images

Loading and saving image

#include"highgui.h"
int main(int argc, char** argv)
{
// Create a named window with the name of the file.
cvNamedWindow( argv[1], 1 );
// Load the image from the given file name.
IplImage* img = cvLoadImage( argv[1] );
// Show the image in the named window
cvShowImage( argv[1], img );
// Wait until the user hits a key.
cvWaitKey(0 );
// Save the image dans other file
cvSaveImage("ImageFile.jpg",img);
// Clean up
cvDestroyWindow( argv[1] );
cvReleaseImage( &img );
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

23 / 58

GUI commands

Video I/O functions

Video I/O functions


Video capturing structure:
typedef struct CvCapture CvCapture;
This structure contains the information needed for reading frames
from a camera or video file.
Initializes capturing video from file:
CvCapture* cvCreateFileCapture( const char* filename
);
Allocates and initialized the CvCapture structure for reading the
video stream from the specified file.
Initializes capturing video from camera:
CvCapture* cvCreateCameraCapture( int index );
index: Index of the camera to be used. If there is only one camera or
it does not matter what camera to use -1 may be passed. The
function allocates and initialized the CvCapture structure for reading
a video stream from the camera.
Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

24 / 58

GUI commands

Video I/O functions

Video I/O functions


Releases the CvCapture structure:
void cvReleaseCapture( CvCapture** capture );
capture: pointer to video capturing structure. It releases the
CvCapture structure allocated by cvCreateFileCapture or
cvCreateCameraCapture.
Grabs file from camera or file:
int cvGrabFrame( CvCapture* capture );
capture: video capturing structure.
Grabs the frame from camera or file. The grabbed frame is stored
internally.
Gets the image grabbed with cvGrabFrame:
IplImage* cvRetrieveFrame( CvCapture* capture);
Returns the pointer to the image grabbed with cvGrabFrame function.
Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

25 / 58

GUI commands

Video I/O functions

Video I/O functions

Grabs and returns a frame from camera or file:


IplImage* cvQueryFrame( CvCapture* capture );
grabs a frame from camera or video file, decompresses and returns it.
This function is just a combination of cvGrabFrame and
cvRetrieveFrame in one call.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

26 / 58

GUI commands

Video I/O functions

Video I/O functions


Example

#include<highgui.h>
main()
{
IplImage *im, *imm;
CvCapture * capture;
capture = cvCreateCameraCapture(-1);
cvGrabFrame(capture);
im =cvRetrieveFrame(capture);
cvNamedWindow("Win");
cvShowImage("Win",im);
cvWaitKey(0);
cvReleaseImage(&im);
cvReleaseCapture(&capture);
return 0;
}
Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

27 / 58

Basic openCV structures

Image data structure

Image data structure


IPL image:
IplImage
|-- int nChannels;
//
|-- int depth;
//
|
//
|
//
|
//
|
//
|-- int width;
//
|-- int height;
//
|-- char* imageData;
//
|
//
|-- int dataOrder;
//
|
//
|
//
|-- int origin;
//
|
//
|-- int widthStep;
//
|-- int imageSize;
//
|-- struct _IplROI *roi;//
|
//
|-|

Mohamed EL ANSARI (EMARO-ECN)

Number of color channels (1,2,3,4)


Pixel depth in bits:
IPL_DEPTH_8U, IPL_DEPTH_8S,
IPL_DEPTH_16U,IPL_DEPTH_16S,
IPL_DEPTH_32S,IPL_DEPTH_32F,
IPL_DEPTH_64F
image width in pixels
image height in pixels
pointer to aligned image data
Note that color images are stored in BGR order
0 - interleaved color channels,
1 - separate color channels
cvCreateImage can only create interleaved images
0 - top-left origin,
1 - bottom-left origin (Windows bitmaps style)
size of aligned image row in bytes
image data size in bytes = height*widthStep
image ROI. when not NULL specifies image
region to be processed.

Introduction to programming with OpenCV 2.0

28 / 58

Basic openCV structures

Matrices

Matrices

CvMat
// 2D array
|-- int
type;
// elements type (uchar,short,int,float,double) and flags
|-- int
step;
// full row length in bytes
|-- int
rows, cols;
// dimensions
|-- int
height, width; // alternative dimensions reference
|-- union data;
|-- uchar* ptr;
// data pointer for an unsigned char matrix
|-- short* s;
// data pointer for a short matrix
|-- int*
i;
// data pointer for an integer matrix
|-- float* fl;
// data pointer for a float matrix
|-- double* db;
// data pointer for a double matrix

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

29 / 58

Basic openCV structures

Matrices

Matrices

CvMatND
// N-dimensional array
|-- int
type;
// elements type (uchar,short,int,float,double) and flags
|-- int
dims;
// number of array dimensions
|-- union data;
|
|-- uchar* ptr;
// data pointer for an unsigned char matrix
|
|-- short* s;
// data pointer for a short matrix
|
|-- int*
i;
// data pointer for an integer matrix
|
|-- float* fl;
// data pointer for a float matrix
|
|-- double* db;
// data pointer for a double matrix
|
|-- struct dim[];
// information for each dimension
|-- size;
// number of elements in a given dimension
|-- step;
// distance between elements in a given dimension

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

30 / 58

Basic openCV structures

Matrices

Matrices

Even though OpenCV is implemented in C, the structures used in OpenCV have an


object-oriented design; in effect, IplImage is derived from CvMat, which is derived from CvArr

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

31 / 58

Basic openCV structures

Other data structures

Other data structures

Points:
CvPoint
p = cvPoint(int x, int y);
CvPoint2D32f p = cvPoint2D32f(float x, float y);
CvPoint3D32f p = cvPoint3D32f(float x, float y, float z);
E.g.: p.x=5.0; p.y=5.0;
Rectangular dimensions:
//Pixel accurate size
CvSize
r = cvSize(int width, int height);
//Sub-pixel accurate size
CvSize2D32f r = cvSize2D32f(float width, float height);
Rectangular dimensions with offset:
CvRect r = cvRect(int x, int y, int width, int height);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

32 / 58

Basic openCV structures

Other data structures

Other data structures

A container for 1-,2-,3- or 4-tuples of numbers:


typedef struct CvScalar
{
double val[4];
}
CvScalar;
/* the constructor function */
inline CvScalar cvScalar( double val0, double val1=0,
double val2=0, double val3=0 );

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

33 / 58

Basic openCV structures

Other data structures

Other data structures

The inline constructors for the data types listed in the table below cvPointXXX(),
cvSize(), cvRect(), and cvScalar()are extremely useful because they make your code not
only easier to write but also easier to read.

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

34 / 58

Basic openCV structures

Other data structures

Other data structures

Using 2D structures to draw a rectangle


#include "highgui.h"
using namespace std;
int main(int argc, char** argv )
{
IplImage* img = cvLoadImage( argv[1], -1 );
// Create a rectangle
cvRectangle(img,cvPoint(5,100),cvPoint(200,130),
cvScalar(255,255,255));
// Save the modified image
cvSaveImage("ImRec.jpg",img);
cvReleaseImage( &img );
}

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

35 / 58

Working with images

Allocating and releasing images

Allocating and releasing images


Allocate an image:
IplImage* cvCreateImage(CvSize size, int depth, int channels);
size: cvSize(width,height);
depth: pixel depth in bits: IPL DEPTH 8U, IPL DEPTH 8S, IPL DEPTH 16U,
IPL DEPTH 16S, IPL DEPTH 32S, IPL DEPTH 32F, IPL DEPTH 64F
channels: Number of channels per pixel.
Examples:
// Allocate a 1-channel byte image
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
Release image data:
void cvReleaseImage( IplImage** image );
Makes a full copy of an image (Clone):
IplImage* cvCloneImage( const IplImage* image);
Example
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
IplImage* img2;
img2=cvCloneImage(img1);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

36 / 58

Working with images

Accessing image elements

Accessing image elements

Assume that you need to access the k-th channel of the pixel at the i-th row and j-th
column.
For a single-channel byte image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
((uchar *)(img->imageData + i*img->widthStep))[j]=111;
For a multi-channel byte image:

IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R
For a multi-channel float image:

IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

37 / 58

Working with images

Accessing image elements

Accessing image elements


Simplified method

A simplified method to access image elements:


template<class T> class Image
{
private:
IplImage* imgp;
public:
Image(IplImage* img=0) imgp=img;
~Image()imgp=0;
void operator=(IplImage* img) imgp=img;
inline T* operator[](const int rowIndx)
return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));
};
typedef struct{
typedef Image<RgbPixel>
RgbImage;
unsigned char b,g,r;
typedef Image<RgbPixelFloat> RgbImageFloat;
} RgbPixel;
typedef Image<unsigned char> BwImage;
typedef struct{
typedef Image<float>
BwImageFloat;
float b,g,r;
} RgbPixelFloat;

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

38 / 58

Working with images

Accessing image elements

Accessing image elements


Simplified method

For a single-channel byte image:


IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
BwImage imgA(img);
imgA[i][j] = 111;
For a multi-channel byte image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
RgbImage imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;
For a multi-channel float image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
RgbImage imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

39 / 58

Working with images

Image conversion

Image conversion
Conversion between image formats:
void cvConvertImage(const CvArr* src,CvArr* dst, int flags = 0 ); src =
float/byte grayscale/color image
dst = byte grayscale/color image
flags = CV CVTIMG FLIP (flip vertically)
CV CVTIMG SWAP RB (swap the R and B channels)

From left to right: Original, CV_CVTIMG_SWAP_RB,CV_CVTIMG_FLIP,0.


Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

40 / 58

Working with images

Image conversion

Image conversion
Conversion between color spaces :
void cvCvtColor(const CvArr* src,CvArr* dst, int code ); code
<X>/<Y> = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS
e.g.: CV_BGR2GRAY, CV_BGR2HSV, CV_BGR2Lab

= CV_<X>2<Y>

(left) RGB, (middle) HSV, (right) YCrCb

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

41 / 58

Working with images

Drawing commands

Drawing commands

Draw a box:
// draw a box with red lines of
width 1 between (100,100) and (200,200)
cvRectangle(img, cvPoint(100,100),\
cvPoint(200,200), cvScalar(255,0,0), 1);
Draw a circle:
// draw a circle at (100,100) with a radius
of 20. Use green lines of width 1
cvCircle(img, cvPoint(100,100), 20,\
cvScalar(0,255,0), 1);
Draw a line segment:
// draw a green line of width 1
between (100,100) and (200,200)
cvLine(img, cvPoint(100,100), cvPoint(200,200),\
cvScalar(0,255,0), 1);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

42 / 58

Working with matrices

Allocating and releasing matrices

Allocating and releasing matrices

Allocate a matrix: CvMat* cvCreateMat(int rows, int cols, int type); type: Type
of the matrix elements. Specified in form
CV_<bit_depth>(S|U|F)C<number_of_channels>.
E.g.: CV_8UC1 means an 8-bit unsigned single-channel matrix, CV_32SC2 means a 32-bit
signed matrix with two channels.
Example: CvMat* M = cvCreateMat(4,4,CV_32FC1);
Release a matrix:
CvMat* M = cvCreateMat(4,4,CV_32FC1);
cvReleaseMat(&M);
Clone a matrix:
CvMat* M1 = cvCreateMat(4,4,CV_32FC1);
CvMat* M2;
M2=cvCloneMat(M1);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

43 / 58

Working with matrices

Allocating and releasing matrices

Allocating and releasing matrices

// Create a new rows by cols matrix of type type.


CvMat* cvCreateMat( int rows, int cols, int type );
// Create only matrix header without allocating data
CvMat* cvCreateMatHeader( int rows, int cols, int type);
// Initialize header on existing CvMat structure
CvMat* cvInitMatHeader(CvMat* mat, int rows, int cols,
int type, void* data = NULL, int step = CV_AUTOSTEP);
// Like cvInitMatHeader() but allocates CvMat as well.
CvMat cvMat(int rows, int cols, int type, void* data= NULL );

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

44 / 58

Working with matrices

Allocating and releasing matrices

Allocating and releasing matrices

Initialize a matrix:
double a[] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12 };
CvMat Ma=cvMat(3, 4, CV_64FC1, a);
Alternatively:
CvMat Ma;
cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

45 / 58

Working with matrices

Accessing matrix elements

Accessing matrix elements

Assume that you need to access the (i, j) cell of a 2D float matrix.
Indirect matrix element access:
double cvmGet( const CvMat* mat, int row, int col );
void cvmSet( CvMat* mat, int row, int col,double value);
Direct matrix element access assuming possible alignment gaps:
CvMat* M
= cvCreateMat(4,4,CV_32FC1);
int
step = M->step/sizeof(float);
float *data = M->data.fl;
(data+i*step)[j] = 3.0;

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

46 / 58

Working with matrices

Matrix/vector operations

Matrix/vector operations

Matrix-matrix operations:
CvMat *Ma, *Mb, *Mc;
cvAdd(Ma, Mb, Mc);
cvSub(Ma, Mb, Mc);
cvMatMul(Ma, Mb, Mc);

// Ma+Mb
// Ma-Mb
// Ma*Mb

-> Mc
-> Mc
-> Mc

Elementwise matrix operations:


CvMat *Ma, *Mb, *Mc;
cvMul(Ma, Mb, Mc);
// Ma.*Mb -> Mc
cvDiv(Ma, Mb, Mc);
// Ma./Mb -> Mc
cvAddS(Ma, cvScalar(-10.0), Mc); // Ma.-10 -> Mc

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

47 / 58

Working with matrices

Matrix/vector operations

Matrix/vector operations

Vector products:
double va[] = {1, 2, 3};
double vb[] = {0, 0, 1};
double vc[3];
CvMat Va=cvMat(3, 1, CV_64FC1, va);
CvMat Vb=cvMat(3, 1, CV_64FC1, vb);
CvMat Vc=cvMat(3, 1, CV_64FC1, vc);
// dot product:
Va . Vb -> res
double res=cvDotProduct(&Va,&Vb);
// cross product: Va x Vb -> Vc
cvCrossProduct(&Va, &Vb, &Vc);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

48 / 58

Working with matrices

Matrix/vector operations

Matrix/vector operations
Inhomogeneous linear system solver:
CvMat* A =
CvMat* x =
CvMat* b =
cvSolve(&A,

cvCreateMat(3,3,CV_32FC1);
cvCreateMat(3,1,CV_32FC1);
cvCreateMat(3,1,CV_32FC1);
&b, &x);
// solve (Ax=b) for x

Eigen analysis (of a symmetric matrix):


CvMat* A = cvCreateMat(3,3,CV_32FC1);
CvMat* E = cvCreateMat(3,3,CV_32FC1);
CvMat* l = cvCreateMat(3,1,CV_32FC1);
cvEigenVV(&A, &E, &l);
// l = eigenvalues of A (descending order)
// E = corresponding eigenvectors (rows)
Singular value decomposition:
CvMat* A =
CvMat* U =
CvMat* D =
CvMat* V =
cvSVD(A, D,

cvCreateMat(3,3,CV_32FC1);
cvCreateMat(3,3,CV_32FC1);
cvCreateMat(3,3,CV_32FC1);
cvCreateMat(3,3,CV_32FC1);
U, V, CV_SVD_U_T|CV_SVD_V_T); // A = U D V^T

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

49 / 58

Working with video files

Capturing a frame from a video sequence

Capturing a frame from a video sequence


OpenCV supports capturing images from a camera or a video file. (AVI).
Initializing capture from a camera:
CvCapture* capture = cvCaptureFromCAM(0);
// capture from video device #0
Initializing capture from a file:
CvCapture* capture = cvCaptureFromAVI("infile.avi");
Capturing a frame:
IplImage* img = 0;
if(!cvGrabFrame(capture)){
// capture a frame
printf("Could not grab a frame\n\7");
exit(0);
}
// retrieve the captured frame
img=cvRetrieveFrame(capture);
Releasing the capture source:
cvReleaseCapture(&capture);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

50 / 58

Working with video files

Getting/setting frame information

Getting/setting frame information

Get capture device properties:


cvQueryFrame(capture); // this call is necessary to
// capture properties
int frameH
= (int) cvGetCaptureProperty(capture,
int frameW
= (int) cvGetCaptureProperty(capture,
int fps
= (int) cvGetCaptureProperty(capture,
int numFrames = (int) cvGetCaptureProperty(capture,

get correct

CV_CAP_PROP_FRAME_HEIGHT);
CV_CAP_PROP_FRAME_WIDTH);
CV_CAP_PROP_FPS);
CV_CAP_PROP_FRAME_COUNT);

Get frame information:

float posMsec
=
cvGetCaptureProperty(capture, CV_CAP_PROP_POS_MSEC);
int posFrames
= (int) cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
float posRatio =
cvGetCaptureProperty(capture,
CV_CAP_PROP_POS_AVI_RATIO);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

51 / 58

Working with video files

Saving a video file

Saving a video file

Writing the video file:


IplImage* img = 0;
int nFrames = 50;
for(i=0;i<nFrames;i++){
cvGrabFrame(capture);
img=cvRetrieveFrame(capture);
cvWriteFrame(writer,img);
}

// capture a frame
// retrieve the captured frame
// add the frame to the file

Releasing the video writer:


cvReleaseVideoWriter(&writer);

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

52 / 58

Using OpenCV functions

Image processing
Gradients
void cvSobel( const CvArr* src, CvArr* dst, int xorder, int yorder,
int aperture_size=3 );

(middle)src/x, (right) src/y (x:horizontal axis, y:vertical axis)


Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

53 / 58

Using OpenCV functions

Using OpenCV Functions


Laplace

void cvLaplace( const CvArr* src, CvArr* dst, int aperture_size=3 );

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

54 / 58

Using OpenCV functions

Using OpenCV functions


Edge detection
void cvCanny( const CvArr* image, CvArr* edges, double lowThreshold,
double heighThreshold, int aperture_size=3 );

Using different values for lowThreshold and highThreshold.


Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

55 / 58

Using OpenCV functions

Using OpenCV functions


Hough Transforms
CvSeq* cvHoughLines2( CvArr* image, void* line_storage, int method,
double rho, double theta, int threshold, double param1 = 0,
double param2 = 0 );

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

56 / 58

Using OpenCV functions

Using OpenCV functions


Face detection

http://opencv.willowgarage.com/wiki/FaceDetection
Source file: FaceDetecion.cpp
How to run:
./FaceDetection --cascade="/usr/share/opencv/
haarcascades/haarcascade_frontalface_alt.xml"
Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

57 / 58

Using OpenCV functions

References

http://www.cs.iit.edu/ agam/cs512/lect-notes/opencv-intro/opencv-intro.html
G. Bradski and A. Kaehler, Learning OpenCV: computer vision with the openCV library.
OREILLY.
http://opencv.willowgarage.com/wiki/

Mohamed EL ANSARI (EMARO-ECN)

Introduction to programming with OpenCV 2.0

58 / 58

Das könnte Ihnen auch gefallen