如何访问CV_32F/CV_64F Mat的像素值? [英] How to access pixel values of 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屋!