构造函数
OpenCV Mat实例详解一中已介绍了部分OpenCV Mat构造函数,下面继续介绍剩余部分构造函数。
Mat (const std::vector< _Tp > &vec, bool copyData=false);
vec 包含数据的vec对象
copyData 是否拷贝数据,true— 拷贝数据,false—不拷贝数据。
下面新建一个控制台应用程序,来演示,该构造函数的使用,在新建程序中加入如下代码:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{vector<uchar> vec = {1,2,3,4,5,6,7,8,9};Mat tmp = Mat(vec, 1);if (tmp.empty())cout << "构建Mat对象失败!" << endl;elsecout << "构建Mat对象成功!" << endl;waitKey(0);return 0;}
试运行结果如下:
说明构建Mat对象成功。
Mat (const std::initializer_list< _Tp > list);
修改上面示例代码,来演示该构造函数的用法,修改后的代码如下:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{//vector<uchar> vec = {1,2,3,4,5,6,7,8,9};//Mat tmp = Mat(vec, 1);initializer_list<uchar> list = { 1,2,3,4,5,6,7,8,9 };Mat tmp = Mat(list);if (tmp.empty())cout << "构建Mat对象失败!" << endl;elsecout << "构建Mat对象成功!" << endl;waitKey(0);return 0;}
试运行,结果如下:
Mat (const std::initializer_list< int > sizes, const std::initializer_list< _Tp > list);
sizes 构建对象维度参数(rows,cols)
list 包含数据对的list对象
修改上面示例代码,来演示该构造函数的用法,修改后的代码如下:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{//vector<uchar> vec = {1,2,3,4,5,6,7,8,9};//Mat tmp = Mat(vec, 1);initializer_list<uchar> list = {1,2,3,4,5,6,7,8,9,10,11,12};//Mat tmp = Mat(list);initializer_list<int> list1 = {3,4};Mat tmp = Mat(list1,list);if (tmp.empty())cout << "构建Mat对象失败!" << endl;else{cout << "构建Mat对象成功!" << endl;cout << "构建Mat对象rows:" << tmp.rows << endl;cout << "构建Mat对象cols:" << tmp.cols << endl;}waitKey(0);return 0;}
试运行,结果如下:
Mat (const std::array< _Tp, _Nm > &arr, bool copyData=false);
arr含有构建Mat对象数据的array对象
copyData 是否拷贝数据,true— 拷贝数据,false—不拷贝数据。
修改上面示例代码,来演示该构造函数的用法,修改后的代码如下:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{//vector<uchar> vec = {1,2,3,4,5,6,7,8,9};//Mat tmp = Mat(vec, 1);//initializer_list<uchar> list = {1,2,3,4,5,6,7,8,9,10,11,12};//Mat tmp = Mat(list);//initializer_list<int> list1 = {3,4};array<uchar,12> arr = { 1,2,3,4,5,6,7,8,9,10,11,12 };Mat tmp = Mat(arr,1);if (tmp.empty())cout << "构建Mat对象失败!" << endl;else{cout << "构建Mat对象成功!" << endl;cout << "构建Mat对象rows:" << tmp.rows << endl;cout << "构建Mat对象cols:" << tmp.cols << endl;}waitKey(0);return 0;
}
试运行,结果如下:
Mat (const Vec< _Tp, n > &vec, bool copyData=true);
vec包含构建Mat对象数据的Vec对象
copyData 是否拷贝数据,true— 拷贝数据,false—不拷贝数据。
修改上面示例代码,来演示该构造函数的用法,修改后的代码如下:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{//vector<uchar> vec = {1,2,3,4,5,6,7,8,9};//Mat tmp = Mat(vec, 1);//initializer_list<uchar> list = {1,2,3,4,5,6,7,8,9,10,11,12};//Mat tmp = Mat(list);//initializer_list<int> list1 = {3,4};//array<uchar,12> arr = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Mat tmp = Mat(arr,1);Vec<uchar,12> vec = { 1,2,3,4,5,6,7,8,9,10,11,12 };Mat tmp = Mat(vec, 1);if (tmp.empty())cout << "构建Mat对象失败!" << endl;else{cout << "构建Mat对象成功!" << endl;cout << "构建Mat对象rows:" << tmp.rows << endl;cout << "构建Mat对象cols:" << tmp.cols << endl;}waitKey(0);return 0;
}
试运行,结果如下:
Mat (const Matx< _Tp, m, n > &mtx, bool copyData=true);
mtx含有构建对象数据的Matx对象
copyData 是否拷贝数据,true— 拷贝数据,false—不拷贝数据。
修改上面示例代码,来演示该构造函数的用法,修改后的代码如下:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{//vector<uchar> vec = {1,2,3,4,5,6,7,8,9};//Mat tmp = Mat(vec, 1);//initializer_list<uchar> list = {1,2,3,4,5,6,7,8,9,10,11,12};//Mat tmp = Mat(list);//initializer_list<int> list1 = {3,4};//array<uchar,12> arr = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Mat tmp = Mat(arr,1);//Vec<uchar,12> vec = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Mat tmp = Mat(vec, 1);Matx<uchar,3,4> mtx = { 1,2,3,4,5,6,7,8,9,10,11,12 };Mat tmp = Mat(mtx, 1);if (tmp.empty())cout << "构建Mat对象失败!" << endl;else{cout << "构建Mat对象成功!" << endl;cout << "构建Mat对象rows:" << tmp.rows << endl;cout << "构建Mat对象cols:" << tmp.cols << endl;}waitKey(0);return 0;
}
试运行,结果如下:
Mat (const Point_< _Tp > &pt, bool copyData=true);
pt 含构建Mat数据的point对象,以point对象的x,y坐标作为Mat对象数据
copyData 是否拷贝数据,true— 拷贝数据,false—不拷贝数据。
修改上面示例代码,来演示该构造函数的用法,修改后的代码如下:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{//vector<uchar> vec = {1,2,3,4,5,6,7,8,9};//Mat tmp = Mat(vec, 1);//initializer_list<uchar> list = {1,2,3,4,5,6,7,8,9,10,11,12};//Mat tmp = Mat(list);//initializer_list<int> list1 = {3,4};//array<uchar,12> arr = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Mat tmp = Mat(arr,1);//Vec<uchar,12> vec = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Mat tmp = Mat(vec, 1);//Matx<uchar,3,4> mtx = { 1,2,3,4,5,6,7,8,9,10,11,12 };Point_<int> point;point.x = 100;point.y = 100;Mat tmp = Mat(point, true);if (tmp.empty())cout << "构建Mat对象失败!" << endl;else{cout << "构建Mat对象成功!" << endl;cout << "构建Mat对象rows:" << tmp.rows << endl;cout << "构建Mat对象cols:" << tmp.cols << endl;}waitKey(0);return 0;
}
试运行,结果如下:
Mat (const Point3_< _Tp > &pt, bool copyData=true)
pt 含构建Mat数据的point对象,以point对象的x,y,z坐标作为Mat对象数据
copyData 是否拷贝数据,true— 拷贝数据,false—不拷贝数据。
修改上面示例代码,来演示该构造函数的用法,修改后的代码如下:
// OpenCVMatTest2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{//vector<uchar> vec = {1,2,3,4,5,6,7,8,9};//Mat tmp = Mat(vec, 1);//initializer_list<uchar> list = {1,2,3,4,5,6,7,8,9,10,11,12};//Mat tmp = Mat(list);//initializer_list<int> list1 = {3,4};//array<uchar,12> arr = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Mat tmp = Mat(arr,1);//Vec<uchar,12> vec = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Mat tmp = Mat(vec, 1);//Matx<uchar,3,4> mtx = { 1,2,3,4,5,6,7,8,9,10,11,12 };//Point_<int> point;Point3_<int> point;point.x = 100;point.y = 100;point.z = 100;Mat tmp = Mat(point, true);if (tmp.empty())cout << "构建Mat对象失败!" << endl;else{cout << "构建Mat对象成功!" << endl;cout << "构建Mat对象rows:" << tmp.rows << endl;cout << "构建Mat对象cols:" << tmp.cols << endl;}waitKey(0);return 0;
}
运行结果如下:
除了用上面的构造函数构建Mat对象外,还可以以下面方式构建并初始化Mat对象:
Mat tmp = (Mat_<uchar>(3,3)<< 1, 0, 0, 0, 1, 0, 0, 0, 1);
Mat类的公共属性
Mat类有一下公共属性:
MatAllocator * allocator
int cols
uchar * data
const uchar * dataend
const uchar * datalimit
const uchar * datastart
int dims
int flags
int rows
MatSize size
MatStep step
UMatData * u
Mat类对象的共公属性可以用.运算符访问.有部分属性已在前面的示例中使用过,这里就不再做介绍了。
本篇 博文示例是基于OpenCV(opencv目录位于d盘根目录下)及VS2022。示例源码已上传到CSDN,其链接为:https://download.csdn.net/download/billliu66/88835162