OPENCV 2. 数据结构

来源:互联网 发布:数据库的定义及特点 编辑:程序博客网 时间:2024/06/14 12:12

Opencv的数据结构定义于types_c.h中,下面对其一一介绍:
a. CvRect 矩形类

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

x和y表示矩形的起始坐标,而width和height则表示矩形的宽和高;其构造函数为:

CV_INLINE  CvRect  cvRect( int x, int y, int width, int height ){    CvRect r;    r.x = x;    r.y = y;    r.width = width;    r.height = height;    return r;}

此外还有IplROI也类似于矩形类;

b. 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;}

此外还有表示浮点型的点:CvPoint2D32f、CvPoint2D64f;三维点CvPoint3D32f、cvPoint2D64f;

c. 尺寸类

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

同样有构造函数 CV_INLINE CvSize cvSize( int width, int height ),和更高精度的尺寸类CvSize2D32f;

d. 颜色类

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

表示图像的B(blue)、G(green)、R(red)、alpha - 图像的透明度;构造函数有:

CV_INLINE  CvScalar  cvScalar( double val0, double val1 CV_DEFAULT(0),                               double val2 CV_DEFAULT(0), double val3 CV_DEFAULT(0))CV_INLINE  CvScalar  cvRealScalar( double val0 )CV_INLINE  CvScalar  cvScalarAll( double val0123 )

e. 矩阵类

typedef struct CvMat{    int type;        // 数据类型,经过计算后的一串数字    int step;        // 一行数据的长度,单位为字节    /* for internal use only */    int* refcount;   // 数据引用计数,内部使用,不用管    int hdr_refcount;    union    {        uchar* ptr;        short* s;        int* i;        float* fl;        double* db;    } data;           // 数据地址#ifdef __cplusplus    union    {        int rows;        int height;    };    union    {        int cols;        int width;    };#else    int rows;    int cols;#endif}CvMat;

1.大多数情况下,step等于 width * 单个数据的长度,但是也存在例外情况
2.ptr、s、i、fl和db表示的是同一个地址。CvMat这样进行定义的目的是为了更好的操作数据;
3.Mat A = B;这时只进行浅拷贝,如果需要用到深拷贝,则需要使用cvCloneMat

int main()  {      double AA[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };    CvMat Ma=cvMat(3, 4, CV_64FC1, AA);    CvMat Mb = Ma;    CvMat * Mc = cvCloneMat(&Ma);    printf("Address of AA = %d\nAddress of Ma = %d\nAddress of Mb = %d\nAddress of Mc = %d\n",         (int)AA, (int)Ma.data.ptr,  (int)Mb.data.ptr, (int)Mc->data.ptr);    cvReleaseMat(&Mc);}

得到的结果是:
Address of AA = 1964272
Address of Ma = 1964272
Address of Mb = 1964272
Address of Mc = 3658064

f. IplImage图像类

typedef struct _IplImage{    int  nSize;             /* sizeof(IplImage) */    int  ID;                /* version (=0)*/    int  nChannels;         /* Most of OpenCV functions support 1,2,3 or 4 channels */    int  alphaChannel;      /* Ignored by OpenCV */    int  depth;             /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,                               IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */    char colorModel[4];     /* Ignored by OpenCV */    char channelSeq[4];     /* ditto */    int  dataOrder;         /* 0 - interleaved color channels, 1 - separate color channels.                               cvCreateImage can only create interleaved images */    int  origin;            /* 0 - top-left origin,                               1 - bottom-left origin (Windows bitmaps style).  */    int  align;             /* Alignment of image rows (4 or 8).                               OpenCV ignores it and uses widthStep instead.    */    int  width;             /* Image width in pixels.                           */    int  height;            /* Image height in pixels.                          */    struct _IplROI *roi;    /* Image ROI. If NULL, the whole image is selected. */    struct _IplImage *maskROI;      /* Must be NULL. */    void  *imageId;                 /* "           " */    struct _IplTileInfo *tileInfo;  /* "           " */    int  imageSize;         /* Image data size in bytes                               (==image->height*image->widthStep                               in case of interleaved data)*/    char *imageData;        /* Pointer to aligned image data.         */    int  widthStep;         /* Size of aligned image row in bytes.    */    int  BorderMode[4];     /* Ignored by OpenCV.                     */    int  BorderConst[4];    /* Ditto.                                 */    char *imageDataOrigin;  /* Pointer to very origin of image data                               (not necessarily aligned) -                               needed for correct deallocation */}IplImage;

1.roi是图像的感兴趣的区域,当进行显示时,只会显示感兴越区域;
2.cvCopy会拷贝图像的感兴趣区域,而cvCloneImage则会拷贝所有的值;

原创粉丝点击