Académique Documents
Professionnel Documents
Culture Documents
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
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
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.
3 / 58
Introduction
Description of OpenCV
What is OpenCV?
4 / 58
Introduction
Description of OpenCV
What is OpenCV?
5 / 58
Introduction
Description of OpenCV
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/
6 / 58
Introduction
Description of OpenCV
OpenCV Books
Learning OpenCV: Computer
Vision with the OpenCV Library.
OpenCV 2 Computer Vision
Application Programming
Cookbook.
7 / 58
Introduction
Description of OpenCV
OpenCV timeline
8 / 58
Introduction
Description of OpenCV
Features
9 / 58
Introduction
Description of OpenCV
Features
10 / 58
Introduction
Description of OpenCV
11 / 58
Introduction
Description of OpenCV
Machine learning (ml) library Includes many statistical classifiers and clustering tools.
12 / 58
Introduction
13 / 58
Introduction
<cv.h>
<cvaux.h>
<highgui.h>
<cxcore.h> // unnecessary - included in cv.h
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.
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");
}
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");
}
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");
}
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");
}
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");
}
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");
}
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");
}
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");
}
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
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
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
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
);
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
19 / 58
GUI commands
Window management
20 / 58
GUI commands
21 / 58
GUI commands
22 / 58
GUI commands
#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 );
}
23 / 58
GUI commands
24 / 58
GUI commands
25 / 58
GUI commands
26 / 58
GUI commands
#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)
27 / 58
28 / 58
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
29 / 58
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
30 / 58
Matrices
Matrices
31 / 58
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);
32 / 58
33 / 58
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.
34 / 58
35 / 58
36 / 58
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
37 / 58
38 / 58
39 / 58
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)
40 / 58
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>
41 / 58
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);
42 / 58
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);
43 / 58
44 / 58
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);
45 / 58
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;
46 / 58
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
47 / 58
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);
48 / 58
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
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
49 / 58
50 / 58
get correct
CV_CAP_PROP_FRAME_HEIGHT);
CV_CAP_PROP_FRAME_WIDTH);
CV_CAP_PROP_FPS);
CV_CAP_PROP_FRAME_COUNT);
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);
51 / 58
// capture a frame
// retrieve the captured frame
// add the frame to the file
52 / 58
Image processing
Gradients
void cvSobel( const CvArr* src, CvArr* dst, int xorder, int yorder,
int aperture_size=3 );
53 / 58
54 / 58
55 / 58
56 / 58
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)
57 / 58
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/
58 / 58