Transcript
Page 1: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Getting to Know OpenCV

主講人:虞台文

Page 2: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Content

Basic StructuresArrays, Matrices, and ImagesMatrix and Image OperatorsDrawing ThingsDrawing TextData Persistence

Page 3: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Getting to Know OpenCV

Basic Structures

Page 4: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Basic Structures (Point)

typedef struct CvPoint{ int x; int y;} CvPoint;

typedef struct CvPoint{ int x; int y;} CvPoint;

CV_INLINE CvPoint cvPoint(int x, int y){ CvPoint p;

p.x = x; p.y = y;

return p;}

CV_INLINE CvPoint cvPoint(int x, int y){ CvPoint p;

p.x = x; p.y = y;

return p;}

CvPoint pt1;

pt1.x = 20;pt1.y = 50

CvPoint pt1;

pt1.x = 20;pt1.y = 50

CvPoint pt2 = cvPoint(20, 50);CvPoint pt2 = cvPoint(20, 50);

cxtypes.h

Page 5: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Basic Structures (Point)

typedef struct CvPoint{ int x; int y;} CvPoint;

typedef struct CvPoint{ int x; int y;} CvPoint;

cxtypes.h

typedef struct CvPoint2D32f{ float x; float y;} CvPoint2D32f;

typedef struct CvPoint2D32f{ float x; float y;} CvPoint2D32f;

typedef struct CvPoint2D64f{ double x; double y;} CvPoint2D64f;

typedef struct CvPoint2D64f{ double x; double y;} CvPoint2D64f;

typedef struct CvPoint3D32f{ float x; float y; float z;} CvPoint3D32f;

typedef struct CvPoint3D32f{ float x; float y; float z;} CvPoint3D32f;

typedef struct CvPoint3D64f{ double x; double y; double z;} CvPoint3D64f;

typedef struct CvPoint3D64f{ double x; double y; double z;} CvPoint3D64f;

Page 6: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Basic Structures (Size)

typedef struct CvSize{ int width; int height;} CvSize;

typedef struct CvSize{ int width; int height;} CvSize;

cxtypes.h

typedef struct CvSize2D32f{ float width; float height;} CvSize2D32f

typedef struct CvSize2D32f{ float width; float height;} CvSize2D32f

typedef struct CvRect{ int x; int y; int width; int height;} CvRect;

typedef struct CvRect{ int x; int y; int width; int height;} CvRect;

Page 7: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Basic Structures (Scalar)

typedef struct CvScalar{ double val[4];} CvScalar;

typedef struct CvScalar{ double val[4];} CvScalar;

cxtypes.h

/* Constructor:initializes val[0] with val0, val[1] with val1, etc.*/inline CvScalar cvScalar( double val0, double val1=0, double val2=0, double val3=0 );/* Constructor:initializes all of val[0]...val[3] with val0123*/inline CvScalar cvScalarAll( double val0123 );

/* Constructor:initializes val[0] with val0, and all of val[1]...val[3] with zeros*/inline CvScalar cvRealScalar( double val0 );

/* Constructor:initializes val[0] with val0, val[1] with val1, etc.*/inline CvScalar cvScalar( double val0, double val1=0, double val2=0, double val3=0 );/* Constructor:initializes all of val[0]...val[3] with val0123*/inline CvScalar cvScalarAll( double val0123 );

/* Constructor:initializes val[0] with val0, and all of val[1]...val[3] with zeros*/inline CvScalar cvRealScalar( double val0 );

Page 8: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Getting to Know OpenCV

Arrays, Matrices, and Images

Page 9: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImage

cxtypes.h

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

Page 10: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Page 11: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

CV_<bit_depth>(S|U|F)C<number_of_channels>CV_<bit_depth>(S|U|F)C<number_of_channels>

Examples

CV_8UC3

CV_32FC1 32-bit floats

unsigned integer 8-bit triplets

Page 12: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

the length of a row in bytes

a pointer to a data array

Page 13: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

the height (rows) and width (cols) of the matrix

Page 14: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Matrix Header

Page 15: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Some Matrix Operators:// Create a new rows by cols matrix of type ‘type’CvMat* cvCreateMat(int rows, int cols, int type)

// Creates only matrix header without allocating dataCvMat* cvCreateMatHeader(int rows, int cols, int type) // allocates image, matrix or multi-dimensional array datavoid cvCreateData(CvArr* arr)

