打印的OpenCV垫内容在C某些格式++

问题描述 投票:3回答:3

我想问一下如何在C ++格式化的OpenCV垫,并打印出来?

例如,双内装物M的垫子,当我写

cout<<M<<endl;

我会得到

[-7.7898273846583732e-15, -0.03749374753019832; -0.0374787251930463, -7.7893623846343843e-15]

但我想一个整洁的输出,例如

[0.0000, -0.0374; -0.0374, 0.0000]

是否有任何内置的方式这样做呢?

我知道我们可以用

cout<<format(M,"C")<<endl;

设置输出格式。所以我在寻找类似的措施。

非常感谢你!

c++ opencv
3个回答
3
投票
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <iomanip>

using namespace cv;
using namespace std;

void print(Mat mat, int prec)
{      
    for(int i=0; i<mat.size().height; i++)
    {
        cout << "[";
        for(int j=0; j<mat.size().width; j++)
        {
            cout << setprecision(prec) << mat.at<double>(i,j);
            if(j != mat.size().width-1)
                cout << ", ";
            else
                cout << "]" << endl; 
        }
    }
}

int main(int argc, char** argv)
{
    double data[2][4];
    for(int i=0; i<2; i++)
    {
        for(int j=0; j<4; j++)
        {
            data[i][j] = 0.123456789;
        }
    }
    Mat src = Mat(2, 4, CV_64F, &data);
    print(src, 3);

    return 0;
}

0
投票

这应该做的伎俩:

cout.precision(5);
cout << M << endl;

你也可能要设置格式,以固定前:

cout.setf( std::ios::fixed, std::ios::floatfield );

0
投票

OpenCV的新版本,可以很容易!

看到cv::Formatter

Mat src;
...
cv::Ptr<cv::Formatter> fmt=Formatter::get(cv::Formatter::FMT_DEFAULT);
fmt->set64fPrecision(4);
fmt->set32fPrecision(4);
std::cout << fmt->format(src) << std::endl;
© www.soinside.com 2019 - 2024. All rights reserved.