如何访问CV_32F/CV_64F Mat的像素值? [英] How to access pixel values of CV_32F/CV_64F Mat?

查看:777
本文介绍了如何访问CV_32F/CV_64F Mat的像素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行单应性检查,每当尝试使用H.at<float>(i, j)检查H矩阵(CV_64F类型)的值时,我都会得到随机数(有时是垃圾值).我想访问浮点矩阵的像素值.有什么办法吗?

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

0.000000 1.875000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

为什么会这样?

推荐答案

您应该尝试以下操作:

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

因为您的矩阵是类型" CV_64F,这又意味着它包含类型为double而不是float的元素.

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

顺便说一下,我不确定您是否意识到这一点,但是您可以使用cout这样打印矩阵:

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.

这篇关于如何访问CV_32F/CV_64F Mat的像素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