// Initialize header on existing CvMat structureCvMat* cvInitMatHeader(CvMat* mat,

int rows, int cols, int type, void* data=NULL, int step=CV_AUTOSTEP)

// Like cvInitMatHeader() but allocates CvMat as wellCvMat cvMat(int rows, int cols, int type, void* data=NULL);// Allocate a new matrix just like the matrix ‘mat’CvMat* cvCloneMat(const cvMat* mat);

// Free the matrix ‘mat’, both header and datavoid cvReleaseMat(CvMat** mat);

Some Matrix Operators:// Create a new rows by cols matrix of type ‘type’CvMat* cvCreateMat(int rows, int cols, int type)

// Creates only matrix header without allocating dataCvMat* cvCreateMatHeader(int rows, int cols, int type) // allocates image, matrix or multi-dimensional array datavoid cvCreateData(CvArr* arr)

// Initialize header on existing CvMat structureCvMat* cvInitMatHeader(CvMat* mat,

int rows, int cols, int type, void* data=NULL, int step=CV_AUTOSTEP)

// Like cvInitMatHeader() but allocates CvMat as wellCvMat cvMat(int rows, int cols, int type, void* data=NULL);// Allocate a new matrix just like the matrix ‘mat’CvMat* cvCloneMat(const cvMat* mat);

// Free the matrix ‘mat’, both header and datavoid cvReleaseMat(CvMat** mat);

Page 16: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Accessing matrix elements

Easy way:

CV_MAT_ELEM()

CV_MAT_ELEM_PTR()

CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );int i, j;float sum = 0.0f;

for(i=0; i<5; i++) for(j=0; j<5; j++) CV_MAT_ELEM(*mat, float, i, j) = (float) i + j;

for(i=0; i<5; i++) for(j=0; j<5; j++) sum += CV_MAT_ELEM(*mat, float, i, j);

CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );int i, j;float sum = 0.0f;

for(i=0; i<5; i++) for(j=0; j<5; j++) CV_MAT_ELEM(*mat, float, i, j) = (float) i + j;

for(i=0; i<5; i++) for(j=0; j<5; j++) sum += CV_MAT_ELEM(*mat, float, i, j);

Page 17: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Accessing matrix elements

Easy way:

CV_MAT_ELEM()

CV_MAT_ELEM_PTR()

CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );int i, j;float sum = 0.0f;

for(i=0; i<5; i++) for(j=0; j<5; j++) *((float*) CV_MAT_ELEM_PTR(*mat, i, j)) = (float) i + j;

for(i=0; i<5; i++) for(j=0; j<5; j++) sum += *((float*) CV_MAT_ELEM_PTR(*mat, i, j)) ;

CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );int i, j;float sum = 0.0f;

for(i=0; i<5; i++) for(j=0; j<5; j++) *((float*) CV_MAT_ELEM_PTR(*mat, i, j)) = (float) i + j;

for(i=0; i<5; i++) for(j=0; j<5; j++) sum += *((float*) CV_MAT_ELEM_PTR(*mat, i, j)) ;

Page 18: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Accessing matrix elements

Easy way:

CV_MAT_ELEM()

CV_MAT_ELEM_PTR()

Although these macros are easy to use, they may

not be the best way to access a matrix.Although these macros are easy to use, they may

not be the best way to access a matrix.

Page 19: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Accessing matrix elements

Hard way:

cvPtr?D()

cvGetReal?D()

cvGet?D()

cvSetReal?D()

cvSet?D()

cvmGet()

cvmSet()

Page 20: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

Accessing matrix elements

Hard way:

cvPtr?D()

cvGetReal?D()

cvGet?D()

cvSetReal?D()

cvSet?D()

cvmGet()

cvmSet()

Using these pointer functions togain access to a particular point in the matrix and then use pointer arithmetic to move around in the matrix from there.

Using these pointer functions togain access to a particular point in the matrix and then use pointer arithmetic to move around in the matrix from there.

Ineffective

Page 21: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImagetypedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

typedef struct CvMat{ int type; int step; //# bytes per row

int* refcount; // internal use int hdr_refcount; // internal use

union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data;

union{ int rows; int height; };

union{ int cols; int width; };} CvMat;

cxtypes.h

#include <stdio.h>#include <cv.h>#include <highgui.h>

float sum( CvMat* mat ) { float s = 0.0f; for( int row=0; row<mat->height; row++ ) { float* ptr = mat->data.fl + row * mat->step/4; for( int col=0; col<mat->width; col++ ) { s += *ptr++; } } return( s );};

