Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print out the values of a (Mat) matrix in OpenCV C++

Tags:

c++

opencv

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.

Would a reader please post (or point me to) a little function or code snippet that prints a Mat?

Regards, Aaron

PS: Code that uses the newer C++ Mat interface as opposed to the older CvMat interface is preferential.

like image 677
ahoffer Avatar asked Nov 01 '11 18:11

ahoffer


People also ask

What is mat in C?

Mat is basically a class with two data parts : the matrix header (containing information such as the size of the matrix, the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing the pixel values (taking any dimensionality depending on the method chosen for ...

What is Mat image in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc. This class comprises of two data parts: the header and a pointer.

What is CV_64FC1?

That is, image of type CV_64FC1 is simple grayscale image and has only 1 channel: image[i, j] = 0.5. while image of type CV_64FC3 is colored image with 3 channels: image[i, j] = (0.5, 0.3, 0.7) (in C++ you can check individual pixels as image.at<double>(i, j) ) CV_64F is the same as CV_64FC1 .

What is CV_32F?

CV_32F : 4-byte floating point ( float ).


2 Answers

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

Or better still with the C++ interface:

cv::Mat M; cout << "M = " << endl << " "  << M << endl << endl; 
like image 81
Martin Beckett Avatar answered Sep 21 '22 17:09

Martin Beckett


If you are using opencv3, you can print Mat like 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 here for more information.

enter image description here

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:

#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:

enter image description here

like image 43
Jayhello Avatar answered Sep 24 '22 17:09

Jayhello