4

I would like to ask how to format a OpenCV Mat in C++ and print it out?

For example, a Mat with double content M, when I wrote

cout<<M<<endl;

I will get

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

But I want a neat output, for example

[0.0000, -0.0374; -0.0374, 0.0000]

Is there any built-in way to do so?

I know we can use

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

to set the output styple. So I am looking for something similar to this.

Thank you very much!

PaulYang
  • 330
  • 3
  • 9
  • 1
    Exact duplicate of : http://stackoverflow.com/questions/16992370/cvmat-matrix-how-to-reduce-digits-to-the-right-of-the-decimal-point-in-cvma – Aurelius Jun 25 '13 at 15:00
  • Thanks! I searched a few minutes but did not find any related question so I posted. It was a problem of different choices of keywords. – PaulYang Jun 25 '13 at 15:59
  • Possible duplicate of [cv::Mat matrix, HOW TO Reduce digits to the right of the decimal point in cv::Mat?](https://stackoverflow.com/questions/16992370/cvmat-matrix-how-to-reduce-digits-to-the-right-of-the-decimal-point-in-cvma) – Alessandro Jacopson Feb 28 '18 at 10:08

3 Answers3

5

new versions of OpenCV make it easy!

see cv::Formatter

Mat src;
...
cv::Ptr<cv::Formatter> fmt = cv::Formatter::get(cv::Formatter::FMT_DEFAULT);
fmt->set64fPrecision(4);
fmt->set32fPrecision(4);
std::cout << fmt->format(src) << std::endl;
abyesilyurt
  • 399
  • 3
  • 13
ma.mehralian
  • 1,274
  • 2
  • 13
  • 29
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;
}
Dmoonleo
  • 76
  • 2
0

This should do the trick:

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

You may also want to set the format to fixed before:

cout.setf( std::ios::fixed, std::ios::floatfield );
Marius
  • 2,234
  • 16
  • 18
  • Thank you! I added this setf command first, and then precision command second. The format did changed (now -7.7898273846583732e-15 becomes 0.00000000000000). But it is still too long. – PaulYang Jun 25 '13 at 08:32
  • 2
    Then the implementation of the printing-method of the matrix overwrites the precision. – Marius Jun 25 '13 at 08:40
  • 1
    @Marius To set the precision with the builtin opencv `mat::operator<<` see https://stackoverflow.com/a/40672924/15485 – Alessandro Jacopson Feb 28 '18 at 10:08