int main(int argc, char** argv){ CvMat *mat = cvCreateMat(5,5,CV_32FC1); float element_3_2 = 7.7; *((float*)CV_MAT_ELEM_PTR( *mat, 3,2) ) = element_3_2; cvmSet(mat,4,4,0.5000); cvSetReal2D(mat,3,3,0.5000); float s = sum(mat); printf("%f\n",s); return 0;}

#include <stdio.h>#include <cv.h>#include <highgui.h>

float sum( CvMat* mat ) { float s = 0.0f; for( int row=0; row<mat->height; row++ ) { float* ptr = mat->data.fl + row * mat->step/4; for( int col=0; col<mat->width; col++ ) { s += *ptr++; } } return( s );};

int main(int argc, char** argv){ CvMat *mat = cvCreateMat(5,5,CV_32FC1); float element_3_2 = 7.7; *((float*)CV_MAT_ELEM_PTR( *mat, 3,2) ) = element_3_2; cvmSet(mat,4,4,0.5000); cvSetReal2D(mat,3,3,0.5000); float s = sum(mat); printf("%f\n",s); return 0;}

Accessing matrix elements

Right way:

Page 22: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImage

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

Page 23: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImage

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

• nSize - sizeof(IplImage)• ID - Version, always equals 0• nChannels - Number of channels. Most OpenCV functions support 1-4 channels.• alphaChannel - Ignored by OpenCV• depth - Pixel depth in bits. The supported depths are:

IPL_DEPTH_8U - Unsigned 8-bit integer

IPL_DEPTH_8S - Signed 8-bit integer

IPL_DEPTH_16U - Unsigned 16-bit integer

IPL_DEPTH_16S - Signed 16-bit integer

IPL_DEPTH_32S - Signed 32-bit integer

IPL_DEPTH_32F - Single-precision floating point

IPL_DEPTH_64F - Double-precision floating point

• nSize - sizeof(IplImage)• ID - Version, always equals 0• nChannels - Number of channels. Most OpenCV functions support 1-4 channels.• alphaChannel - Ignored by OpenCV• depth - Pixel depth in bits. The supported depths are:

IPL_DEPTH_8U - Unsigned 8-bit integer

IPL_DEPTH_8S - Signed 8-bit integer

IPL_DEPTH_16U - Unsigned 16-bit integer

IPL_DEPTH_16S - Signed 16-bit integer

IPL_DEPTH_32S - Signed 32-bit integer

IPL_DEPTH_32F - Single-precision floating point

IPL_DEPTH_64F - Double-precision floating point

Page 24: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImage

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

• colorModel - Ignored by OpenCV.

• channelSeq - Ignored by OpenCV

• dataOrder - 0 = IPL_DATA_ORDER_PIXEL 1 = IPL_DATA_ORDER_PLANE

• origin - 0 = IPL_ORIGIN_TL 1 = IPL_ORIGIN_BL

• align - OpenCV ignores this and uses widthStep instead.

• width - Image width in pixels

• height - Image height in pixels

• colorModel - Ignored by OpenCV.

• channelSeq - Ignored by OpenCV

• dataOrder - 0 = IPL_DATA_ORDER_PIXEL 1 = IPL_DATA_ORDER_PLANE

• origin - 0 = IPL_ORIGIN_TL 1 = IPL_ORIGIN_BL

• align - OpenCV ignores this and uses widthStep instead.

• width - Image width in pixels

• height - Image height in pixels

Page 25: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImage

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

• roi - Region Of Interest (ROI). If not NULL, only this image region will be processed.

• maskROI - Must be NULL in OpenCV

• imageId - Must be NULL in OpenCV

• tileInfo - Must be NULL in OpenCV

• roi - Region Of Interest (ROI). If not NULL, only this image region will be processed.

• maskROI - Must be NULL in OpenCV

• imageId - Must be NULL in OpenCV

• tileInfo - Must be NULL in OpenCV

Page 26: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImage

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

• imageSize - Image data size in bytes. For interleaved data, this equals image->height*image->widthStep

• imageData - A pointer to the aligned image data

• widthStep - The size of an aligned image row, in bytes

• BorderMode - Border completion mode, ignored by OpenCV

• BorderConst - Border completion mode, ignored by OpenCV

• imageDataOrigin - A pointer to the origin of the image data (not necessarily aligned). This is used for

