在 OpenCV C++ 中打印出(Mat)矩阵的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7970988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print out the values of a (Mat) matrix in OpenCV C++
提问by ahoffer
I want to dump the values of a matrix in OpenCV to the console using cout. I quickly learned that I do not understand OpenvCV's type system nor C++ templates well enough to accomplish this simple task.
我想使用 cout 将 OpenCV 中矩阵的值转储到控制台。我很快了解到,我对 OpenvCV 的类型系统和 C++ 模板的理解都不够好,无法完成这个简单的任务。
Would a reader please post (or point me to) a little function or code snippet that prints a Mat?
请读者发布(或指向我)一个打印 Mat 的小函数或代码片段?
Regards, Aaron
问候, 亚伦
PS: Code that uses the newer C++ Mat interface as opposed to the older CvMat interface is preferential.
PS:使用较新的 C++ Mat 接口而不是旧的 CvMat 接口的代码是优先的。
回答by Martin Beckett
See the first answer to Accessing a matrix element in the "Mat" object (not the CvMat object) in OpenCV C++
Then just loop over all the elements in cout << M.at<double>(0,0);
rather than just 0,0
请参阅在 OpenCV C++ 中访问“Mat”对象(不是 CvMat 对象)中的矩阵元素的第一个答案
然后循环遍历所有元素cout << M.at<double>(0,0);
而不是 0,0
Or better still with the C++ interface:
或者使用 C++ 接口更好:
cv::Mat M;
cout << "M = " << endl << " " << M << endl << endl;
回答by Dmoonleo
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <iomanip>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
double data[4] = {-0.0000000077898273846583732, -0.03749374753019832, -0.0374787251930463, -0.000000000077893623846343843};
Mat src = Mat(1, 4, CV_64F, &data);
for(int i=0; i<4; i++)
cout << setprecision(3) << src.at<double>(0,i) << endl;
return 0;
}
回答by Dima Maligin
I think using the matrix.at<type>(x,y)
is not the best way to iterate trough a Mat object!
If I recall correctly matrix.at<type>(x,y)
will iterate from the beginning of the matrix each time you call it(I might be wrong though).
I would suggest using cv::MatIterator_
我认为使用matrix.at<type>(x,y)
不是遍历 Mat 对象的最佳方法!如果我matrix.at<type>(x,y)
没记错的话,每次调用它时都会从矩阵的开头进行迭代(不过我可能是错的)。我建议使用cv::MatIterator_
cv::Mat someMat(1, 4, CV_64F, &someData);;
cv::MatIterator_<double> _it = someMat.begin<double>();
for(;_it!=someMat.end<double>(); _it++){
std::cout << *_it << std::endl;
}
回答by Jayhello
If you are using opencv3, you can print Mat like python numpy style
:
如果您使用的是 opencv3,您可以像这样打印 Mat python numpy style
:
Mat xTrainData = (Mat_<float>(5,2) << 1, 1, 1, 1, 2, 2, 2, 2, 2, 2);
cout << "xTrainData (python) = " << endl << format(xTrainData, Formatter::FMT_PYTHON) << endl << endl;
Output as below, you can see it'e more readable, see herefor more information.
输出如下,您可以看到它更具可读性,请参阅此处了解更多信息。
But in most case, there is no need to output all the data in Mat, you can output by row range like 0 ~ 2 row:
但在大多数情况下,不需要输出 Mat 中的所有数据,您可以按行范围输出,如 0 ~ 2 行:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <iomanip>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
//row: 6, column: 3,unsigned one channel
Mat image1(6, 3, CV_8UC1, 5);
// output row: 0 ~ 2
cout << "image1 row: 0~2 = "<< endl << " " << image1.rowRange(0, 2) << endl << endl;
//row: 8, column: 2,unsigned three channel
Mat image2(8, 2, CV_8UC3, Scalar(1, 2, 3));
// output row: 0 ~ 2
cout << "image2 row: 0~2 = "<< endl << " " << image2.rowRange(0, 2) << endl << endl;
return 0;
}
Output as below:
输出如下: