【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];
相关推荐
火山引擎开发者社区2 小时前
Agent Plan、Coding Plan限时优惠:2.5折畅享多模型!
人工智能
冬奇Lab3 小时前
AI Workflow 定义的四次演进:从 Markdown 到 JS 脚本,再到分布式多 Agent
javascript·人工智能·agent
冬奇Lab3 小时前
每日一个开源项目(第136篇):OpenMemory - 给 AI Agent 真正的认知记忆引擎
人工智能
黄啊码4 小时前
【黄啊码】微信 AI 把聊天功能和 Vibe Coding打通了,创业者:我又白干了
人工智能
IT_陈寒4 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
用户413062258295 小时前
给AI回答加引用角标citation:RAG前端实现
人工智能
米小虾6 小时前
WAIC 2026 倒计时30天:300+ AI 产品全球首发,今年看点全解析
人工智能
码上天下6 小时前
多模态Agent上传图片:前端压缩格式与预览实战
人工智能