【opencv】示例-cout_mat.cpp cout输出各种格式矩阵、向量

cpp 复制代码
/*
 * cvout_sample 只是演示了 cv::Mat 的序列化输出能力。
 * 也就是说,现在可以这样使用:cv::Mat M(...); cout << M;。
 */


#include "opencv2/core.hpp" // 包含OpenCV核心功能的头文件
#include <iostream> // 包含标准输入输出流的头文件


// 使用标准和OpenCV命名空间中的名字,避免每次调用时都要加前缀
using namespace std;
using namespace cv;


// 帮助信息的函数
static void help(char** argv)
{
    cout
    << "\n------------------------------------------------------------------\n"
    << " This program shows the serial out capabilities of cv::Mat\n"
    << "That is, cv::Mat M(...); cout << M;  Now works.\n"
    << "Output can be formatted to OpenCV, matlab, python, numpy, csv and \n"
    << "C styles Usage:\n"
    << argv[0]
    << "\n------------------------------------------------------------------\n\n"
    << endl;
}


// 程序的主入口点
int main(int argc, char** argv)
{
    cv::CommandLineParser parser(argc, argv, "{help h||}"); // 创建命令行解析器
    if (parser.has("help")) // 如果用户请求帮助
    {
        help(argv); // 显示帮助信息
        return 0; // 退出程序
    }
    Mat I = Mat::eye(4, 4, CV_64F); // 创建一个4x4的双精度单位矩阵
    I.at<double>(1,1) = CV_PI; // 将第1行第1列的元素设为π
    cout << "I = \n" << I << ";" << endl << endl; // 打印矩阵


    Mat r = Mat(10, 3, CV_8UC3); // 创建一个10x3的8位无符号3通道(彩色)矩阵
    randu(r, Scalar::all(0), Scalar::all(255)); // 使用随机值填充矩阵


    // 以下部分演示不同输出格式
    cout << "r (default) = \n" << r << ";" << endl << endl;
    cout << "r (matlab) = \n" << format(r, Formatter::FMT_MATLAB) << ";" << endl << endl;
    cout << "r (python) = \n" << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;
    cout << "r (numpy) = \n" << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl;
    cout << "r (csv) = \n" << format(r, Formatter::FMT_CSV) << ";" << endl << endl;
    cout << "r (c) = \n" << format(r, Formatter::FMT_C) << ";" << endl << endl;


    Point2f p(5, 1); // 创建一个2D浮点型点
    cout << "p = " << p << ";" << endl; // 打印点


    Point3f p3f(2, 6, 7); // 创建一个3D浮点型点
    cout << "p3f = " << p3f << ";" << endl; // 打印点


    vector<float> v; // 创建一个浮点型向量
    v.push_back(1); // 向向量中添加元素
    v.push_back(2);
    v.push_back(3);


    cout << "shortvec = " << Mat(v) << endl; // 打印向量


    vector<Point2f> points(20); // 创建一个包含20个2D浮点型点的向量
    for (size_t i = 0; i < points.size(); ++i) // 用循环填充这个向量
        points[i] = Point2f((float)(i * 5), (float)(i % 7));


    cout << "points = " << points << ";" << endl; // 打印点的向量
    return 0; // 程序结束
}

这段代码展示了OpenCV库中的cv::Mat类的序列化输出 功能。它包含了一系列可以输出为不同格式的示例,如OpenCV风格、Matlab风格、Python的NumPy风格、CSV风格和C风格。同时,也展示了如何在控制台中显示点和点向量。总的来说,这段代码主要用于教学和演示如何在C++中使用OpenCV的cv::Mat对象以不同的编程语言风格格式化输出。

终端输出:

cs 复制代码
I =
[1, 0, 0, 0;
 0, 3.141592653589793, 0, 0;
 0, 0, 1, 0;
 0, 0, 0, 1];


r (default) =
[ 91,   2,  79, 179,  52, 205, 236,   8, 181;
 239,  26, 248, 207, 218,  45, 183, 158, 101;
 102,  18, 118,  68, 210, 139, 198, 207, 211;
 181, 162, 197, 191, 196,  40,   7, 243, 230;
  45,   6,  48, 173, 242, 125, 175,  90,  63;
  90,  22, 112, 221, 167, 224, 113, 208, 123;
 214,  35, 229,   6, 143, 138,  98,  81, 118;
 187, 167, 140, 218, 178,  23,  43, 133, 154;
 150,  76, 101,   8,  38, 238,  84,  47,   7;
 117, 246, 163, 237,  69, 129,  60, 101,  41];


r (matlab) =
(:, :, 1) =
 91, 179, 236;
239, 207, 183;
102,  68, 198;
181, 191,   7;
 45, 173, 175;
 90, 221, 113;
214,   6,  98;
187, 218,  43;
150,   8,  84;
117, 237,  60
(:, :, 2) =
  2,  52,   8;
 26, 218, 158;
 18, 210, 207;
162, 196, 243;
  6, 242,  90;
 22, 167, 208;
 35, 143,  81;
