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

相关推荐
ifeng091813 分钟前
HarmonyOS实战项目:AI健康助手(影像识别与健康分析)
人工智能·华为·wpf·harmonyos
Aevget14 分钟前
界面控件Telerik UI for WPF 2025 Q3亮点 - 集成AI编码助手
人工智能·ui·wpf·界面控件·ui开发·telerik
ccLianLian18 分钟前
计算机视觉·TagCLIP
人工智能·算法
aneasystone本尊23 分钟前
重温 Java 21 之虚拟线程
人工智能
geneculture24 分钟前
官学商大跨界 · 产学研大综合:融智学新范式应用体系
大数据·人工智能·物联网·数据挖掘·哲学与科学统一性·信息融智学
这张生成的图像能检测吗28 分钟前
(综述)基于深度学习的制造业表面缺陷检测图像合成方法综述
人工智能·计算机视觉·图像生成·工业检测·计算机图像学
草莓熊Lotso29 分钟前
C++ 继承特殊场景解析:友元、静态成员与菱形继承的底层逻辑
服务器·开发语言·c++·人工智能·经验分享·笔记·1024程序员节
安如衫30 分钟前
【学习笔记更新中】Deeplearning.AI 大语言模型后训练:微调与强化学习导论
人工智能·llm·sft·后训练·deepseek
IT_陈寒41 分钟前
5个Python 3.12新特性让你的代码效率提升50%,第3个太实用了!
前端·人工智能·后端
love is sour42 分钟前
理解全连接层:深度学习中的基础构建块
人工智能·深度学习