cv::Mat matrix, HOW TO Reduce digits to the right of the decimal point in cv::Mat?
Asked Answered
E

2

6

I have an app that prints a 3x3 cv::Mat on the iPhone screen. I need to reduce the decimals, as the screen is not so big, see:

[1.004596557012473, -0.003116992336797859, 5.936915104939593; -0.007241746117066327, 0.9973985665720294, -0.2118670500989478; 1.477734234970711e-05, -1.03363734495053e-05, 1.000089074805124]

so I would like to reduce the decimals .4 or .6 or six decimals. Any ideas?

Cheers

Escamilla answered 7/6, 2013 at 20:23 Comment(0)
H
4

If you were using printf

cv::Mat data(3, 3, CV_64FC1);
for (int y = 0; y < data.rows; ++y) {
  for (int x = 0;x < data.cols; ++x) {
    printf("%.6f ", data.at<double>(y, x));
  }
}

If you were using std::cout

cv::Mat data(3, 3, CV_64FC1);
std::cout.setf(std::ios::fixed, std:: ios::floatfield);
std::cout.precision(6);
for (int y = 0; y < data.rows; ++y) {
  for (int x = 0;x < data.cols; ++x) {
    std::cout<<data.at<double>(y, x)<<" ";
  }
}
Halsy answered 7/6, 2013 at 20:39 Comment(0)
S
9

direct OpenCV version

select the base formatter as you wish, according to
http://docs.opencv.org/3.1.0/d3/da1/classcv_1_1Formatter.html
then adapt the function set64fPrecision (or set32fPrecision)
and then cout:

 cv::Ptr<cv::Formatter> formatMat=Formatter::get(cv::Formatter::FMT_DEFAULT);
 formatMat->set64fPrecision(3);
 formatMat->set32fPrecision(3);
 std::cout << "matrix:" << std::endl << formatMat->format( sim ) << std::endl;
Shalon answered 18/11, 2016 at 8:58 Comment(0)
H
4

If you were using printf

cv::Mat data(3, 3, CV_64FC1);
for (int y = 0; y < data.rows; ++y) {
  for (int x = 0;x < data.cols; ++x) {
    printf("%.6f ", data.at<double>(y, x));
  }
}

If you were using std::cout

cv::Mat data(3, 3, CV_64FC1);
std::cout.setf(std::ios::fixed, std:: ios::floatfield);
std::cout.precision(6);
for (int y = 0; y < data.rows; ++y) {
  for (int x = 0;x < data.cols; ++x) {
    std::cout<<data.at<double>(y, x)<<" ";
  }
}
Halsy answered 7/6, 2013 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.