You are on page 1of 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

Was founded to advance the field of computer vision. It collects and makes available the most useful algorithms. Uses range from interactive art.Introduction Description of OpenCV What is OpenCV? Was founded to advance the field of computer vision. to mine inspection. It gives everyone a reliable.0 4 / 58 . Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. stitching maps on the web on through advanced robotics. real time infrastructure to build on.

0 5 / 58 .Introduction Description of OpenCV What is OpenCV? Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.

yahoo.com/wiki/ User group (help): http://tech.willowgarage.net/projects/opencvlibrary/ Other links: http://www.laganiere.Introduction Description of OpenCV Useful OpenCV links OpenCV wiki: http://opencv.com/group/OpenCV/ OpenCV Code Repository: http://sourceforge.html http://www.groups.0 6 / 58 .iit.edu/ agam/cs512/lect-notes/opencv-intro/opencvintro.name/opencv1Tut/ Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.cs.

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

various moments. keyboard and mouse handling. scroll-bars). Hough transform. ellipse fitting. fundamental matrix estimation. Basic GUI (display image/video. text drawing) Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. motion segmentation. Object recognition (eigen-methods). contour processing. Camera calibration (finding and tracking calibration patterns. Delaunay triangulation). template matching.Introduction Description of OpenCV Features Structural analysis (connected components. calibration. distance transform. stereo correspondence). conic. polygonal approximation. Image labeling (line. line fitting. polygon. Motion analysis (optical flow.0 10 / 58 . tracking).

0 11 / 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.

Introduction Description of OpenCV OpenCV structure and content cv .Data structures and linear algebra support.Main OpenCV functions.Auxiliary (experimental) OpenCV functions. cxcore . highgui . I/O routines and functions for storing and loading video and images Machine learning (ml) library Includes many statistical classifiers and clustering tools. cvaux .0 12 / 58 .GUI functions. Less used as some of its features migrated to CV. Basic image processing and higher-level computer vision algorithms. Contains defunct areas and experimental algorithms. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.

. .g.Introduction OpenCV naming conventions OpenCV naming conventions .) Action = the core functionality (e.. . contour. .: CV_8UC1 means an 8-bit unsigned single-channel matrix..g. . Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. . . 1/2 Functions naming conventions: cvActionTargetMod(. create) Target = the target image area (e. . argument type) Matrix data types: CV_<bit_depth>(S|U|F)C<number_of_channels> S = Signed integer U = Unsigned integer F = Float E. . set. CV_32FC2 means a 32-bit float matrix with two channels. . .g. polygon) Mod = optional modifiers (e.0 13 / 58 .g.

.h> <cxcore.h> <cvaux. . Header files: #include #include #include #include <cv. . .: IPL_DEPTH_8U means an 8-bit unsigned image.g. .included in cv.0 14 / 58 .h Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.h> // unnecessary .h> <highgui. . . . . IPL_DEPTH_32F means a 32-bit float image. .Introduction OpenCV naming conventions OpenCV naming conventions . . 2/2 Image data types: IPL_DEPTH_<bit_depth>(S|U|F) E.

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.0 15 / 58 .Introduction Compilation instructions Compilation instructions Linux: g++ hello-world. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.

//Destroy all windows cvDestroyWindow("Example1"). char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ). img). CV_WINDOW_AUTOSIZE ). //Show the image cvShowImage("Example1".h" #include "highgui.0 16 / 58 . //Wait for a key cvWaitKey(0).h" int main(int argc.Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv. //Create a window cvNamedWindow("Example1". //Release the image cvReleaseImage( &img ). } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.

//Show the image cvShowImage("Example1". CV_WINDOW_AUTOSIZE ). //Destroy all windows cvDestroyWindow("Example1").Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv. img). char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ). //Create a window cvNamedWindow("Example1". //Wait for a key cvWaitKey(0).h" int main(int argc. } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.h" #include "highgui.0 16 / 58 . //Release the image cvReleaseImage( &img ).

//Show the image cvShowImage("Example1".Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv. } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.0 16 / 58 . //Release the image cvReleaseImage( &img ). //Wait for a key cvWaitKey(0). //Create a window cvNamedWindow("Example1".h" #include "highgui. //Destroy all windows cvDestroyWindow("Example1"). CV_WINDOW_AUTOSIZE ). img).h" int main(int argc. char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ).

CV_WINDOW_AUTOSIZE ).0 16 / 58 . } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. //Wait for a key cvWaitKey(0). char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ).h" #include "highgui. //Release the image cvReleaseImage( &img ).Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv. //Create a window cvNamedWindow("Example1". //Show the image cvShowImage("Example1". img). //Destroy all windows cvDestroyWindow("Example1").h" int main(int argc.

