Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

opencv - How to access pixel values of CV_32F/CV_64F Mat?

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
", 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...