167, 178, 133;
 76,  38,  47;
246,  69, 101
(:, :, 3) =
 79, 205, 181;
248,  45, 101;
118, 139, 211;
197,  40, 230;
 48, 125,  63;
112, 224, 123;
229, 138, 118;
140,  23, 154;
101, 238,   7;
163, 129,  41;


r (python) =
[[[ 91,   2,  79], [179,  52, 205], [236,   8, 181]],
 [[239,  26, 248], [207, 218,  45], [183, 158, 101]],
 [[102,  18, 118], [ 68, 210, 139], [198, 207, 211]],
 [[181, 162, 197], [191, 196,  40], [  7, 243, 230]],
 [[ 45,   6,  48], [173, 242, 125], [175,  90,  63]],
 [[ 90,  22, 112], [221, 167, 224], [113, 208, 123]],
 [[214,  35, 229], [  6, 143, 138], [ 98,  81, 118]],
 [[187, 167, 140], [218, 178,  23], [ 43, 133, 154]],
 [[150,  76, 101], [  8,  38, 238], [ 84,  47,   7]],
 [[117, 246, 163], [237,  69, 129], [ 60, 101,  41]]];


r (numpy) =
array([[[ 91,   2,  79], [179,  52, 205], [236,   8, 181]],
       [[239,  26, 248], [207, 218,  45], [183, 158, 101]],
       [[102,  18, 118], [ 68, 210, 139], [198, 207, 211]],
       [[181, 162, 197], [191, 196,  40], [  7, 243, 230]],
       [[ 45,   6,  48], [173, 242, 125], [175,  90,  63]],
       [[ 90,  22, 112], [221, 167, 224], [113, 208, 123]],
       [[214,  35, 229], [  6, 143, 138], [ 98,  81, 118]],
       [[187, 167, 140], [218, 178,  23], [ 43, 133, 154]],
       [[150,  76, 101], [  8,  38, 238], [ 84,  47,   7]],
       [[117, 246, 163], [237,  69, 129], [ 60, 101,  41]]], dtype='uint8');


r (csv) =
 91,   2,  79, 179,  52, 205, 236,   8, 181
239,  26, 248, 207, 218,  45, 183, 158, 101
102,  18, 118,  68, 210, 139, 198, 207, 211
181, 162, 197, 191, 196,  40,   7, 243, 230
 45,   6,  48, 173, 242, 125, 175,  90,  63
 90,  22, 112, 221, 167, 224, 113, 208, 123
214,  35, 229,   6, 143, 138,  98,  81, 118
187, 167, 140, 218, 178,  23,  43, 133, 154
150,  76, 101,   8,  38, 238,  84,  47,   7
117, 246, 163, 237,  69, 129,  60, 101,  41
;


r (c) =
{ 91,   2,  79, 179,  52, 205, 236,   8, 181,
 239,  26, 248, 207, 218,  45, 183, 158, 101,
 102,  18, 118,  68, 210, 139, 198, 207, 211,
 181, 162, 197, 191, 196,  40,   7, 243, 230,
  45,   6,  48, 173, 242, 125, 175,  90,  63,
  90,  22, 112, 221, 167, 224, 113, 208, 123,
 214,  35, 229,   6, 143, 138,  98,  81, 118,
 187, 167, 140, 218, 178,  23,  43, 133, 154,
 150,  76, 101,   8,  38, 238,  84,  47,   7,
 117, 246, 163, 237,  69, 129,  60, 101,  41};


p = [5, 1];
p3f = [2, 6, 7];
shortvec = [1;
 2;
 3]
points = [0, 0;
 5, 1;
 10, 2;
 15, 3;
 20, 4;
 25, 5;
 30, 6;
 35, 0;
 40, 1;
 45, 2;
 50, 3;
 55, 4;
 60, 5;
 65, 6;
 70, 0;
 75, 1;
 80, 2;
 85, 3;
 90, 4;
 95, 5];
相关推荐
OceanBase数据库官方博客10 分钟前
OceanBase DataPilot AIP:Ontology 承载AI能力面的另一条路
人工智能·oceanbase
笨鸟先飞,勤能补拙1 小时前
AI 赋能网络安全:技术全景、成熟度评估与实战案例
人工智能·python·安全·web安全·网络安全·sqlite·github
一次旅行1 小时前
AI 前沿日报 | 2026年07月31日
人工智能
2601_963749102 小时前
标题:越华环保集团|面向美丽河湖项目的数字化污水治理云边协同采集架构设计
人工智能
沐籽李2 小时前
从溶剂可及表面积SASA理解抗体结构与工程改造
人工智能·药物设计·aidd·sasa
智慧物业老杨2 小时前
物业如何做好预算管理?落地架构逻辑
人工智能·架构
微学AI2 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
城管不管2 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
boppu2 小时前
布草特殊污渍去渍剂的种类及作用
大数据·人工智能
AIsoft_86882 小时前
会议录音转文字与AI纪要工具推荐:免费额度与核心功能对比指南
人工智能