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

相关推荐
Shockang8 小时前
AI 设计工作流全景拆解:Figma MCP / Claude Design / Codex / Google Stitch
人工智能
To_OC9 小时前
数据集划分不是随便切:手把手切分大众点评情感数据集
人工智能·llm·agent
冬奇Lab10 小时前
每日一个开源项目(第142篇):android/skills - Google 官方 Android 开发 AI Skill 库
人工智能·开源·资讯
冬奇Lab10 小时前
Skill 系列(06):Skill 工程化与治理——路由准确率 38%、压缩节省 76%
人工智能·开源·agent
IT_陈寒12 小时前
Vue这个坑我跳了两次,原来问题出在这
前端·人工智能·后端
新新技术迷12 小时前
Node给AI接口做SSE代理与鉴权
人工智能
redreamSo13 小时前
大模型是不是到顶了?瓶颈到底在哪
人工智能·openai
Oo92013 小时前
Tool Use 背后的技术逻辑
人工智能
姗姗来迟了13 小时前
Vue3封装AI流式对话组件踩坑实录
人工智能