gpt4 book ai didi

c++ - opencv undistortPoints 返回 NaN ios

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:18 25 4
gpt4 key购买 nike

我一直在使用 opencv,但我似乎无法让 undistortPoints 工作。它返回的矩阵只有 NaN 值。

    //newKeyPoints is std::vector<cv::KeyPoint>, and it's values are valid
cv::Mat src = cv::Mat(1,newKeyPoints.size(),CV_32FC2);
int i = 0;
for (std::vector<cv::KeyPoint>::iterator it = newKeyPoints.begin(); it != newKeyPoints.end(); it++){
src.at<cv::Vec2f>(0,i)[0] = (*it).pt.x;
src.at<cv::Vec2f>(0,i)[1] = (*it).pt.y;
i++;
}

cv::Mat norm = cv::Mat(1,newKeyPoints.size(),CV_32FC2);

//Note: fx, fy, cx, cy... k3 are all global constants declared when initialized
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_32F);
cameraMatrix.at<double>(0,0) = fx; //double fx = 354.65
cameraMatrix.at<double>(1,0) = 0;
cameraMatrix.at<double>(2,0) = 0;
cameraMatrix.at<double>(0,1) = 0;
cameraMatrix.at<double>(1,1) = fy; //double fy = 355.66
cameraMatrix.at<double>(2,1) = 0;
cameraMatrix.at<double>(0,2) = cx; //double cx = 143.2
cameraMatrix.at<double>(1,2) = cy; //double cy = 173.6
cameraMatrix.at<double>(2,2) = 1;

cv::Mat distCo = cv::Mat(1, 5, CV_32F);
distCo.at<double>(0,0) = k1; //double k1 = .005
distCo.at<double>(0,1) = k2; //double k2 = .002
distCo.at<double>(0,2) = p1; //double p1 = -.009
distCo.at<double>(0,3) = p2; //double p2 = -.008
distCo.at<double>(0,4) = k3; //double k3 = -.03

cv::undistortPoints(src, norm, cameraMatrix, distCo);

for (int p = 0; p<newKeyPoints.size(); p++){
printf("%f, %f \n",norm.at<Vec2f>(0,p)[0], norm.at<Vec2f>(0,p)[1]);
}

打印的值总是“nan, nan”。我也尝试使用 norm 作为 std::vector,但返回的是同样的东西。调用该方法后,src、cameraMatrix 和 distCo 的值也保持不变(我通过打印出它们的值进行测试),因此我确信我为 undistortPoints 提供了所有正确的信息。我是否错误地使用了 cv::Mat,使用了糟糕的形式,或者这是 opencv 的错误。任何关于从这里做什么的见解都将不胜感激。

艾萨克

最佳答案

如果你想让你的矩阵存储 double 值,你需要声明它

cv::Mat your_matrix(rows,cols,CV_64FC1);

您还没有对 cameraMatrix 和 distCo 矩阵执行此操作。目前您正在尝试使用 64 位访问器访问这些数组的 32 位元素。

关于c++ - opencv undistortPoints 返回 NaN ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com