Mat 为OpenCV中的核心数据结构,主要负责图像数据的保存,Mat创建方法有很多种
Mat构造函数
可以使用Mat构造函数,创建Mat,Mat构造函数有多种形式的参数,来满足要求
| Mat 主要构造函数 | Description |
| Mat() | 默认构造函数 |
| Mat(int rows, int cols, int type) | 矩阵的行和列或者是图片的高和宽 type为存储的数据格式为单通道还是多通道,以及是char 还是unshort等,其type参数为CV_8UC1, ..., CV_64FC4 等最多为4个通道,其中UC为数据类型为unsigned char SC为 char, 16UC为unsigned short , 16SC为short, 32SC为 int, 32FC为32位浮点型, 64FC为64位浮点型 |
| Mat(Size size, int type) | Size位创建一个二维矩阵,其参数为Size(cols, rows),type同上 |
| Mat(int rows, int cols, int type, const Scalar& s) | 带初始化的Mat创建,其中Scalar为初始化Mat中的值,为OpenCV的比较重要的数据结构,最多具有4个元素值,常用来传递像素值,例如RGB,其表达式可以为Scalar(a, b, c). 如果是单通道可以为Scalar(a) 如果数双通道为Scalar(a, b) |
| Mat(Size size, int type, const Scalar& s) | 带初始化的Mat创建 |
| Mat(int ndims, const int* sizes, int type) | ndims为Mat维度 sizes为个维度大小的数组 |
| Mat(const std::vector<int>& sizes, int type) | verctor形式参数初始化,其中sizes为各维度大小, |
| Mat(int ndims, const int* sizes, int type, const Scalar& s) | 带初始化的Mat创建 |
| Mat(const std::vector<int>& sizes, int type, const Scalar& s) | 带初始化的Mat创建 |
| Mat(const Mat& m) | 直接从已有的一个Mat中进行创建一个新的Mat 注意只创建Mat的头,其数据矩阵还是指向的同一块 |
| Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP) | 带数据初始化Mat创建,通常用于将一块数据转换成Mat场景中. step:Number of bytes each matrix row occupies 每个矩阵一行占用的字节数,实际值为cols*elemSize() 其中elemSize()为单个值占用的字节数*通道数,默认为AUTO_STEP |
| Mat(Size size, int type, void* data, size_t step=AUTO_STEP) | 带初始化的Mat创建,同上 |
| Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0) | 带初始化的Mat创建,step为本像素点与下一个像素点间隔几个数据,即该数据通道是几个通道, |
| Mat(const std::vector<int>& sizes, int type, void* data, const size_t* steps=0); | 带初始化的Mat创建,同上 |
| Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all()) | Mat初始化, Range(int _start, int _end),表示的是其行或列的开始以及结束位置,可以选择性的截取m中的一部分数据,其数据部分还是同一块,不会产生新的数据copy,只是相当于取指针 |
| Mat(const Mat& m, const Rect& roi) | Mat初始化,和 Range类似只是取其中一部分 |
| Mat(const Mat& m, const Range* ranges) | Mat初始化,只是规定行的起始位置,没有规定列的起始位置 |
| Mat(const Mat& m, const std::vector<Range>& ranges) | Mat初始化,只是规定行的起始位置,没有规定列的起始位置 |
用例大概如下:
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"using namespace cv;
using namespace std;void main()
{int sz[3] = { 2,2,2 };Mat L(3, sz, CV_8UC1, Scalar::all(12));Mat mtx();Mat test1(640, 480, CV_8UC1);Mat test2(Size(640, 480), CV_8UC1);Mat M(2, 2, CV_8UC3, Scalar(110, 0, 255));cout << "M= " << endl << " " << M << endl << endl;Mat test3(Size(2, 2), CV_8UC3, Scalar(120, 0, 255));cout << "test3= " << endl << " " << test3 << endl << endl;int sz2[2] = { 5,5};Mat test4(2, sz2, CV_8UC3, Scalar(12));cout << "test4= " << endl << " " << test4 << endl << endl;Mat test5(test4);cout << "test5= " << endl << " " << test5 << endl << endl;Mat test6(3,3, CV_8UC3, test5.data);cout << "test6= " << endl << " " << test6 << endl << endl;Mat test11(4, 4, CV_8UC3, test5.data, 12);cout << "test11= " << endl << " " << test11 << endl << endl;Mat test7(2, sz2, CV_8UC3, test5.data);cout << "test7= " << endl << " " << test7 << endl << endl;const size_t steps = 3;Mat test12(2, sz2, CV_8UC3, test5.data, &steps);cout << "test12= " << endl << " " << test12<< endl << endl;Mat test8(test7, Range(0,2), Range(0, 2));cout << "test8= " << endl << " " << test8 << endl << endl;Mat test9(test7, Rect(0,0, 3, 3));cout << "test8= " << endl << " " << test9 << endl << endl;Mat test10(test7, Range(0, 2));cout << "test10= " << endl << " " << test10 << endl << endl;}
Mat类创建方法
后续补充