当前位置:   article > 正文

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

cout 输出矩阵

de82278bd350974a97b06d0ee8064b7f.png

  1. /*
  2. * cvout_sample 只是演示了 cv::Mat 的序列化输出能力。
  3. * 也就是说,现在可以这样使用:cv::Mat M(...); cout << M;。
  4. */
  5. #include "opencv2/core.hpp" // 包含OpenCV核心功能的头文件
  6. #include <iostream> // 包含标准输入输出流的头文件
  7. // 使用标准和OpenCV命名空间中的名字,避免每次调用时都要加前缀
  8. using namespace std;
  9. using namespace cv;
  10. // 帮助信息的函数
  11. static void help(char** argv)
  12. {
  13. cout
  14. << "\n------------------------------------------------------------------\n"
  15. << " This program shows the serial out capabilities of cv::Mat\n"
  16. << "That is, cv::Mat M(...); cout << M; Now works.\n"
  17. << "Output can be formatted to OpenCV, matlab, python, numpy, csv and \n"
  18. << "C styles Usage:\n"
  19. << argv[0]
  20. << "\n------------------------------------------------------------------\n\n"
  21. << endl;
  22. }
  23. // 程序的主入口点
  24. int main(int argc, char** argv)
  25. {
  26. cv::CommandLineParser parser(argc, argv, "{help h||}"); // 创建命令行解析器
  27. if (parser.has("help")) // 如果用户请求帮助
  28. {
  29. help(argv); // 显示帮助信息
  30. return 0; // 退出程序
  31. }
  32. Mat I = Mat::eye(4, 4, CV_64F); // 创建一个4x4的双精度单位矩阵
  33. I.at<double>(1,1) = CV_PI; // 将第1行第1列的元素设为π
  34. cout << "I = \n" << I << ";" << endl << endl; // 打印矩阵
  35. Mat r = Mat(10, 3, CV_8UC3); // 创建一个10x3的8位无符号3通道(彩色)矩阵
  36. randu(r, Scalar::all(0), Scalar::all(255)); // 使用随机值填充矩阵
  37. // 以下部分演示不同输出格式
  38. cout << "r (default) = \n" << r << ";" << endl << endl;
  39. cout << "r (matlab) = \n" << format(r, Formatter::FMT_MATLAB) << ";" << endl << endl;
  40. cout << "r (python) = \n" << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;
  41. cout << "r (numpy) = \n" << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl;
  42. cout << "r (csv) = \n" << format(r, Formatter::FMT_CSV) << ";" << endl << endl;
  43. cout << "r (c) = \n" << format(r, Formatter::FMT_C) << ";" << endl << endl;
  44. Point2f p(5, 1); // 创建一个2D浮点型点
  45. cout << "p = " << p << ";" << endl; // 打印点
  46. Point3f p3f(2, 6, 7); // 创建一个3D浮点型点
  47. cout << "p3f = " << p3f << ";" << endl; // 打印点
  48. vector<float> v; // 创建一个浮点型向量
  49. v.push_back(1); // 向向量中添加元素
  50. v.push_back(2);
  51. v.push_back(3);
  52. cout << "shortvec = " << Mat(v) << endl; // 打印向量
  53. vector<Point2f> points(20); // 创建一个包含20个2D浮点型点的向量
  54. for (size_t i = 0; i < points.size(); ++i) // 用循环填充这个向量
  55. points[i] = Point2f((float)(i * 5), (float)(i % 7));
  56. cout << "points = " << points << ";" << endl; // 打印点的向量
  57. return 0; // 程序结束
  58. }

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

