Mat像素点的存储方法
Mat的数据类型
CV_8U - 8bit unsigned integers(0 ~ 255) 8-bit unsigned integer (uchar)
CV_8S - 8bit signed integers(-128 ~ 127) 8-bit signed integer(schar)
CV_16U - 16bit unsigned integers(0 ~ 65535) 16-bit unsigned integer(ushort)
CV_16S - 16bit signed integers(-32768 ~ 32767) 16-bit signed integer(short)
CV_32S - 32bit signed integers(-2147483648 ~ 2147483647) 32-bit signed integers(int)
CV_32F - 32bit floating-point numbers() 32-bit floating-point number(float)
CV_64F - 64bit floating-point numbers() 64-bit floating-point number(double)
Mat中一个数据(像素)点的格式的定义方式:
CV_[The number of bits per item][Signed or Unsigned][Type Prefix][The channel number]
显式创建Mat对象
1.构造函数创建
Mat M(3, 2, CV_8UC3, Scalar(0, 0, 255));
2.create函数创建
Mat M;
M.create(4, 4, CV_8UC(2));
使用create函数,无法初始化,如果新的需求跟原来的内存块不匹配,他才会重新分配内存区域,可以减少开辟内存的动作,提升效率
void demo3()
{
//创建二维矩阵
Mat M(3, 2, CV_8UC3, Scalar(0, 0, 255));
cout << "M = " << endl << M << endl;
//创建三维矩阵
int sz[3] = { 2, 2, 2 };
Mat L(3, sz, CV_8UC(1), Scalar::all(0));
//不支持二维以上的矩阵输出
}
//使用Mat的create函数,显式的创建Mat对象
void demo4()
{
Mat M;
cout << "用create函数显式创建一个4*4矩阵,输出查看初始值" << endl;
M.create(4, 4, CV_8UC1);
cout << "M = " << endl << M << endl;
cout << "修改前两个数据的值,输出修改值后的值" << endl;
M.ptr<uchar>(0)[0] = 12;
M.ptr<uchar>(0)[1] = 32;
cout << "M = " << endl << M << endl;
cout << "再次调用create函数,创建相同大小的矩阵,查看是否对原来的内存区域做了初始化或者重新开辟" << endl;
M.create(4, 4, CV_8UC1);
cout << "M = " << endl << M << endl;
cout << "再次调用create函数,创建不同大小,创建3*3" << endl;
M.create(3, 3, CV_8UC1);
cout << "M = " << endl << M << endl;
cout << "再次调用create函数,创建5*5" << endl;
M.create(5, 5, CV_8UC1);
cout << "M = " << endl << M << endl;
}