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

基础数据结构


一、基本几何

  • 矩形

"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[];
}
CvScalar;

二、矩阵描述

  • Mat

Ref: opencv基础知识------IplImage, CvMat, Mat 的关系和相互转换

在openCV中,Mat类型与CvMat和IplImage类型都可以代表和显示图像,

但是,Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化。

而CvMat和IplImage类型更侧重于“图像”,openCV对其中的图像操作(缩放、单通道提取、图像阈值操作等)进行了优化。

补充:

IplImage由CvMat派生,而CvMat由CvArr派生即CvArr -> CvMat -> IplImage

CvArr用作函数的参数,无论传入的是CvMat或IplImage,内部都是按CvMat处理。

三、图片描述

  • 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[]; /* Ignored by OpenCV */
char channelSeq[]; /* 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[]; /* Ignored by OpenCV. */
int BorderConst[]; /* 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;

[1] 初始化一个矩阵

#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, , , CV_32FC1, vals);
printf("Ex 3_3 matrix initialized\n");
}

[2] 相关源代码:"modules/core/src/array.cpp"

[3] 初始化细节: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 < || cols <= )
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 = ;
arr->hdr_refcount = ; int pix_size = CV_ELEM_SIZE(type);
int min_step = arr->cols*pix_size; if( step != CV_AUTOSTEP && step != )
{
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 == || arr->step == min_step ? CV_MAT_CONT_FLAG : ); icvCheckHuge( arr );
return arr;
}

[4] 其他相关API: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 );

四、存取像素点

  • 宏方法

"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( , , CV_32FC1 );
// 1. 指针法
float element_3_2 = 7.7;
*( (float*)CV_MAT_ELEM_PTR( *mat, , ) ) = element_3_2; // 2. 处理“浮点型,单通道矩阵”
cvmSet( mat, , , 0.5000 ); // 3. 较常用
cvSetReal2D( mat, , , 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, , ),
    CV_MAT_ELEM( *mat, float, , ),
    CV_MAT_ELEM( *mat, float, , ) );
}
  • Get pixel

"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 = ,
unsigned* precalc_hashval = NULL
);
  • Set Pixel
// 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 );
  • 综合性例子
/* 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=; row<mat->height; row++ ) {
float* ptr = mat->data.fl + row * mat->step/;
for( int col=; col<mat->width; col++ ) {
s += *ptr++;
}
}
return( s );
}; int main(int argc, char** argv)
{
CvMat *mat = cvCreateMat(,,CV_32FC1);
float element_3_2 = 7.7;
*((float*)CV_MAT_ELEM_PTR( *mat, ,) ) = element_3_2;
cvmSet(mat,,,0.5000);
cvSetReal2D(mat,,,0.5000);
float s = sum(mat);
printf("%f\n",s);
return ;
}

[OpenCV] Basic data types - Matrix的更多相关文章

  1. Basic SAP Data Types

    Basic SAP Data Types 6 out of 11 rated this helpful - Rate this topic The parameter types that the M ...

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

  3. Data Types

    原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...

  4. 一、spark 数据类型(Data Types)

    Data Types - MLlib(数据类型)       MLlib支持存储在单机上的局部向量和局部矩阵,也可以支持通过一个或多个RDD(可伸缩数据集)表示的分布式矩阵.局部向量和局部矩阵是用作公 ...

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

    [12c]扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE 在12c中,与早期版本相比,诸如VARCHAR2, NAVARCHAR2以及 RAW这些数据类型的 ...

  6. Oracle Schema Objects——Tables——Oracle Data Types

    Oracle Schema Objects Oracle Data Types 数据类型 Data Type Description NUMBER(P,S) Number value having a ...

  7. C and SQL data types for ODBC and CLI

    C and SQL data types for ODBC and CLI   This topic lists the C and SQL data types for ODBC and CLI a ...

  8. allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[System.DateTime]

    allow zero datetime=true导致datetime转换失败:MySql.Data.Types.MySqlDateTime”的对象无法转换为类型“System.Nullable`1[S ...

  9. "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."

    "SQL Server does not handle comparison of NText, Text, Xml, or Image data types." sql2000 ...

随机推荐

  1. CSS揭秘读书笔记 (一)

    CSS揭秘读书笔记      (一) 一.半透明边框 要想实现半透明边框可以使用border: border: 10px  solid  hsla(0,0%,100%,.5); background: ...

  2. linux nc (NetCat) 命令详解

    原文:http://www.huanxiangwu.com/477/linux-nc-netcat 一.版本通常的Linux发行版中都带有NetCat(简称nc),甚至在拯救模式光盘中也由busybo ...

  3. 《Windows核心编程》学习笔记(9)– 在win7或者vista系统下提升一个进程的运行权限

    win7或者vista默认运行程序是在受限制的环境下运行的,以减轻病毒对于系统的破坏.那么我们怎样才能提升一个进程的权限以至让它在 管理员模式下运行.当然CreateProcess函数没有提供这个功能 ...

  4. 使用 Aircrack-ng 破解 WEP 和 WPA/WPA2 加密的 Wi-Fi 密码。(转)

    1.首先请不要使用此方法去搞破坏,去蹭Wi-Fi,因为不装逼地说,我认为技术本身的价值很大,尤其是在学习这个技术的过程中解决遇到的问题,当经过重重困难最后终于成功之后的喜悦又怎么能拿去蹭网呢.我在此过 ...

  5. ClamAV安装使用及API例子

    ClamAV是一款由Sourcefire组织开发的开源杀毒引擎,Sourcefire同时也是Snort入侵检测引擎的所有者.ClamAV提供了一种更为快速灵活的框架用以检测恶意代码和软件产品.可以作为 ...

  6. html页面禁止自动填充浏览器记住的密码

    现在的浏览器功能越来越强大,比如Chrome浏览器,在一个系统login的时候我们一般会记住密码,那么在整个系统中,浏览器一旦遇到 type="password"的控件,就会把密码 ...

  7. win7下虚拟机安装mac 转载自 http://itbbs.pconline.com.cn/50602805.html

    最近,不断有人问起,如何在vmware下安装MAC系统.起因是以前曾发过一篇贴,在vmware8下安装MAC的方法.于是,重新下载了最新版苹果系统10.8.5,终于成功安装.现将注意事项及过程与各位朋 ...

  8. java模拟开锁

    java模拟开锁 service qq:928900200 Introduction to Computer Science II: CSCI142Fall 2014Lab #1Instructor: ...

  9. 移动应用安全开发指南(Android)--完结篇(http://www.bubuko.com/infodetail-577312.html)

    1.认证和授权 概述 认证是用来证明用户身份合法性的过程,授权是用来证明用户可以合法地做哪些事的过程,这两个过程一般是在服务器端执行的,但也有的APP出于性能提升或用户体验等原因,将其做在客户端完成, ...

  10. Java对象创建阶段的代码调用顺序

    在创建阶段系统通过下面的几个步骤来完成对象的创建过程 为对象分配存储空间 开始构造对象 从超类到子类对static成员进行初始化 超类成员变量按顺序初始化,递归调用超类的构造方法 子类成员变量按顺序初 ...