How to access pixel values of CV_32F/CV_64F Mat?

梦想的初衷 提交于 2019-11-26 21:36:34

问题


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

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 correct image of an identity matrix but while trying to access 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?


回答1:


You should try this:

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

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

By the way, I'm not sure whether you are aware of this, but you can use cout to print the matrix like so:

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

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




回答2:


The example below initializes a Hilbert matrix:

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 operator cannot be chosen at random. It depends on the image from which you are trying to retrieve the data. The table below gives a better insight in this:

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

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

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

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

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

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

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

(Taken from OpenCV docs)



来源:https://stackoverflow.com/questions/15130162/how-to-access-pixel-values-of-cv-32f-cv-64f-mat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!