2

我一直在使用 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 的错误。任何有关从这里做什么的见解将不胜感激。

艾萨克

4

1 回答 1

2

如果您希望矩阵存储双精度值,则需要使用

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

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

于 2013-03-13T14:29:43.477 回答