我一直在使用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]);
}
印刷的价值总是“纳,纳”。我也试过使用norm作为std :: vector,但是返回了相同的东西。调用该方法后,src,cameraMatrix和distCo的值也保持不变(我通过打印出它们的值来测试),所以我确信我正在给undistortPoints提供所有正确的信息。我使用cv :: Mat不正确,使用不良形式,或者这是opencv的错误。任何有关如何从这里做的见解将不胜感激。
艾萨克
答案 0 :(得分:2)
如果您希望矩阵存储双精度值,则需要使用
声明它cv::Mat your_matrix(rows,cols,CV_64FC1);
你没有使用cameraMatrix和distCo矩阵。目前,您正尝试使用64位访问器访问这些数组的32位元素。