image deallocation.

• imageSize - Image data size in bytes. For interleaved data, this equals image->height*image->widthStep

• imageData - A pointer to the aligned image data

• widthStep - The size of an aligned image row, in bytes

• BorderMode - Border completion mode, ignored by OpenCV

• BorderConst - Border completion mode, ignored by OpenCV

• imageDataOrigin - A pointer to the origin of the image data (not necessarily aligned). This is used for

image deallocation.

Page 27: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Accessing Image Data

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

typedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align;int width;int height;struct _IplROI* roi;struct _IplImage* maskROI;void* imageId;struct _IplTileInfo* tileInfo;int imageSize;char* imageData;int widthStep;int BorderMode[4];int BorderConst[4];char* imageDataOrigin;

} IplImage;

#include <stdio.h>#include <cv.h>#include <highgui.h>

void saturate_sv( IplImage* img ){ for( int y=0; y<img->height; y++ ) { uchar* ptr = (uchar*) ( img->imageData + y * img->widthStep ); for( int x=0; x<img->width; x++ ) { ptr[3*x+1] = 255; ptr[3*x+2] = 255; } }}

int main( int argc, char** argv ){ IplImage* img = cvLoadImage( argv[1] ); cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE ); saturate_sv(img); cvShowImage("Example1", img ); cvWaitKey(0); cvReleaseImage( &img ); cvDestroyWindow("Example1");}

#include <stdio.h>#include <cv.h>#include <highgui.h>

void saturate_sv( IplImage* img ){ for( int y=0; y<img->height; y++ ) { uchar* ptr = (uchar*) ( img->imageData + y * img->widthStep ); for( int x=0; x<img->width; x++ ) { ptr[3*x+1] = 255; ptr[3*x+2] = 255; } }}

int main( int argc, char** argv ){ IplImage* img = cvLoadImage( argv[1] ); cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE ); saturate_sv(img); cvShowImage("Example1", img ); cvWaitKey(0); cvReleaseImage( &img ); cvDestroyWindow("Example1");}

Page 28: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ROI

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

cvSetImageROI

Page 29: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ROI

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

cvSetImageROI

cvAddS

Page 30: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ROI

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

cvSetImageROI

cvAddS

#include <cv.h>#include <highgui.h>// ch3_ex3_12 image_name x y width height add# int main(int argc, char** argv){ IplImage* src; cvNamedWindow("Example3_12_pre", CV_WINDOW_AUTOSIZE); cvNamedWindow("Example3_12_post", CV_WINDOW_AUTOSIZE); if( argc == 7 && ((src=cvLoadImage(argv[1],1)) != 0 )) {

int x = atoi(argv[2]);int y = atoi(argv[3]);int width = atoi(argv[4]);int height = atoi(argv[5]);int add = atoi(argv[6]);cvShowImage( "Example3_12_pre", src);cvSetImageROI(src, cvRect(x,y,width,height));cvAddS(src, cvScalar(add, add, add),src);cvResetImageROI(src);cvShowImage( "Example3_12_post",src);cvWaitKey();

} cvReleaseImage( &src ); cvDestroyWindow("Example3_12_pre"); cvDestroyWindow("Example3_12_post"); return 0;}

#include <cv.h>#include <highgui.h>// ch3_ex3_12 image_name x y width height add# int main(int argc, char** argv){ IplImage* src; cvNamedWindow("Example3_12_pre", CV_WINDOW_AUTOSIZE); cvNamedWindow("Example3_12_post", CV_WINDOW_AUTOSIZE); if( argc == 7 && ((src=cvLoadImage(argv[1],1)) != 0 )) {

int x = atoi(argv[2]);int y = atoi(argv[3]);int width = atoi(argv[4]);int height = atoi(argv[5]);int add = atoi(argv[6]);cvShowImage( "Example3_12_pre", src);cvSetImageROI(src, cvRect(x,y,width,height));cvAddS(src, cvScalar(add, add, add),src);cvResetImageROI(src);cvShowImage( "Example3_12_post",src);cvWaitKey();

} cvReleaseImage( &src ); cvDestroyWindow("Example3_12_pre"); cvDestroyWindow("Example3_12_post"); return 0;}

Page 31: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ROI by widthStep

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

NULL

Page 32: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ROI by widthStep

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

NULLNULL

Page 33: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ROI by widthStep

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

NULL

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

cvAddS

