[openCV] Basic data types

http://docs.opencv.org/2.4.13/

Basis


矩形

"modules/core/src/drawing.cpp"

CV_IMPL void
cvRectangle( CvArr* _img, CvPoint pt1, CvPoint pt2,
             CvScalar color, int thickness,
             int line_type, int shift )
{
    cv::Mat img = cv::cvarrToMat(_img);
    cv::rectangle( img, pt1, pt2, color, thickness, line_type, shift );
}

点 

"modules/core/include/opencv2/core/types_c.h" 

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

面积

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

大数表示 (例如:表示颜色

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


IplImage


"modules/core/include/opencv2/core/types_c.h"

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  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;

矩阵CvMat



"modules/core/include/opencv2/core/types_c.h" 708L, 58287C

typedef struct CvMat
{
    int type;
    int step;

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

    union
    {
        uchar*;
        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;

初始化一个矩阵:

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

int main()
{
  // Create an OpenCV Matrix containing some fixed data.
  //
  float vals[] = { 0.866025, -0.500000, 0.500000, 0.866025};

  CvMat rotmat;

  cvInitMatHeader(
    &rotmat,
    2,
    2,
    CV_32FC1,
    vals
  );
  printf("Ex 3_3 matrix initialized\n");
}

"modules/core/src/array.cpp"

 Init Matrax ( ):

CV_IMPL CvMat*
cvInitMatHeader( CvMat* arr, int rows, int cols,
                 int type, void* data, int step )
{
    if( !arr )
        CV_Error( CV_StsNullPtr, "" );

    if( (unsigned)CV_MAT_DEPTH(type) > CV_DEPTH_MAX )
        CV_Error( CV_BadNumChannels, "" );

    if( rows < 0 || cols <= 0 )
        CV_Error( CV_StsBadSize, "Non-positive cols or rows" );

    type = CV_MAT_TYPE( type );
    arr->type = type | CV_MAT_MAGIC_VAL;
    arr->rows = rows;
    arr->cols = cols;
    arr->data.ptr = (uchar*)data;
    arr->refcount = 0;
    arr->hdr_refcount = 0;

    int pix_size = CV_ELEM_SIZE(type);
    int min_step = arr->cols*pix_size;

    if( step != CV_AUTOSTEP && step != 0 )
    {
        if( step < min_step )
            CV_Error( CV_BadStep, "" );
        arr->step = step;
    }
    else
    {
        arr->step = min_step;
    }

    arr->type = CV_MAT_MAGIC_VAL | type |
        (arr->rows == 1 || arr->step == min_step ? CV_MAT_CONT_FLAG : 0);

    icvCheckHuge( arr );
    return arr;
}

Relevant Functions

// Create a new rows by cols matrix of type ‘type’.
//
CvMat* cvCreateMat( int rows, int cols, int type );
// Create only matrix header without allocating data
//
CvMat* cvCreateMatHeader( int rows, int cols, int type );
// 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 );

存取数据 in CvMat



1. "modules/core/include/opencv2/core/types_c.h"

#define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size )  \
    (assert( (unsigned)(row) < (unsigned)(mat).rows &&                (unsigned)(col) < (unsigned)(mat).cols ),        (mat).data.ptr + (size_t)(mat).step*(row) + (pix_size)*(col))

#define CV_MAT_ELEM_PTR( mat, row, col )                 \
    CV_MAT_ELEM_PTR_FAST( mat, row, col, CV_ELEM_SIZE((mat).type) )

#define CV_MAT_ELEM( mat, elemtype, row, col )           \
    (*(elemtype*)CV_MAT_ELEM_PTR_FAST( mat, row, col, sizeof(elemtype)))

宏取值,简单但通用性不强:

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

int main()
{
  CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );  // 1. 指针法
  float element_3_2 = 7.7;
  *( (float*)CV_MAT_ELEM_PTR( *mat, 3, 2 ) ) = element_3_2;  // 2. 处理“浮点型,单通道矩阵”
  cvmSet( mat, 2, 2, 0.5000 );  // 3. 较常用
  cvSetReal2D( mat, 3, 3, 0.3300 );

  printf("Exercise 3_5, matrix created and accessed [3,2]=%f, [2,2]=%f, [3,3]=%f\n",    CV_MAT_ELEM( *mat, float, 3, 2 ),    CV_MAT_ELEM( *mat, float, 2, 2 ),    CV_MAT_ELEM( *mat, float, 3, 3 ) );
}

2. "modules/core/src/array.cpp"

// CvMat and IplImage element functions
double cvGetReal1D( const  CvArr* arr, int idx0 );
double cvGetReal2D( const  CvArr* arr, int idx0, int idx1 );
double cvGetReal3D( const  CvArr* arr, int idx0, int idx1, int idx2 );
double cvGetRealND( const  CvArr* arr, int* idx );
CvScalar cvGet1D( const CvArr*  arr, int idx0 );
CvScalar cvGet2D( const CvArr*  arr, int idx0, int idx1 );
CvScalar cvGet3D( const CvArr*  arr, int idx0, int idx1, int idx2 );
CvScalar cvGetND( const CvArr*  arr, int* idx );

返回值类型占用空间大,也有小的方案 as following:

// Pointer access to matrix structures
uchar* cvPtr1D(
      const CvArr* arr,
      int          idx0,
      int*         type = NULL
);
uchar* cvPtr2D(
      const CvArr* arr,
      int          idx0,
      int          idx1,
      int*         type = NULL
);
uchar* cvPtr3D(
      const CvArr* arr,
      int          idx0,
      int          idx1,
      int          idx2,
      int*         type = NULL
);
uchar* cvPtrND(
      const CvArr* arr,
      int*         idx,
      int*         type            = NULL,
      int          create_node     = 1,
      unsigned*    precalc_hashval = NULL
);

3. Set 数据

// Set element functions for CvMat or IplImage.
void cvSetReal1D( CvArr* arr, int idx0, double value );
void cvSetReal2D( CvArr* arr, int idx0, int idx1, double value );
void cvSetReal3D(
   CvArr* arr,
   int idx0,
   int idx1,
   int idx2,
   double value
);
void cvSetRealND( CvArr* arr, int* idx, double value );
void cvSet1D( CvArr* arr, int idx0, CvScalar value );
void cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );
void cvSet3D(
   CvArr* arr,
   int idx0,
   int idx1,
   int idx2,
   CvScalar value
);
void cvSetND( CvArr* arr, int* idx, CvScalar value );

4. 综合性例子

/* License:
   Oct. 3, 2008
   Right to use this code in any way you want without warrenty, support or any guarentee of it working.

   BOOK: It would be nice if you cited it:
   Learning OpenCV: Computer Vision with the OpenCV Library
     by Gary Bradski and Adrian Kaehler
     Published by O‘Reilly Media, October 3, 2008

   AVAILABLE AT:
     http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
     Or: http://oreilly.com/catalog/9780596516130/
     ISBN-10: 0596516134 or: ISBN-13: 978-0596516130    

   OTHER OPENCV SITES:
   * The source code is on sourceforge at:
     http://sourceforge.net/projects/opencvlibrary/
   * The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
     http://opencvlibrary.sourceforge.net/
   * An active user group is at:
     http://tech.groups.yahoo.com/group/OpenCV/
   * The minutes of weekly OpenCV development meetings are at:
     http://pr.willowgarage.com/wiki/OpenCV
*/

#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;
}

