[英]Print openCV Mat content with certain format in C++
我想問一下如何在C ++中格式化OpenCV Mat並將其打印出來?
例如,當我寫的時候,帶有雙倍內容M的Mat
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;
設置輸出樣式。 所以我正在尋找類似的東西。
非常感謝你!
#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;
}
這應該做的伎倆:
cout.precision(5);
cout << M << endl;
您可能還希望在以下情況下將格式設置為fixed:
cout.setf( std::ios::fixed, std::ios::floatfield );
新版OpenCV讓它變得簡單!
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.