NULL

Page 34: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ROI by widthStep

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

void cvSetImageROI( IplImage* image, CvRect rect );

void cvResetImageROI( IplImage* image );

NULL

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

typedef struct _IplImage {........................int width;int height;........................struct _IplROI* roi;

........................ char* imageData;

int widthStep; ........................} IplImage;

cvAddS

int main(int argc, char** argv){ IplImage* interest_img; CvRect interest_rect; if( argc == 7 && ((interest_img=cvLoadImage(argv[1])) != 0 )) { interest_rect.x = atoi(argv[2]); interest_rect.y = atoi(argv[3]); interest_rect.width = atoi(argv[4]); interest_rect.height = atoi(argv[5]); int add = atoi(argv[6]);

IplImage *sub_img = cvCreateImageHeader( cvSize(interest_rect.width, interest_rect.height), interest_img->depth, interest_img->nChannels ); sub_img->origin = interest_img->origin; sub_img->widthStep = interest_img->widthStep; sub_img->imageData = interest_img->imageData + interest_rect.y * interest_img->widthStep + interest_rect.x * interest_img->nChannels; cvAddS( sub_img, cvScalar(add, add, add), sub_img ); cvReleaseImageHeader(&sub_img);

cvNamedWindow( "Roi_Add", CV_WINDOW_AUTOSIZE ); cvShowImage( "Roi_Add", interest_img ); cvWaitKey(); } return 0;}

int main(int argc, char** argv){ IplImage* interest_img; CvRect interest_rect; if( argc == 7 && ((interest_img=cvLoadImage(argv[1])) != 0 )) { interest_rect.x = atoi(argv[2]); interest_rect.y = atoi(argv[3]); interest_rect.width = atoi(argv[4]); interest_rect.height = atoi(argv[5]); int add = atoi(argv[6]);

IplImage *sub_img = cvCreateImageHeader( cvSize(interest_rect.width, interest_rect.height), interest_img->depth, interest_img->nChannels ); sub_img->origin = interest_img->origin; sub_img->widthStep = interest_img->widthStep; sub_img->imageData = interest_img->imageData + interest_rect.y * interest_img->widthStep + interest_rect.x * interest_img->nChannels; cvAddS( sub_img, cvScalar(add, add, add), sub_img ); cvReleaseImageHeader(&sub_img);

cvNamedWindow( "Roi_Add", CV_WINDOW_AUTOSIZE ); cvShowImage( "Roi_Add", interest_img ); cvWaitKey(); } return 0;}

Page 35: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Getting to Know OpenCV

Matrix and Image Operators

Page 36: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

ArrayMatrixImage

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

Page 37: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Array Operators

Web

Table

Page 38: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

cvCalcCovarMatrix

void cvCalcCovarMatrix (const CvArr** vects,int count,CvArr* cov_mat,CvArr* avg,int flags

);

void cvCalcCovarMatrix (const CvArr** vects,int count,CvArr* cov_mat,CvArr* avg,int flags

);

Detail

Example

Page 39: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Getting to Know OpenCV

Drawing Things

Page 40: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Drawing Functions

Drawing functions work with matrices/images of arbitrary depth

The boundaries of the shapes can be rendered with antialiasing– implemented only for 8-bit images for now

All the functions include the parameter color that uses a rgb value (that may be constructed with CV_RGB macro or the cvScalar function ) for color images and brightness for grayscale images

Page 41: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Drawing Functions

Clipping– If a drawn figure is partially or completely outside

the image, the drawing functions clip it. sub-pixel accuracy

– many drawing functions can handle pixel coordinates specified with sub-pixel accuracy, i.e., the coordinates can be passed as fixed-point numbers, encoded as integers.

– The number of fractional bits is specified by the shift parameter and the real point coordinates are calculated as

Point( , ) Point2f ( 2 , 2 )shift shiftx y x y

Page 42: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

CV_RGB

Constructs a color value

#define CV_RGB( r, g, b ) cvScalar( (b), (g), (r) )

Page 43: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Line

void cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 );

void cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 );

line_type Type of the line:8 (or 0) - 8-connected line.4 - 4-connected line.CV_AA - antialiased line.

Draws a line by the Bresenham’s algorithm

Page 44: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Rectangle

void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, double color, int thickness=1 int line_type=8, int shift=0 );

void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, double color, int thickness=1 int line_type=8, int shift=0 );

Draws simple, thick or filled rectangle

