【问题标题】:Print out the values of a (Mat) matrix in OpenCV C++在 OpenCV C++ 中打印出 (Mat) 矩阵的值
【发布时间】:2011-12-19 17:40:47
【问题描述】:

我想使用 cout 将 OpenCV 中矩阵的值转储到控制台。我很快了解到我对 OpenvCV 的类型系统和 C++ 模板的了解不足以完成这个简单的任务。

请读者发布(或指向我)一个打印 Mat 的小函数或代码 sn-p 吗?

问候, 亚伦

PS:优先使用较新的 C++ Mat 接口而不是较旧的 CvMat 接口的代码。

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    查看Accessing a matrix element in the "Mat" object (not the CvMat object) in OpenCV C++的第一个答案
    然后循环遍历cout << M.at<double>(0,0); 中的所有元素,而不仅仅是 0,0

    或者使用 C++ 接口更好:

    cv::Mat M;
    cout << "M = " << endl << " "  << M << endl << endl;
    

    【讨论】:

    • 太棒了。我应该首先推一个垫子来cout,看看是否有人实施了
    • 我刚刚问过a question about this。 ostream 运算符
    • @ahoffer,有趣的是你提到:docs.opencv.org/4.1.0/dc/d84/…
    • 要补充一点:确保包含#include &lt;opencv2/core.hpp&gt; 而不是#include &lt;opencv2/core/mat.hpp&gt;,因为运算符在其他地方超载。
    【解决方案2】:
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    #include <iostream>
    #include <iomanip>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char** argv)
    {
        double data[4] = {-0.0000000077898273846583732, -0.03749374753019832, -0.0374787251930463, -0.000000000077893623846343843};
        Mat src = Mat(1, 4, CV_64F, &data);
        for(int i=0; i<4; i++)
            cout << setprecision(3) << src.at<double>(0,i) << endl;
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      我认为使用 matrix.at&lt;type&gt;(x,y) 并不是遍历 Mat 对象的最佳方式! 如果我没记错的话,matrix.at&lt;type&gt;(x,y) 每次调用它时都会从矩阵的开头进行迭代(不过我可能是错的)。 我建议使用cv::MatIterator_

      cv::Mat someMat(1, 4, CV_64F, &someData);;
      cv::MatIterator_<double> _it = someMat.begin<double>();
      for(;_it!=someMat.end<double>(); _it++){
          std::cout << *_it << std::endl;
      }
      

      【讨论】:

        【解决方案4】:

        如果你使用的是opencv3,你可以像python numpy style一样打印Mat:

        Mat xTrainData = (Mat_<float>(5,2) << 1, 1, 1, 1, 2, 2, 2, 2, 2, 2);
        
        cout << "xTrainData (python)  = " << endl << format(xTrainData, Formatter::FMT_PYTHON) << endl << endl;
        

        输出如下,您可以看到它更具可读性,请参阅here了解更多信息。

        但大多数情况下,不需要输出Mat中的所有数据,可以按行范围输出,如0~2行:

        #include <opencv2/imgproc/imgproc.hpp>
        #include <opencv2/highgui/highgui.hpp>
        
        #include <iostream>
        #include <iomanip>
        
        using namespace cv;
        using namespace std;
        
        int main(int argc, char** argv)
        {
            //row: 6, column: 3,unsigned one channel
            Mat image1(6, 3, CV_8UC1, 5);
        
            // output row: 0 ~ 2
            cout << "image1 row: 0~2 = "<< endl << " "  << image1.rowRange(0, 2) << endl << endl;
        
            //row: 8, column: 2,unsigned three channel
            Mat image2(8, 2, CV_8UC3, Scalar(1, 2, 3));
        
            // output row: 0 ~ 2
            cout << "image2 row: 0~2 = "<< endl << " "  << image2.rowRange(0, 2) << endl << endl;
        
            return 0;
        }
        

        输出如下:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-12
          • 1970-01-01
          相关资源
          最近更新 更多