关于 c :opencv undistortPoints 返回 NaN ios

opencv undistortPoints returning NaN ios

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    //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 \
"
,norm.at<Vec2f>(0,p)[0], norm.at<Vec2f>(0,p)[1]);
    }

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

以撒


如果你想让你的矩阵存储双精度值,你需要用

声明它

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

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