thickness Thickness of lines that make up the rectangle.Negative values, e.g., CV_FILLED, make the function to draw a filled rectangle.

Page 45: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Circle

void cvCircle( CvArr* img, CvPoint center, int radius, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

void cvCircle( CvArr* img, CvPoint center, int radius, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

Draws simple, thick or filled circle

Page 46: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Ellipsevoid cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

Draws simple or thick elliptic arc or fills ellipse sector

Page 47: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

EllipseBox

void cvEllipseBox( CvArr* img, CvBox2D box, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

void cvEllipseBox( CvArr* img, CvBox2D box, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

An alternate way to draw ellipse

typedef struct { CvPoint2D32f center; CvSize2D32f size; float angle;} CvBox2D;

typedef struct { CvPoint2D32f center; CvSize2D32f size; float angle;} CvBox2D;

Page 48: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

FillPoly

void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours, cvScalar color, int line_type=8, int shift=0 );

void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours, cvScalar color, int line_type=8, int shift=0 );

Fills polygons interior

pts Array of pointers to polygons. npts Array of polygon vertex counters. contours #contours that bind the filled region.

pts Array of pointers to polygons. npts Array of polygon vertex counters. contours #contours that bind the filled region.

Page 49: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

FillConvexPoly

void cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

void cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

Fills convex polygon

pts Array of pointers to a single polygon

npts Polygon vertex counter

pts Array of pointers to a single polygon

npts Polygon vertex counter

Page 50: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

PolyLine

void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

Draws simple or thick polygons

pts Array of pointers to polygons. npts Array of polygon vertex counters. contours #contours that bind the filled region.is_closed Indicates whether the polylines must be drawn closed.

pts Array of pointers to polygons. npts Array of polygon vertex counters. contours #contours that bind the filled region.is_closed Indicates whether the polylines must be drawn closed.

Page 51: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Getting to Know OpenCV

Drawing Text

Page 52: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

InitFont

void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 );

void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 );

Initializes font structure

Page 53: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

InitFont

void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 );

void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 );

Initializes font structure

Page 54: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

InitFont

void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 );

void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 );

Initializes font structure

Page 55: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

PutText

void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color );

void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color );

Draws text string

Page 56: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

GetTextSize

void cvGetTextSize( const char* text_string, const CvFont* font, CvSize* text_size, int* baseline );

void cvGetTextSize( const char* text_string, const CvFont* font, CvSize* text_size, int* baseline );

Retrieves width and height of text string

Page 57: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Getting to Know OpenCV

Data Persistence

Page 58: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Reading and Writing Images and Video

Topics on HighGUI

Page 59: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

A Simple Way to Save and Load OpenCV Matrices

CvMat A = cvMat( 5, 5, CV_32F, the_matrix_data );

cvSave( “my_matrix.xml”, &A );

. . .

// to load it then in some other program use …

CvMat* A1 = (CvMat*) cvLoad( “my_matrix.xml” );

CvMat A = cvMat( 5, 5, CV_32F, the_matrix_data );

cvSave( “my_matrix.xml”, &A );

. . .

// to load it then in some other program use …

CvMat* A1 = (CvMat*) cvLoad( “my_matrix.xml” );

Page 60: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

The structure CvFileStorage is a “black box” representation of the file storage associated with a file on disk.

On disk, the data is stored in an XML or YAML format.

File Storage

typedef struct CvFileStorage{ ... // hidden fields} CvFileStorage

typedef struct CvFileStorage{ ... // hidden fields} CvFileStorage

Page 61: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

XML & YAMLXML:

<?xml version="1.0">

<opencv_storage>

<A type_id="opencv-matrix">

<rows>3</rows>

<cols>3</cols>

<dt>f</dt>

<data>1. 0. 0. 0. 1. 0. 0. 0. 1.</data>

</A>

</opencv_storage>

XML:

<?xml version="1.0">

<opencv_storage>

<A type_id="opencv-matrix">

<rows>3</rows>

<cols>3</cols>

<dt>f</dt>

<data>1. 0. 0. 0. 1. 0. 0. 0. 1.</data>

</A>

</opencv_storage>

YAML:%YAML:1.0A: !!opencv-matrix rows: 3 cols: 3 dt: f data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1.]

YAML:%YAML:1.0A: !!opencv-matrix rows: 3 cols: 3 dt: f data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1.]

XML uses nested tags

YAML uses indentation

