How to access pixel values ​​CV_32F / CV_64F Mat?

I worked on homography and whenever I try to check the values ​​of the matrix H (type CV_64F) using H.at<float>(i, j) , I get random numbers (sometimes a garbage value). I want to access the pixel values ​​of a floating point matrix. Is there any way to do this?

 Mat A = Mat::eye(3, 3, CV_64F); float B; for(int i=0; i<A.rows; i++) { for(int j=0; j<A.cols; j++) { printf("%f\n", A.at<float>(i, j)); } } imshow("identity", A); waitKey(0); 

This shows the correct image of the identification matrix, but when I try to access the pixel values, I get

0.000000 1.875000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

Why is this so?

+8
pixel opencv mat
source share
2 answers

You should try the following:

 A.at<double>(i, j); 

because your matrix has a "type" CV_64F , which in turn means that it contains elements of type double , not float .

By the way, I'm not sure if you know about this, but you can use cout to print such a matrix:

 std::cout << A << std::endl; 

I found this to be useful for checking a small matrix or matrix slice.

+16
source share

In the example below, the Hilbert matrix is ​​initialized:

 Mat H(100, 100, CV_64F); for(int i = 0; i < H.rows; i++) for(int j = 0; j < H.cols; j++) H.at<double>(i,j)=1./(i+j+1); 

Keep in mind that the size identifier used in the at statement cannot be randomly selected. It depends on the image you are trying to get data from. The table below gives a more complete picture of this:

If the matrix is ​​of type CV_8U, then use Mat.at<uchar>(y,x) .

If the matrix is ​​of type CV_8S, use Mat.at<schar>(y,x) .

If the matrix is ​​of type CV_16U, use Mat.at<ushort>(y,x) .

If the matrix is ​​of type CV_16S, use Mat.at<short>(y,x) .

If the matrix is ​​of type CV_32S, use Mat.at<int>(y,x) .

If the matrix is ​​of type CV_32F, use Mat.at<float>(y,x) .

If the matrix is ​​of type CV_64F, use Mat.at<double>(y,x) .

(taken from OpenCV Docs )

+3
source share

All Articles