h" int main(int argc. //Show the image cvShowImage("Example1".Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv.h" #include "highgui. //Wait for a key cvWaitKey(0). char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ). } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. CV_WINDOW_AUTOSIZE ). //Destroy all windows cvDestroyWindow("Example1"). //Release the image cvReleaseImage( &img ).0 16 / 58 . img). //Create a window cvNamedWindow("Example1".

h" #include "highgui. char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ). img). //Wait for a key cvWaitKey(0). //Release the image cvReleaseImage( &img ).h" int main(int argc. //Create a window cvNamedWindow("Example1". } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv.0 16 / 58 . //Destroy all windows cvDestroyWindow("Example1"). CV_WINDOW_AUTOSIZE ). //Show the image cvShowImage("Example1".

h" #include "highgui. //Show the image cvShowImage("Example1". img). //Create a window cvNamedWindow("Example1".h" int main(int argc. } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv. //Destroy all windows cvDestroyWindow("Example1"). char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ). //Release the image cvReleaseImage( &img ). //Wait for a key cvWaitKey(0). CV_WINDOW_AUTOSIZE ).0 16 / 58 .

//Create a window cvNamedWindow("Example1". //Show the image cvShowImage("Example1".Introduction Program examples Program examples First program: load and display an image //Including needed header files #include "cv. CV_WINDOW_AUTOSIZE ). char** argv ) { //Load an image IplImage* img = cvLoadImage( argv[1] ). //Release the image cvReleaseImage( &img ). } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.h" #include "highgui. img). //Destroy all windows cvDestroyWindow("Example1"). //Wait for a key cvWaitKey(0).h" int main(int argc.0 16 / 58 .

Introduction Program examples Program examples Compiling: g++ Program-1.cpp -o Program-1\ -I/usr/include/opencv -lhighgui Output: Running: .tif Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2./Program-1 LombardStreet-SF.0 17 / 58 .

tif Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.0 17 / 58 .cpp -o Program-1\ -I/usr/include/opencv -lhighgui Output: Running: .Introduction Program examples Program examples Compiling: g++ Program-1./Program-1 LombardStreet-SF.

/Program-1 LombardStreet-SF.tif Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.cpp -o Program-1\ -I/usr/include/opencv -lhighgui Running: .Introduction Program examples Program examples Output: Compiling: g++ Program-1.0 17 / 58 .