An example of 3×3 floating-point identity matrix A

Page 62: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Open & Release

CvFileStorage* cvOpenFileStorage( const char* filename, CvMemStorage* memstorage, int flags );

CvFileStorage* cvOpenFileStorage( const char* filename, CvMemStorage* memstorage, int flags );

void cvReleaseFileStorage( CvFileStorage** fs );

void cvReleaseFileStorage( CvFileStorage** fs );

flags – Can be one of the following:CV_STORAGE_READ - the storage is open for reading CV_STORAGE_WRITE - the storage is open for writing

Page 63: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Writing

Page 64: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Reading

Page 65: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Example: Writing & Reading XML

<?xml version=“1.0”?>

<opencv_storage>

<frame_count>10</frame_count>

<frame_size>320 200</frame_size>

<color_cvt_matrix type_id=“opencv-matrix”>

<rows>3</rows> <cols>3</cols>

<dt>f</dt>

<data>…</data></color_cvt_matrix>

</opencv_storage>

XML version of cfg.xml on disk

Page 66: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Example: Writing & Reading XML

<?xml version=“1.0”?>

<opencv_storage>

<frame_count>10</frame_count>

<frame_size>320 200</frame_size>

<color_cvt_matrix type_id=“opencv-matrix”>

<rows>5</rows> <cols>5</cols>

<dt>f</dt>

<data>…</data></color_cvt_matrix>

</opencv_storage>

<?xml version=“1.0”?>

<opencv_storage>

<frame_count>10</frame_count>

<frame_size>320 200</frame_size>

<color_cvt_matrix type_id=“opencv-matrix”>

<rows>5</rows> <cols>5</cols>

<dt>f</dt>

<data>…</data></color_cvt_matrix>

</opencv_storage>

XML version of cfg.xml on disk

Page 67: Getting to Know OpenCV 主講人:虞台文. Content Basic Structures Arrays, Matrices, and Images Matrix and Image Operators Drawing Things Drawing Text Data Persistence

Example: Writing & Reading XML

XML version of cfg.xml on disk<?xml version=“1.0”?><opencv_storage><frame_count>10</frame_count><frame_size>320 200</frame_size><color_cvt_matrix type_id=“opencv-matrix”><rows>5</rows> <cols>5</cols><dt>f</dt><data>…</data></color_cvt_matrix></opencv_storage>

<?xml version=“1.0”?><opencv_storage><frame_count>10</frame_count><frame_size>320 200</frame_size><color_cvt_matrix type_id=“opencv-matrix”><rows>5</rows> <cols>5</cols><dt>f</dt><data>…</data></color_cvt_matrix></opencv_storage>

#include <cv.h>#include <highgui.h>#include <stdio.h>

int main(int argc, char** argv){ printf("Example 3_19 Reading in cfg.xml\n"); CvFileStorage* fs = cvOpenFileStorage( "cfg.xml", 0, CV_STORAGE_READ );

int frame_count = cvReadIntByName(fs, 0, "frame_count", 5 /* default value */);

CvSeq* s = cvGetFileNodeByName(fs,0,"frame_size")->data.seq; int frame_width = cvReadInt((CvFileNode*) cvGetSeqElem(s,0)); int frame_height = cvReadInt((CvFileNode*) cvGetSeqElem(s,1));

CvMat* color_cvt_matrix = (CvMat*) cvRead(fs, 0); printf("frame_count=%d, frame_width=%d, frame_height=%d\n", frame_count,frame_width,frame_height);

cvReleaseFileStorage( &fs );}

#include <cv.h>#include <highgui.h>#include <stdio.h>

int main(int argc, char** argv){ printf("Example 3_19 Reading in cfg.xml\n"); CvFileStorage* fs = cvOpenFileStorage( "cfg.xml", 0, CV_STORAGE_READ );

int frame_count = cvReadIntByName(fs, 0, "frame_count", 5 /* default value */);

CvSeq* s = cvGetFileNodeByName(fs,0,"frame_size")->data.seq; int frame_width = cvReadInt((CvFileNode*) cvGetSeqElem(s,0)); int frame_height = cvReadInt((CvFileNode*) cvGetSeqElem(s,1));

CvMat* color_cvt_matrix = (CvMat*) cvRead(fs, 0); printf("frame_count=%d, frame_width=%d, frame_height=%d\n", frame_count,frame_width,frame_height);

cvReleaseFileStorage( &fs );}