Print Matrix Values ​​in OpenCV C ++

I want to dump matrix values ​​in OpenCV to console using cout. I quickly realized that I did not understand a system like OpenvCV and C ++ templates well enough to accomplish this simple task.

Reader, please write (or point me) a small function or piece of code that prints Mat?

Regards, Aaron

PS: Code that uses the new C ++ Mat interface, in contrast to the old CvMat interface, is preferred.

+46
c ++ opencv
Nov 01 '11 at 18:22
source share
3 answers

See the first answer, Adopting a matrix element in "Mat." object (not CvMat object) in OpenCV C ++
Then just move all the elements to cout << M.at<double>(0,0); , not just 0.0

Or even better with the new C ++ interface (thanks to SSteve )

 cv::Mat M; cout << "M = "<< endl << " " << M << endl << endl; 
+68
Nov 01 2018-11-11T00:
source share
 #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; } 
+3
Jun 25 '13 at 15:29
source share

I think using matrix.at<type>(x,y) is not the best way to repeat through a Mat object! If I remember correctly, matrix.at<type>(x,y) will iterate from the beginning of the matrix every time you call it (I could be wrong, though). I would suggest using 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; } 
+3
Feb 18 '14 at 10:14
source share



All Articles