GUI commands Window management Create a window: int cvNamedWindow( const char* name. // New width int height // New height ). // Name of the window to be resized int width. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. Sets window size: void cvResizeWindow( const char* name.0 18 / 58 . // Window name int flags = CV_WINDOW_AUTOSIZE // flag ).

// Name of the window to be moved int x. Sets window position: void cvMoveWindow( const char* name.GUI commands Window management Destroys a window: void cvDestroyWindow( const char* name // Name of the window to be destroyed ).0 19 / 58 . Destroys all the HighGUI windows: void cvDestroyAllWindows(void). // New y coordinate Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. // New x coordinate int y ).

Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. // Image to be shown Waits for a pressed key: int cvWaitKey( int delay=0 // Delay in milliseconds ). // Name of the window const CvArr* image ).GUI commands Window management Shows the image in the specified window: void cvShowImage( const char* name. Waits for key event infinitely (delay<=0) or for ”delay” milliseconds.0 20 / 58 .

GUI commands Loading and saving images Loading and saving images Loads an image from file: IplImage* cvLoadImage( const char* filename.PGM.JPG. PNG. DIB. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. 1 channel (CV_LOAD_IMAGE_GRAYSCALE). TIF. Formats supported: Windows bitmaps . PBM.BMP.0 21 / 58 . The colorness specifies whether the loaded image is to be converted to 3 channels (CV_LOAD_IMAGE_COLOR). //Name of file to be loaded int flags=CV_LOAD_IMAGE_COLOR // Specifies colorness and depth of the loaded image ). or left as it was in the input file (CV_LOAD_IMAGE_ANYCOLOR).PPM.

// Name of the file const CvArr* image // Image to be saved ). The image format is chosen depending on the filename extension.GUI commands Loading and saving images Loading and saving images Saves an image to the file: int cvSaveImage( const char* filename. see cvLoadImage. Only 8-bit single-channel or 3-channel (with ’BGR’ channel order) images can be saved using this function.0 22 / 58 . Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.

h" int main(int argc. 1 ). // Save the image dans other file cvSaveImage("ImageFile.jpg". cvReleaseImage( &img ). // Clean up cvDestroyWindow( argv[1] ). cvNamedWindow( argv[1]. img ). // Wait until the user hits a key. // Show the image in the named window cvShowImage( argv[1].img). // Load the image from the given file name.GUI commands Loading and saving images Loading and saving image #include"highgui. } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. IplImage* img = cvLoadImage( argv[1] ). char** argv) { // Create a named window with the name of the file.0 23 / 58 . cvWaitKey(0 ).

The function allocates and initialized the CvCapture structure for reading a video stream from the camera.GUI commands Video I/O functions Video I/O functions Video capturing structure: typedef struct CvCapture CvCapture. Initializes capturing video from file: CvCapture* cvCreateFileCapture( const char* filename ). Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.0 24 / 58 . 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. Allocates and initialized the CvCapture structure for reading the video stream from the specified file. This structure contains the information needed for reading frames from a camera or video file. Initializes capturing video from camera: CvCapture* cvCreateCameraCapture( int index ).

GUI commands Video I/O functions Video I/O functions Releases the CvCapture structure: void cvReleaseCapture( CvCapture** capture ). Gets the image grabbed with cvGrabFrame: IplImage* cvRetrieveFrame( CvCapture* capture). Grabs the frame from camera or file. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. It releases the CvCapture structure allocated by cvCreateFileCapture or cvCreateCameraCapture. The grabbed frame is stored internally. Returns the pointer to the image grabbed with cvGrabFrame function. capture: pointer to video capturing structure.0 25 / 58 . Grabs file from camera or file: int cvGrabFrame( CvCapture* capture ). capture: video capturing structure.

GUI commands Video I/O functions Video I/O functions Grabs and returns a frame from camera or file: IplImage* cvQueryFrame( CvCapture* capture ).0 26 / 58 . This function is just a combination of cvGrabFrame and cvRetrieveFrame in one call. decompresses and returns it. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. grabs a frame from camera or video file.

im). cvWaitKey(0). capture = cvCreateCameraCapture(-1). cvNamedWindow("Win"). cvGrabFrame(capture). *imm. im =cvRetrieveFrame(capture). cvShowImage("Win". CvCapture * capture. return 0.0 27 / 58 .GUI commands Video I/O functions Video I/O functions Example #include<highgui.h> main() { IplImage *im. cvReleaseCapture(&capture). cvReleaseImage(&im). } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.

3. // |-.int height. // | // |-.bottom-left origin (Windows bitmaps style) size of aligned image row in bytes image data size in bytes = height*widthStep image ROI. Introduction to programming with OpenCV 2. 1 . IPL_DEPTH_8S. // |-.0 28 / 58 .int imageSize.int widthStep. 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 .int origin.// | // |-| Mohamed EL ANSARI (EMARO-ECN) Number of color channels (1. IPL_DEPTH_32S. // |-. // | // |-. 1 .IPL_DEPTH_16S.separate color channels cvCreateImage can only create interleaved images 0 .top-left origin.char* imageData.interleaved color channels.Basic openCV structures Image data structure Image data structure IPL image: IplImage |-.struct _IplROI *roi.2. IPL_DEPTH_16U.4) Pixel depth in bits: IPL_DEPTH_8U.int depth.int nChannels.int dataOrder. when not NULL specifies image region to be processed. // |-. // | // | // |-. // | // | // | // | // |-.int width. // |-.IPL_DEPTH_32F.

// data pointer for an unsigned char matrix |-.short. // dimensions |-.int* i. // data pointer for an integer matrix |-. // full row length in bytes |-. |-.int rows.int height. // elements type (uchar.double* db.0 29 / 58 .int type.int step.int. width. // alternative dimensions reference |-.short* s. // data pointer for a short matrix |-.double) and flags |-.union data.uchar* ptr. cols. // data pointer for a float matrix |-.Basic openCV structures Matrices Matrices CvMat // 2D array |-.float. // data pointer for a double matrix Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.float* fl.

// data pointer for a double matrix | |-. // information for each dimension |-.size. | |-.int type.double) and flags |-.union data. // distance between elements in a given dimension Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. // number of elements in a given dimension |-.float.short.int* i.0 30 / 58 .short* s. // data pointer for a short matrix | |-.float* fl.Basic openCV structures Matrices Matrices CvMatND // N-dimensional array |-. // data pointer for an integer matrix | |-. // data pointer for a float matrix | |-.struct dim[].uchar* ptr.step.int.double* db.int dims. // number of array dimensions |-. // elements type (uchar. // data pointer for an unsigned char matrix | |-.

in effect.Basic openCV structures Matrices Matrices Even though OpenCV is implemented in C. which is derived from CvArr Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.0 31 / 58 . IplImage is derived from CvMat. the structures used in OpenCV have an object-oriented design.

int y.: p. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. float y. float height).Basic openCV structures Other data structures Other data structures Points: CvPoint p = cvPoint(int x. Rectangular dimensions: //Pixel accurate size CvSize r = cvSize(int width. CvPoint2D32f p = cvPoint2D32f(float x.g. Rectangular dimensions with offset: CvRect r = cvRect(int x.0. //Sub-pixel accurate size CvSize2D32f r = cvSize2D32f(float width.y=5. float y). E.0. float z).0 32 / 58 . int width. int y). int height). p. int height). CvPoint3D32f p = cvPoint3D32f(float x.x=5.

3. double val3=0 ). Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.Basic openCV structures Other data structures Other data structures A container for 1-.0 33 / 58 . double val1=0.or 4-tuples of numbers: typedef struct CvScalar { double val[4]. double val2=0. } CvScalar.2-. /* the constructor function */ inline CvScalar cvScalar( double val0.

0 34 / 58 . cvSize(). cvRect(). and cvScalar()are extremely useful because they make your code not only easier to write but also easier to read.Basic openCV structures Other data structures Other data structures The inline constructors for the data types listed in the table below cvPointXXX(). Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.

h" using namespace std.img). -1 ). // Create a rectangle cvRectangle(img.255)).jpg". } Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. int main(int argc. char** argv ) { IplImage* img = cvLoadImage( argv[1].0 35 / 58 . // Save the modified image cvSaveImage("ImRec.255. cvScalar(255. cvReleaseImage( &img ).cvPoint(200.Basic openCV structures Other data structures Other data structures Using 2D structures to draw a rectangle #include "highgui.cvPoint(5.130).100).

IPL DEPTH 64F channels: Number of channels per pixel. Makes a full copy of an image (Clone): IplImage* cvCloneImage( const IplImage* image). Examples: // Allocate a 1-channel byte image IplImage* img1=cvCreateImage(cvSize(640. img2=cvCloneImage(img1). Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.1).1). Release image data: void cvReleaseImage( IplImage** image ). IplImage* img2.IPL_DEPTH_8U. int channels). IPL DEPTH 32S.height).480). int depth. IPL DEPTH 16S.0 36 / 58 . size: cvSize(width. IPL DEPTH 16U. depth: pixel depth in bits: IPL DEPTH 8U. IPL DEPTH 32F. Example IplImage* img1=cvCreateImage(cvSize(640. IPL DEPTH 8S.Working with images Allocating and releasing images Allocating and releasing images Allocate an image: IplImage* cvCreateImage(CvSize size.IPL_DEPTH_8U.480).

For a single-channel byte image: IplImage* img=cvCreateImage(cvSize(640.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.IPL_DEPTH_32F. ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111. For a multi-channel byte image: IplImage* img=cvCreateImage(cvSize(640.3). ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111. // B ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112. // R For a multi-channel float image: IplImage* img=cvCreateImage(cvSize(640.1).0 37 / 58 . ((uchar *)(img->imageData + i*img->widthStep))[j]=111.480).480).IPL_DEPTH_8U. // G ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113. // B ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112.3). // R Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. // G ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113.IPL_DEPTH_8U.480).

r. } RgbPixel. unsigned char b. void operator=(IplImage* img) imgp=img. ~Image()imgp=0. typedef Image<unsigned char> BwImage. float b. } RgbPixelFloat.r. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.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. inline T* operator[](const int rowIndx) return ((T *)(imgp->imageData + rowIndx*imgp->widthStep)).g.0 38 / 58 . typedef struct{ typedef Image<float> BwImageFloat. }. public: Image(IplImage* img=0) imgp=img.g. typedef Image<RgbPixelFloat> RgbImageFloat. typedef struct{ typedef Image<RgbPixel> RgbImage.

imgA[i][j]. RgbImage imgA(img). BwImage imgA(img).480).IPL_DEPTH_32F.Working with images Accessing image elements Accessing image elements Simplified method For a single-channel byte image: IplImage* img=cvCreateImage(cvSize(640.r = 111. imgA[i][j].IPL_DEPTH_8U.g = 111.IPL_DEPTH_8U.1). imgA[i][j].b = 111. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.b = 111. imgA[i][j].480). imgA[i][j]. imgA[i][j] = 111. imgA[i][j].480).3). RgbImage imgA(img). For a multi-channel byte image: IplImage* img=cvCreateImage(cvSize(640.0 39 / 58 . For a multi-channel float image: IplImage* img=cvCreateImage(cvSize(640.g = 111.r = 111.3).

0 40 / 58 .Working with images Image conversion Image conversion Conversion between image formats: void cvConvertImage(const CvArr* src.CvArr* dst.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. int flags = 0 ). CV_CVTIMG_SWAP_RB. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.CV_CVTIMG_FLIP.

int code ).CvArr* dst.g. (right) YCrCb Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. HLS e. YCrCb. BGR. CV_BGR2Lab = CV_<X>2<Y> (left) RGB. HSV. Luv.0 41 / 58 . (middle) HSV.: CV_BGR2GRAY. XYZ. CV_BGR2HSV. GRAY. code <X>/<Y> = RGB.Working with images Image conversion Image conversion Conversion between color spaces : void cvCvtColor(const CvArr* src. Lab.

200).0).200) cvRectangle(img. Draw a line segment: // draw a green line of width 1 between (100.255. 1). 1).\ cvScalar(0.100).100).0). cvPoint(100.200) cvLine(img. Draw a circle: // draw a circle at (100.200).0 42 / 58 . 20.100) and (200.100) and (200. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. cvPoint(100.0. cvScalar(255. cvPoint(100.Working with images Drawing commands Drawing commands Draw a box: // draw a box with red lines of width 1 between (100.\ cvScalar(0.\ cvPoint(200.255.100). 1).0). cvPoint(200.100) with a radius of 20. Use green lines of width 1 cvCircle(img.

Specified in form CV_<bit_depth>(S|U|F)C<number_of_channels>.: CV_8UC1 means an 8-bit unsigned single-channel matrix. type: Type of the matrix elements.CV_32FC1). Release a matrix: CvMat* M = cvCreateMat(4. Example: CvMat* M = cvCreateMat(4. CvMat* M2.4.Working with matrices Allocating and releasing matrices Allocating and releasing matrices Allocate a matrix: CvMat* cvCreateMat(int rows. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.CV_32FC1).0 43 / 58 . int cols. Clone a matrix: CvMat* M1 = cvCreateMat(4. CV_32SC2 means a 32-bit signed matrix with two channels.CV_32FC1). int type).4. cvReleaseMat(&M).4.g. M2=cvCloneMat(M1). E.

CvMat cvMat(int rows.Working with matrices Allocating and releasing matrices Allocating and releasing matrices // Create a new rows by cols matrix of type type. int type).0 44 / 58 . int cols. int step = CV_AUTOSTEP). void* data = NULL. void* data= NULL ). int cols. int cols. // Like cvInitMatHeader() but allocates CvMat as well. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. int cols. // Initialize header on existing CvMat structure CvMat* cvInitMatHeader(CvMat* mat. int type. int type ). int rows. CvMat* cvCreateMat( int rows. // Create only matrix header without allocating data CvMat* cvCreateMatHeader( int rows. int type.

7. a). 11. 10. CvMat Ma=cvMat(3. 3. 4. 9. 4. 12 }. 3. CV_64FC1. 2.0 45 / 58 . 6.Working with matrices Allocating and releasing matrices Allocating and releasing matrices Initialize a matrix: double a[] = { 1. 4. 8. Alternatively: CvMat Ma. CV_64FC1. 5. cvInitMatHeader(&Ma. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. a).

4. float *data = M->data. Indirect matrix element access: double cvmGet( const CvMat* mat. int row. j) cell of a 2D float matrix. (data+i*step)[j] = 3.0. int step = M->step/sizeof(float). Direct matrix element access assuming possible alignment gaps: CvMat* M = cvCreateMat(4. int col ).0 46 / 58 .double value).Working with matrices Accessing matrix elements Accessing matrix elements Assume that you need to access the (i. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.fl. void cvmSet( CvMat* mat. int row.CV_32FC1). int col.

// Ma. // Ma. Mb.Working with matrices Matrix/vector operations Matrix/vector operations Matrix-matrix operations: CvMat *Ma. cvMatMul(Ma. Mb. cvSub(Ma. Mc). Mc).*Mb -> Mc cvDiv(Ma. Mc). cvScalar(-10. cvAdd(Ma. Mc). Mb.-10 -> Mc Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. *Mc. Mb. *Mb. *Mc. Mc). *Mb.0). Mc). // Ma+Mb // Ma-Mb // Ma*Mb -> Mc -> Mc -> Mc Elementwise matrix operations: CvMat *Ma./Mb -> Mc cvAddS(Ma. cvMul(Ma.0 47 / 58 . // Ma. Mb.

CV_64FC1. // cross product: Va x Vb -> Vc cvCrossProduct(&Va. 1. CV_64FC1. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. CvMat Vb=cvMat(3.0 48 / 58 . // dot product: Va . double vc[3]. &Vc). CV_64FC1. 1.&Vb). CvMat Va=cvMat(3. CvMat Vc=cvMat(3. 0. 3}. &Vb. va). 1}. vc). 2. double vb[] = {0. Vb -> res double res=cvDotProduct(&Va. vb).Working with matrices Matrix/vector operations Matrix/vector operations Vector products: double va[] = {1. 1.

V.3.CV_32FC1).CV_32FC1). &b.Working with matrices Matrix/vector operations Matrix/vector operations Inhomogeneous linear system solver: CvMat* A = CvMat* x = CvMat* b = cvSolve(&A. // l = eigenvalues of A (descending order) // E = corresponding eigenvectors (rows) Singular value decomposition: CvMat* A = CvMat* U = CvMat* D = CvMat* V = cvSVD(A. cvCreateMat(3.3. cvCreateMat(3.CV_32FC1).CV_32FC1). cvCreateMat(3. cvEigenVV(&A.CV_32FC1). cvCreateMat(3.3.CV_32FC1).CV_32FC1).3.CV_32FC1). // solve (Ax=b) for x Eigen analysis (of a symmetric matrix): CvMat* A = cvCreateMat(3. // A = U D V^T Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.CV_32FC1). cvCreateMat(3. cvCreateMat(3. &x). &E.3.3.1. cvCreateMat(3. CV_SVD_U_T|CV_SVD_V_T).1.0 49 / 58 . CvMat* l = cvCreateMat(3. U. CvMat* E = cvCreateMat(3.1. &l).3.CV_32FC1). D.

exit(0).0 50 / 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. if(!cvGrabFrame(capture)){ // capture a frame printf("Could not grab a frame\n\7"). Releasing the capture source: cvReleaseCapture(&capture). Capturing a frame: IplImage* img = 0.avi"). } // retrieve the captured frame img=cvRetrieveFrame(capture). Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. Initializing capture from a camera: CvCapture* capture = cvCaptureFromCAM(0). (AVI). // capture from video device #0 Initializing capture from a file: CvCapture* capture = cvCaptureFromAVI("infile.

int frameW = (int) cvGetCaptureProperty(capture. int posFrames = (int) cvGetCaptureProperty(capture. get correct CV_CAP_PROP_FRAME_HEIGHT). int numFrames = (int) cvGetCaptureProperty(capture. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. CV_CAP_PROP_FPS).0 51 / 58 . Get frame information: float posMsec = cvGetCaptureProperty(capture. CV_CAP_PROP_POS_MSEC). CV_CAP_PROP_POS_FRAMES). CV_CAP_PROP_FRAME_WIDTH). CV_CAP_PROP_FRAME_COUNT). CV_CAP_PROP_POS_AVI_RATIO). // this call is necessary to // capture properties int frameH = (int) cvGetCaptureProperty(capture.Working with video files Getting/setting frame information Getting/setting frame information Get capture device properties: cvQueryFrame(capture). float posRatio = cvGetCaptureProperty(capture. int fps = (int) cvGetCaptureProperty(capture.

