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

依然范特西╮ 提交于 2019-12-04 07:42:50

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)<<" ";
  }
}

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!