初识Mat矩阵

Mat矩阵

什么是Mat矩阵

Mat矩阵是一个类,定义于core.cpp中
Mat举证包含两个部分
- 矩阵头(包含矩阵的大小,存储方式,矩阵存储地址)
- 指向矩阵包含像素值的指针(data)

<span style="font-size:12px;">class CV_EXPORTS Mat  
{  
public:  
    // ... a lot of methods ...  
    ...  

    /*! includes several bit-fields: 
         - the magic signature 
         - continuity flag 
         - depth 
         - number of channels 
     */  
    int flags;  
    //! the array dimensionality, >= 2  
    int dims;  
    //! the number of rows and columns or (-1, -1) when the array has more than 2 dimensions  
    int rows, cols;  
    //! pointer to the data  
    uchar* data;  

    //! pointer to the reference counter;  
    // when array points to user-allocated data, the pointer is NULL  
    int* refcount;  

    // other members  
    ...  
};</span> 

Mat矩阵中的数据格式

Mat的存储是逐行存储的,矩阵中的数据类型包括:
- Mat_对应的是CV_8U
- Mat_对应的是CV_8U
- Mat_对应的是CV_8S
- Mat_对应的是CV_32S
- Mat_对应的是CV_32F
- Mat_对应的是CV_64F

对应的数据深度如下:

  • CV_8U - 8-bit unsigned integers ( 0..255 )
  • CV_8S - 8-bit signed integers ( -128..127 )
  • CV_16U - 16-bit unsigned integers ( 0..65535 )
  • CV_16S - 16-bit signed integers ( -32768..32767 )
  • CV_32S - 32-bit signed integers ( -2147483648..2147483647 )
  • CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )
  • CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

如何创建一个Mat矩阵

  • 使用Mat矩阵的构造函数
Mat::Mat();//default
Mat::Mat(int rows,int cols,int type);
Mat::Mat(Size size,int type);
Mat::Mat(int rows, int cols,int type,const Scalar& s);
Mat::Mat(Size size, int type ,const Scalar& s);
Mat:: Mat(const Mat& m);
//参数说明:  
//int rows:高  
//int cols:宽  
//int type:参见"Mat类型定义"  
//Size size:矩阵尺寸,注意宽和高的顺序:Size(cols, rows)  
//const Scalar& s:用于初始化矩阵元素的数值  
//const Mat& m:拷贝m的矩阵头给新的Mat对象,但是不复制数据!相当于创建了m的一个引用对象

//例子1:创建100*90的矩阵,矩阵元素为3通道32位浮点型  
cv::Mat M(100, 90, CV_32FC3);  
//例子2:使用一维或多维数组来初始化矩阵,  
double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};  
cv::Mat M = cv::Mat(3, 3, CV_64F, m);  

//2.使用create函数:  
Mat a = create(10, 9, CV_16U);  //创建10*9的矩阵,矩阵元素为16位无符号整型  
//create的一个特殊用法:如果初始化的时候没有传入size的参数,或者后面需要改变size的参数,可以使用create来调整  
// make 7x7 complex matrix filled with 1+3j.  
cv::Mat M(7,7,CV_32FC2,Scalar(1,3));  
// and now turn M to 100x60 15-channel 8-bit matrix.  
// The old content will be deallocated:隐式使用release()释放  
M.create(100,60,CV_8UC(15));</span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值