时间: 2024-10-01 06:14:43

[openCV] Basic data types的相关文章

【12c】扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE

[12c]扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE 在12c中,与早期版本相比,诸如VARCHAR2, NAVARCHAR2以及 RAW这些数据类型的大小会从4K以及2K字节扩展至32K字节.只要可能,扩展字符的大小会降低对LOB 数据类型的使用.为了启用扩展字符大小,你必须将MAX_STRING_SIZE的初始数据库参数设置为EXTENDED. 要使用扩展字符类型需要执行以下过程: 1.关闭数据库 2.以升级模式重启数据库3.更改参数: ALT

C++ Data Types

Primitive Built-in Types: C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types: Type  Keyword Boolean bool Character char Integer int Floating point float Dou

Oracle Schema Objects——Tables——Oracle Data Types

Character Data Types 字符数据类型 Character data types store character (alphanumeric) data in strings. 字符数据类型存储在字符串中的字符 (字母或数字) 数据. The most commonly used character data type is VARCHAR2, which is the most efficient option for storing character data. 最常用的字

Core Java Volume I — 3.3. Data Types

3.3. Data TypesJava is a strongly typed language(强类型语音). This means that every variable must have a declared type(每个变量都必须声明类型). There are eight primitive types in Java(Java有8种原始类型). Four of them are integer types; two are floatingpoint number types;

高性能MySQL笔记-第4章Optimizing Schema and Data Types

1.Good schema design is pretty universal, but of course MySQL has special implementation details to consider. In a nutshell, it’s a good idea to keep things as small and simple as you can. MySQL likes simplicity, and so will the people who have to wo

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

【翻译】苹果官网的命名规范之 Naming Properties and Data Types

苹果官方原文:Naming Properties and Data Types 前言:纯属练习英语和学习.翻译错误和不通顺的地方敬请谅解和指正.O(∩_∩)O 属性和数据类型的命名 本节讲述了属性定义.变量.常量.通知和异常的常用命名规范. l 定义属性和变量 一个属性定义会影响该属性的访问方法的定义.所以属性的命名规范大体上和访问器(getter,setter)的命名规范是一致的.如果一个属性是表达一个名词或者动词,那么定义如下: @property(…) type nounOrVerb 例如

Data Types

原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administration Data Types Each value manipulated by Oracle Database has a data type. The data type of a value associates a fixed set of properties with the va

VHDL之User-defined data types

VHDL allows the user to define own data types. 1 user-defined integer types -- This is indeed the pre-defined type integer type integer is range -2147483647 to +2147483647; -- indeed the pre-defined type natural type natural is range 0 to +2147483647