Click here to load reader
View
215
Download
0
Embed Size (px)
Getting to Know OpenCV
ContentBasic StructuresArrays, Matrices, and ImagesMatrix and Image OperatorsDrawing ThingsDrawing TextData Persistence
Getting to Know OpenCVBasic Structures
Basic Structures (Point)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;}CvPoint pt1;
pt1.x = 20;pt1.y = 50CvPoint pt2 = cvPoint(20, 50);cxtypes.h
Basic Structures (Point)typedef struct CvPoint{ int x; int y;} CvPoint;cxtypes.htypedef struct CvPoint2D32f{ float x; float y;} CvPoint2D32f;typedef struct CvPoint2D64f{ double x; double y;} CvPoint2D64f;typedef struct CvPoint3D32f{ float x; float y; float z;} CvPoint3D32f;typedef struct CvPoint3D64f{ double x; double y; double z;} CvPoint3D64f;
Basic Structures (Size)typedef struct CvSize{ int width; int height;} CvSize;cxtypes.htypedef struct CvSize2D32f{ float width; float height;} CvSize2D32ftypedef struct CvRect{ int x; int y; int width; int height;} CvRect;
Basic Structures (Scalar)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 );
Getting to Know OpenCVArrays, Matrices, and Images
ArrayMatrixImagecxtypes.hEven 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
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;cxtypes.h
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;cxtypes.hCV_(S|U|F)CExamplesCV_8UC3CV_32FC132-bit floatsunsigned integer 8-bit triplets
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;cxtypes.hthe length of a row in bytes a pointer to a data array
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;cxtypes.h the height (rows) and width (cols) of the matrix
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;cxtypes.h Matrix Header
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;cxtypes.hSome Matrix Operators:// Create a new rows by cols matrix of type typeCvMat* 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 matCvMat* cvCloneMat(const cvMat* mat);
// Free the matrix mat, both header and datavoid cvReleaseMat(CvMat** mat);
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;cxtypes.hAccessing 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
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;cxtypes.hAccessing 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
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;cxtypes.hAccessing 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.
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;cxtypes.hAccessing matrix elements Hard way:cvPtr?D()cvGetReal?D()cvGet?D()cvSetReal?D()cvSet?D()cvmGet()cvmSet()
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;cxtypes.hAccessing 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.Ineffective
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;cxtypes.h#include #include #include
float sum( CvMat* mat ) { float s = 0.0f; for( int row=0; rowheight; row++ ) { float* ptr = mat->data.fl + row * mat->step/4; for( int col=0; colwidth; 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:
ArrayMatrixImagetypedef 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;
ArrayMatrixImagetypedef struct _IplImage {int nSize;int ID;int nChannels;int alphaChannel;int depth;char