img=cvRetrieveFrame(capture).Working with video files Saving a video file Saving a video file Writing the video file: IplImage* img = 0.0 52 / 58 . cvWriteFrame(writer. } // capture a frame // retrieve the captured frame // add the frame to the file Releasing the video writer: cvReleaseVideoWriter(&writer).img). for(i=0. int nFrames = 50. Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.i++){ cvGrabFrame(capture).i<nFrames.

int xorder.Using OpenCV functions Image processing Gradients void cvSobel( const CvArr* src. (right) ∂src/∂y (x:horizontal axis. y:vertical axis) Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. int yorder. int aperture_size=3 ).0 53 / 58 . (middle)∂src/∂x. CvArr* dst.

Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2.Using OpenCV functions Using OpenCV Functions Laplace void cvLaplace( const CvArr* src. CvArr* dst.0 54 / 58 . int aperture_size=3 ).

Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. double heighThreshold.Using OpenCV functions Using OpenCV functions Edge detection void cvCanny( const CvArr* image. double lowThreshold. int aperture_size=3 ). Using different values for lowThreshold and highThreshold. CvArr* edges.0 55 / 58 .

Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. double param1 = 0. void* line_storage. int threshold. double param2 = 0 ).0 56 / 58 . int method. double rho.Using OpenCV functions Using OpenCV functions Hough Transforms CvSeq* cvHoughLines2( CvArr* image. double theta.

com/wiki/FaceDetection Source file: FaceDetecion.cpp How to run: .0 57 / 58 .willowgarage.xml" Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2./FaceDetection --cascade="/usr/share/opencv/ haarcascades/haarcascade_frontalface_alt.Using OpenCV functions Using OpenCV functions Face detection http://opencv.

Learning OpenCV: computer vision with the openCV library.willowgarage.iit. O’REILLY.edu/ agam/cs512/lect-notes/opencv-intro/opencv-intro.com/wiki/ Mohamed EL ANSARI (EMARO-ECN) Introduction to programming with OpenCV 2. Bradski and A. Kaehler.Using OpenCV functions References http://www.0 58 / 58 . http://opencv.html G.cs.