Isaac
Isaac

Reputation: 323

opencv undistortPoints returning NaN ios

I have been working with opencv, and I cannot seem to get undistortPoints to work. The matrix it returns has nothing but NaN values.

    //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]);
    }

The value printed is always "nan, nan". I also tried using norm as a std::vector, but that returned the same thing. The values of src, cameraMatrix, and distCo also remain unchanged after the method is called (i tested by printing out their values), so I am confident that I am giving undistortPoints all the correct information. Am I using a cv::Mat incorrectly, using poor form, or is this a bug with opencv. Any insight on what to do from here would be greatly appreciated.

Isaac

Upvotes: 2

Views: 1516

Answers (1)

Max Allan
Max Allan

Reputation: 2395

If you want your matrix to store double precision values you need to declare it with

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

You haven't done this with both the cameraMatrix and distCo matrices. Currently you are trying to access the 32 bit elements of these arrays with a 64 bit accessor.

Upvotes: 2

Related Questions