终端输出:

  1. I =
  2. [1, 0, 0, 0;
  3. 0, 3.141592653589793, 0, 0;
  4. 0, 0, 1, 0;
  5. 0, 0, 0, 1];
  6. r (default) =
  7. [ 91, 2, 79, 179, 52, 205, 236, 8, 181;
  8. 239, 26, 248, 207, 218, 45, 183, 158, 101;
  9. 102, 18, 118, 68, 210, 139, 198, 207, 211;
  10. 181, 162, 197, 191, 196, 40, 7, 243, 230;
  11. 45, 6, 48, 173, 242, 125, 175, 90, 63;
  12. 90, 22, 112, 221, 167, 224, 113, 208, 123;
  13. 214, 35, 229, 6, 143, 138, 98, 81, 118;
  14. 187, 167, 140, 218, 178, 23, 43, 133, 154;
  15. 150, 76, 101, 8, 38, 238, 84, 47, 7;
  16. 117, 246, 163, 237, 69, 129, 60, 101, 41];
  17. r (matlab) =
  18. (:, :, 1) =
  19. 91, 179, 236;
  20. 239, 207, 183;
  21. 102, 68, 198;
  22. 181, 191, 7;
  23. 45, 173, 175;
  24. 90, 221, 113;
  25. 214, 6, 98;
  26. 187, 218, 43;
  27. 150, 8, 84;
  28. 117, 237, 60
  29. (:, :, 2) =
  30. 2, 52, 8;
  31. 26, 218, 158;
  32. 18, 210, 207;
  33. 162, 196, 243;
  34. 6, 242, 90;
  35. 22, 167, 208;
  36. 35, 143, 81;
  37. 167, 178, 133;
  38. 76, 38, 47;
  39. 246, 69, 101
  40. (:, :, 3) =
  41. 79, 205, 181;
  42. 248, 45, 101;
  43. 118, 139, 211;
  44. 197, 40, 230;
  45. 48, 125, 63;
  46. 112, 224, 123;
  47. 229, 138, 118;
  48. 140, 23, 154;
  49. 101, 238, 7;
  50. 163, 129, 41;
  51. r (python) =
  52. [[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]],
  53. [[239, 26, 248], [207, 218, 45], [183, 158, 101]],
  54. [[102, 18, 118], [ 68, 210, 139], [198, 207, 211]],
  55. [[181, 162, 197], [191, 196, 40], [ 7, 243, 230]],
  56. [[ 45, 6, 48], [173, 242, 125], [175, 90, 63]],
  57. [[ 90, 22, 112], [221, 167, 224], [113, 208, 123]],
  58. [[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]],
  59. [[187, 167, 140], [218, 178, 23], [ 43, 133, 154]],
  60. [[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]],
  61. [[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]];
  62. r (numpy) =
  63. array([[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]],
  64. [[239, 26, 248], [207, 218, 45], [183, 158, 101]],
  65. [[102, 18, 118], [ 68, 210, 139], [198, 207, 211]],
  66. [[181, 162, 197], [191, 196, 40], [ 7, 243, 230]],
  67. [[ 45, 6, 48], [173, 242, 125], [175, 90, 63]],
  68. [[ 90, 22, 112], [221, 167, 224], [113, 208, 123]],
  69. [[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]],
  70. [[187, 167, 140], [218, 178, 23], [ 43, 133, 154]],
  71. [[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]],
  72. [[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]], dtype='uint8');
  73. r (csv) =
  74. 91, 2, 79, 179, 52, 205, 236, 8, 181
  75. 239, 26, 248, 207, 218, 45, 183, 158, 101
  76. 102, 18, 118, 68, 210, 139, 198, 207, 211
  77. 181, 162, 197, 191, 196, 40, 7, 243, 230
  78. 45, 6, 48, 173, 242, 125, 175, 90, 63
  79. 90, 22, 112, 221, 167, 224, 113, 208, 123
  80. 214, 35, 229, 6, 143, 138, 98, 81, 118
  81. 187, 167, 140, 218, 178, 23, 43, 133, 154
  82. 150, 76, 101, 8, 38, 238, 84, 47, 7
  83. 117, 246, 163, 237, 69, 129, 60, 101, 41
  84. ;
  85. r (c) =
  86. { 91, 2, 79, 179, 52, 205, 236, 8, 181,
  87. 239, 26, 248, 207, 218, 45, 183, 158, 101,
  88. 102, 18, 118, 68, 210, 139, 198, 207, 211,
  89. 181, 162, 197, 191, 196, 40, 7, 243, 230,
  90. 45, 6, 48, 173, 242, 125, 175, 90, 63,
  91. 90, 22, 112, 221, 167, 224, 113, 208, 123,
  92. 214, 35, 229, 6, 143, 138, 98, 81, 118,
  93. 187, 167, 140, 218, 178, 23, 43, 133, 154,
  94. 150, 76, 101, 8, 38, 238, 84, 47, 7,
  95. 117, 246, 163, 237, 69, 129, 60, 101, 41};
  96. p = [5, 1];
  97. p3f = [2, 6, 7];
  98. shortvec = [1;
  99. 2;
  100. 3]
  101. points = [0, 0;
  102. 5, 1;
  103. 10, 2;
  104. 15, 3;
  105. 20, 4;
  106. 25, 5;
  107. 30, 6;
  108. 35, 0;
  109. 40, 1;
  110. 45, 2;
  111. 50, 3;
  112. 55, 4;
  113. 60, 5;
  114. 65, 6;
  115. 70, 0;
  116. 75, 1;
  117. 80, 2;
  118. 85, 3;
  119. 90, 4;
  120. 95, 5];

dcd4dd2b091614e567df1739d04bba1d.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/886793
推荐阅读
相关标签
  

闽ICP备14008679号