2013-03-12 184 views
2

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

//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的值也保持不变,所以我确信我正给予unistortPoints所有正确的信息。我是否使用cv :: Mat错误地使用了差的表单,或者这是opencv的错误。任何有关在这里做什么的洞察力将不胜感激。

艾萨克

回答

2

如果你希望你的矩阵存储你需要

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

您还没有与cameraMatrix和配电公司矩阵都做到了这一点宣布它的双精度值。目前,您正尝试使用64位访问器访问这些阵列的32位元素。

+0

非常感谢你,你是男人!!!!! – Isaac 2013-03-13 14:57:21