[英]Print openCV Mat content with certain format in C++
I would like to ask how to format a OpenCV Mat in C++ and print it out? 我想问一下如何在C ++中格式化OpenCV Mat并将其打印出来?
For example, a Mat with double content M, when I wrote 例如,当我写的时候,带有双倍内容M的Mat
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! 非常感谢你!
#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;
}
This should do the trick: 这应该做的伎俩:
cout.precision(5);
cout << M << endl;
You may also want to set the format to fixed before: 您可能还希望在以下情况下将格式设置为fixed:
cout.setf( std::ios::fixed, std::ios::floatfield );
new versions of OpenCV make it easy! 新版OpenCV让它变得简单!
see cv::Formatter 看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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.