1 / 67

Getting to Know OpenCV

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. cxtypes.h. Basic Structures (Point). typedef struct CvPoint { int x; int y;

kristy
Download Presentation

Getting to Know OpenCV

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Getting to Know OpenCV 主講人:虞台文

  2. Content • Basic Structures • Arrays, Matrices, and Images • Matrix and Image Operators • Drawing Things • Drawing Text • Data Persistence

  3. Getting to Know OpenCV Basic Structures

  4. cxtypes.h Basic Structures (Point) typedefstruct CvPoint { int x; int y; }CvPoint; CV_INLINECvPointcvPoint(int x, int y) { CvPoint p; p.x = x; p.y = y; return p; } CvPoint pt1; pt1.x = 20; pt1.y = 50 CvPoint pt2 = cvPoint(20, 50);

  5. cxtypes.h Basic Structures (Point) typedefstruct CvPoint { int x; int y; }CvPoint; typedef struct CvPoint3D32f { float x; float y; float z; } CvPoint3D32f; typedef struct CvPoint2D32f { float x; float y; } CvPoint2D32f; typedef struct CvPoint3D64f { double x; double y; double z; } CvPoint3D64f; typedef struct CvPoint2D64f { double x; double y; } CvPoint2D64f;

  6. cxtypes.h Basic Structures (Size) typedef struct CvSize { int width; int height; } CvSize; typedef struct CvRect { int x; int y; int width; int height; } CvRect; typedef struct CvSize2D32f { float width; float height; } CvSize2D32f

  7. cxtypes.h Basic Structures (Scalar) typedef struct CvScalar { double val[4]; } CvScalar; /* 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 );

  8. Getting to Know OpenCV Arrays, Matrices, and Images

  9. cxtypes.h ArrayMatrixImage 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

  10. cxtypes.h ArrayMatrixImage 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;

  11. cxtypes.h ArrayMatrixImage 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; CV_<bit_depth>(S|U|F)C<number_of_channels> 32-bit floats CV_32FC1 unsigned integer 8-bit triplets CV_8UC3 Examples

  12. cxtypes.h ArrayMatrixImage 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; the length of a row in bytes a pointer to a data array

  13. cxtypes.h ArrayMatrixImage 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; the height (rows) and width(cols) of the matrix

  14. cxtypes.h ArrayMatrixImage 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; Matrix Header

  15. cxtypes.h ArrayMatrixImage 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 data CvMat* cvCreateMatHeader(int rows, int cols, int type) // allocates image, matrix or multi-dimensional array data void cvCreateData(CvArr* arr) // Initialize header on existing CvMat structure CvMat* cvInitMatHeader(CvMat* mat, int rows, int cols, int type, void* data=NULL, int step=CV_AUTOSTEP) // Like cvInitMatHeader() but allocates CvMat as well CvMat cvMat(int rows, int cols, int type, void* data=NULL) ; // Allocate a new matrix just like the matrix ‘mat’ CvMat* cvCloneMat(const cvMat* mat); // Free the matrix ‘mat’, both header and data void cvReleaseMat(CvMat** mat); 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;

  16. cxtypes.h ArrayMatrixImage 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; 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);

  17. cxtypes.h ArrayMatrixImage 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; 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)) ;

  18. cxtypes.h ArrayMatrixImage 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; 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.

  19. cxtypes.h ArrayMatrixImage 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; Accessing matrix elements Hard way: cvPtr?D() cvGetReal?D() cvGet?D() cvSetReal?D() cvSet?D() cvmGet() cvmSet()

  20. cxtypes.h ArrayMatrixImage Using these pointer functions to gain access to a particular point in the matrix and then use pointer arithmetic to move around in the matrix from there. 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; Accessing matrix elements Hard way: cvPtr?D() cvGetReal?D() Ineffective cvGet?D() cvSetReal?D() cvSet?D() cvmGet() cvmSet()

  21. cxtypes.h ArrayMatrixImage #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 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; Right way:

  22. ArrayMatrixImage • 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;

  23. ArrayMatrixImage • 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

  24. ArrayMatrixImage • 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

  25. ArrayMatrixImage • 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

  26. ArrayMatrixImage • 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.

  27. Accessing Image Data #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"); } • 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;

  28. void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); ROI • typedef struct _IplImage { • ........................ • int width; • int height; • ........................ • struct _IplROI* roi; • ........................ • char* imageData; • int widthStep; • ........................ • } IplImage; cvSetImageROI

  29. void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); ROI • typedef struct _IplImage { • ........................ • int width; • int height; • ........................ • struct _IplROI* roi; • ........................ • char* imageData; • int widthStep; • ........................ • } IplImage; cvSetImageROI cvAddS

  30. void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); ROI #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; } • typedef struct _IplImage { • ........................ • int width; • int height; • ........................ • struct _IplROI* roi; • ........................ • char* imageData; • int widthStep; • ........................ • } IplImage; cvSetImageROI cvAddS

  31. void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); ROI by widthStep • typedef struct _IplImage { • ........................ • int width; • int height; • ........................ • struct _IplROI* roi; • ........................ • char* imageData; • int widthStep; • ........................ • } IplImage; NULL

  32. void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); 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; NULL NULL

  33. void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); 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; NULL NULL cvAddS

  34. void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); 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; } 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; NULL cvAddS

  35. Getting to Know OpenCV Matrix and Image Operators

  36. ArrayMatrixImage 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

  37. Array Operators Web Table

  38. cvCalcCovarMatrix • void cvCalcCovarMatrix ( • const CvArr** vects, • int count, • CvArr* cov_mat, • CvArr* avg, • int flags • ); Example Detail

  39. Getting to Know OpenCV Drawing Things

  40. Drawing Functions • Drawing functions work with matrices/images of arbitrarydepth • 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

  41. 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

  42. CV_RGB Constructs a color value #define CV_RGB( r, g, b ) cvScalar( (b), (g), (r) )

  43. Line Draws a line by the Bresenham’s algorithm 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.

  44. Rectangle Draws simple, thick or filled rectangle void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, double color, int thickness=1 int line_type=8, int shift=0 ); • thickness • Thickness of lines that make up the rectangle. • Negative values, e.g.,CV_FILLED, make the function to draw a filled rectangle.

  45. Circle Draws simple, thick or filled circle void cvCircle( CvArr* img, CvPoint center, int radius, cvScalar color, int thickness=1 int line_type=8, int shift=0 );

  46. Ellipse Draws simple or thick elliptic arc or fills ellipse sector 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 );

  47. EllipseBox An alternate way to draw ellipse void cvEllipseBox( CvArr* img, CvBox2D box, cvScalar color, int thickness=1 int line_type=8, int shift=0 ); typedef struct { CvPoint2D32f center; CvSize2D32f size; float angle; } CvBox2D;

  48. FillPoly Fills polygons interior void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours, cvScalar color, int line_type=8, int shift=0 ); pts Array of pointers to polygons. npts Array of polygon vertex counters. contours #contours that bind the filled region.

  49. Fills convex polygon FillConvexPoly void cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts, cvScalar color, int thickness=1 int line_type=8, int shift=0 ); • pts • Array of pointers to a single polygon • npts • Polygon vertex counter

  50. PolyLine Draws simple or thick polygons 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 ); 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.

More Related