Print out the values of a (Mat) matrix in OpenCV C++
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;
Print openCV Mat content with certain format in C++
#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;
}
Printing a value of Mat object
ptr accepts as argument the row number. You can thus get the pointer to the starting element of each row by calling pointer on each i
(outside inner loop).
Then you need %d
(or %u
) in your printf
(or you can use cout
). Check the code below:
#include <opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
int N = 10;
// Init matrix
Mat destination(N, N, CV_8UC1);
randu(destination, Scalar(0), Scalar(255));
Size s = destination.size();
int count = 0;
for (int i = 0; i<s.height; i++)
{
unsigned char* pByte = destination.ptr<unsigned char>(i);
for (int j = 0; j<s.width; j++) {
count++;
printf("value %d ", pByte[j]);
printf("\n");
//cout << "value " << int(pByte[j]) << endl;
}
}
return(0);
}
You can see in my other answer other methods to get the value at specific position in a Mat
.
Printing CV_32F opencv matrix values
Close, the loop needs to start at 0!
std::cout<<O.at<float>(0,i)<<" ";
Print multidimensional Mat in OpenCV (C++)
To print n-dim matrix you could use Matrix slice. Since 2d matrices are stored row by row, 3d matrices plane by plane and so on, you could use code:
cv::Mat sliceMat(cv::Mat L,int dim,std::vector<int> _sz)
{
cv::Mat M(L.dims - 1, std::vector<int>(_sz.begin() + 1, _sz.end()).data(), CV_8UC1, L.data + L.step[0] * 0);
return M;
}
To perform mat slice.For more dimensions you should make more slices. Example shows 3 and 4 dimension matrices:
std::cout << "3 dimensions" << std::endl;
std::vector<int> sz = { 3,3,3 };
cv::Mat L;
L.create(3, sz.data(), CV_8UC1);
L = cv::Scalar(255);
std::cout<< sliceMat(L, 1, sz);
std::cout << std::endl;
std::cout <<"4 dimensions"<< std::endl;
sz = { 5,4,3,5 };
L.create(4, sz.data(), CV_8UC1);
L = cv::Scalar(255);
std::cout << sliceMat(sliceMat(L, 1, sz),2, std::vector<int>(sz.begin() + 1, sz.end()));
end result screen
Get value from OpenCV Mat
When you initialize a Mat with 32FC1 you allocate cells of 32bits, which are represented by float
in C. In order to work with a matrix of double
which are 64 bit floating point values, you need to use 64FC1.
So, either change the matrix to doubles, or use at<float>
Printing out Matrix values in OPENCV does not work
It is printing a value that looks bad, but it is actually correct.
The type Vec3b is a vector with 3 byte entries, where those bytes are stored as unsigned char values to represent the 0 -> 255 color range. You are seeing the byte value of 205 in the console.
The Vec3b object most likely has the << overridden to deal with printing out the values for the user to read.
Try type casting the byte to an int to see the value:
cout << (int)image.at<Vec3b>(1, 1)[0] << std::endl;
Related Topics
Calling Function Template Specialization Using C Calling Conventions
Assigning a String of Characters to a Char Array
Undefined Reference to Winmain@16 C++, Sdl-2
Portability of Native C++ Properties
Copy Constructor in C++ Is Called When Object Is Returned from a Function
Pointers to Elements of Std::Vector and Std::List
Redefining or Changing MACro Value
Convert a Number to a String Literal with Constexpr
Macro to Replace C++ Operator New
How to Install Tensorflow on Windows
How to Use a Lambda Expression as a Template Parameter
Opencv - Dll Missing, But It's Not
Std::Make_Tuple Doesn't Make References
Std::Unordered_Map::Find Using a Type Different Than the Key Type