OpenCV Mat 实例详解 二

构造函数

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;
    else
        cout << "构建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;
    else
        cout << "构建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类对象的共公属性可以用.运算符访问.有部分属性已在前面的示例中使用过,这里就不再做介绍了。

本篇 博文示例是基于OpenCV4.8(opencv目录位于d盘根目录下)及VS2022。示例源码已上传到CSDN,其链接为:https://download.csdn.net/download/billliu66/88835162

相关推荐
云卓SKYDROID3 分钟前
无人机视觉跟踪模块技术解析!
人工智能·无人机·科普·高科技·云卓科技
OpenVINO生态社区4 分钟前
人工智能与无人机的组合如何撕开俄空天军的 “核心“
人工智能·无人机
强盛小灵通专卖员11 分钟前
DL00871-基于深度学习YOLOv11的盲人障碍物目标检测含完整数据集
人工智能·深度学习·yolo·目标检测·计算机视觉·无人机·核心期刊
pen-ai15 分钟前
【NLP】 38. Agent
人工智能·自然语言处理
Morpheon21 分钟前
循环神经网络(RNN):从理论到翻译
人工智能·rnn·深度学习·循环神经网络
量子位25 分钟前
6 分钟狂掉 750 亿市值!苹果发布会发啥了…
人工智能·ai编程
Gyoku Mint30 分钟前
机器学习×第五卷:线性回归入门——她不再模仿,而开始试着理解你
人工智能·python·算法·机器学习·pycharm·回归·线性回归
机器之心31 分钟前
刚刚,苹果WWDC掀AI重构风暴!端侧模型全开放、AI版Siri却成最大「鸽」王
人工智能
Blossom.11835 分钟前
基于机器学习的智能故障预测系统:构建与优化
人工智能·python·深度学习·神经网络·机器学习·分类·tensorflow
DisonTangor1 小时前
【字节拥抱开源】字节团队开源视频模型 ContentV: 有限算力下的视频生成模型高效训练
人工智能·开源·aigc