Mat OpenCv的赋值错误

assignment error with Mat OpenCv

本文关键字:错误 赋值 OpenCv Mat      更新时间:2023-10-16

我正在使用OpenCV和c++进行一个项目,我发现了以下问题:在用以下语句初始化mat后

Mat or_mat=Mat(img->height,img->width,CV_32FC1);

检查以下值

or_mat.at <float> (i, j) = atan (fy / fx) / 2 +1.5707963;

在完成返回函数输出的垫子之后,但是当我去读取时,有许多值与输出不对应。插入I-4.31602e +008的不正确的精确值,如果我使cout,则表达式的值是正确的。错误是什么?

相关代码:

Mat or_mat=Mat(img->height,img->width,CV_32FC1);
to angle
if(fx > 0){   
    or_mat.at<float>(i,j) = atan(fy/fx)/2+1.5707963;
}
else if(fx<0 && fy >0){   
    or_mat.at<float>(i,j) = atan(fy/fx)/2+3.1415926;
}
else if(fx<0 && fy <0){   
    or_mat.at<float>(i,j) = atan(fy/fx)/2;
}
else if(fy!=0 && fx==0){   
    or_mat.at<float>(i,j) = 1.5707963;
}

我必须计算指纹图像的局部方向,下面的代码我省略了几个没有错误的语句和计算。

我会再三检查你的索引是否正确。下面的代码显示了我初始化一个满是零的矩阵,然后用.at操作符填充它。它可以很好地编译和运行:

int main()
{
    int height = 10;
    int width = 3;
    // Initialise or_mat to with every element set to zero
    cv::Mat or_mat = cv::Mat::zeros(height, width, CV_32FC1);
    std::cout << "Original or_mat:n" << or_mat << std::endl;
    // Loop through and set each element equal to some float
    float value = 10.254;
    for (int i = 0; i < or_mat.rows; ++i)
    {
        for (int j = 0; j < or_mat.cols; ++j)
        {
            or_mat.at<float>(i,j) = value;
        }
    }
    std::cout << "Final or_mat:n" << or_mat << std::endl;
    return 0;
}