diff --git a/components/rgbd-sources/src/calibrate.cpp b/components/rgbd-sources/src/calibrate.cpp index 35b2988a2bd5f87f024a3c5ad0d0bdda92c60d9c..c5a96c862b2c35a3f1d97ac2fdad94c800c5b955 100644 --- a/components/rgbd-sources/src/calibrate.cpp +++ b/components/rgbd-sources/src/calibrate.cpp @@ -521,52 +521,52 @@ static double computeReprojectionErrors( const vector<Mat>& rvecs, const vector<Mat>& tvecs, const Mat& cameraMatrix , const Mat& distCoeffs, vector<float>& perViewErrors, bool fisheye) { - vector<Point2f> imagePoints2; - size_t totalPoints = 0; - double totalErr = 0; - perViewErrors.resize(objectPoints.size()); - - for (size_t i = 0; i < objectPoints.size(); ++i) { - if (fisheye) { - cv::fisheye::projectPoints(objectPoints[i], imagePoints2, rvecs[i], - tvecs[i], cameraMatrix, distCoeffs); - } else { - projectPoints(objectPoints[i], rvecs[i], tvecs[i], cameraMatrix, - distCoeffs, imagePoints2); - } - double err = norm(imagePoints[i], imagePoints2, NORM_L2); - - size_t n = objectPoints[i].size(); - perViewErrors[i] = static_cast<float>(std::sqrt(err*err/n)); - totalErr += err*err; - totalPoints += n; - } + vector<Point2f> imagePoints2; + size_t totalPoints = 0; + double totalErr = 0; + perViewErrors.resize(objectPoints.size()); + + for (size_t i = 0; i < objectPoints.size(); ++i) { + if (fisheye) { + cv::fisheye::projectPoints(objectPoints[i], imagePoints2, rvecs[i], + tvecs[i], cameraMatrix, distCoeffs); + } else { + projectPoints(objectPoints[i], rvecs[i], tvecs[i], cameraMatrix, + distCoeffs, imagePoints2); + } + double err = norm(imagePoints[i], imagePoints2, NORM_L2); - return std::sqrt(totalErr/totalPoints); + size_t n = objectPoints[i].size(); + perViewErrors[i] = static_cast<float>(std::sqrt(err*err/n)); + totalErr += err*err; + totalPoints += n; + } + + return std::sqrt(totalErr/totalPoints); } //! [compute_errors] //! [board_corners] static void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f>& corners, Calibrate::Settings::Pattern patternType) { - corners.clear(); - - switch (patternType) { - case Calibrate::Settings::CHESSBOARD: - case Calibrate::Settings::CIRCLES_GRID: - for (int i = 0; i < boardSize.height; ++i) - for (int j = 0; j < boardSize.width; ++j) - corners.push_back(Point3f(j*squareSize, i*squareSize, 0)); - break; - - case Calibrate::Settings::ASYMMETRIC_CIRCLES_GRID: - for (int i = 0; i < boardSize.height; i++) - for (int j = 0; j < boardSize.width; j++) - corners.push_back(Point3f((2*j + i % 2)*squareSize, - i*squareSize, 0)); - break; - default: - break; - } + corners.clear(); + + switch (patternType) { + case Calibrate::Settings::CHESSBOARD: + case Calibrate::Settings::CIRCLES_GRID: + for (int i = 0; i < boardSize.height; ++i) + for (int j = 0; j < boardSize.width; ++j) + corners.push_back(Point3f(j*squareSize, i*squareSize, 0)); + break; + + case Calibrate::Settings::ASYMMETRIC_CIRCLES_GRID: + for (int i = 0; i < boardSize.height; i++) + for (int j = 0; j < boardSize.width; j++) + corners.push_back(Point3f((2*j + i % 2)*squareSize, + i*squareSize, 0)); + break; + default: + break; + } } //! [board_corners] static bool _runCalibration(const Calibrate::Settings& s, Size& imageSize, @@ -574,55 +574,55 @@ static bool _runCalibration(const Calibrate::Settings& s, Size& imageSize, vector<vector<Point2f> > imagePoints, vector<Mat>& rvecs, vector<Mat>& tvecs, vector<float>& reprojErrs, double& totalAvgErr, vector<Point3f>& newObjPoints, float grid_width, bool release_object) { - cameraMatrix = Mat::eye(3, 3, CV_64F); - if (s.flag & CALIB_FIX_ASPECT_RATIO) - cameraMatrix.at<double>(0, 0) = s.aspectRatio; + cameraMatrix = Mat::eye(3, 3, CV_64F); + if (s.flag & CALIB_FIX_ASPECT_RATIO) + cameraMatrix.at<double>(0, 0) = s.aspectRatio; - if (s.useFisheye) { - distCoeffs = Mat::zeros(4, 1, CV_64F); - } else { - distCoeffs = Mat::zeros(8, 1, CV_64F); - } + if (s.useFisheye) { + distCoeffs = Mat::zeros(4, 1, CV_64F); + } else { + distCoeffs = Mat::zeros(8, 1, CV_64F); + } - vector<vector<Point3f> > objectPoints(1); - calcBoardCornerPositions(s.boardSize, s.squareSize, objectPoints[0], - Calibrate::Settings::CHESSBOARD); - objectPoints[0][s.boardSize.width - 1].x = objectPoints[0][0].x + - grid_width; - newObjPoints = objectPoints[0]; - - objectPoints.resize(imagePoints.size(), objectPoints[0]); - - // Find intrinsic and extrinsic camera parameters - double rms; - - if (s.useFisheye) { - Mat _rvecs, _tvecs; - rms = cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, - cameraMatrix, distCoeffs, _rvecs, _tvecs, s.flag); - - rvecs.reserve(_rvecs.rows); - tvecs.reserve(_tvecs.rows); - for (int i = 0; i < static_cast<int>(objectPoints.size()); i++) { - rvecs.push_back(_rvecs.row(i)); - tvecs.push_back(_tvecs.row(i)); - } - } else { - rms = calibrateCamera(objectPoints, imagePoints, imageSize, - cameraMatrix, distCoeffs, rvecs, tvecs, - s.flag | CALIB_USE_LU); - } + vector<vector<Point3f> > objectPoints(1); + calcBoardCornerPositions(s.boardSize, s.squareSize, objectPoints[0], + Calibrate::Settings::CHESSBOARD); + objectPoints[0][s.boardSize.width - 1].x = objectPoints[0][0].x + + grid_width; + newObjPoints = objectPoints[0]; + + objectPoints.resize(imagePoints.size(), objectPoints[0]); + + // Find intrinsic and extrinsic camera parameters + double rms; + + if (s.useFisheye) { + Mat _rvecs, _tvecs; + rms = cv::fisheye::calibrate(objectPoints, imagePoints, imageSize, + cameraMatrix, distCoeffs, _rvecs, _tvecs, s.flag); - LOG(INFO) << "Re-projection error reported by calibrateCamera: "<< rms; + rvecs.reserve(_rvecs.rows); + tvecs.reserve(_tvecs.rows); + for (int i = 0; i < static_cast<int>(objectPoints.size()); i++) { + rvecs.push_back(_rvecs.row(i)); + tvecs.push_back(_tvecs.row(i)); + } + } else { + rms = calibrateCamera(objectPoints, imagePoints, imageSize, + cameraMatrix, distCoeffs, rvecs, tvecs, + s.flag | CALIB_USE_LU); + } - bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs); + LOG(INFO) << "Re-projection error reported by calibrateCamera: "<< rms; - objectPoints.clear(); - objectPoints.resize(imagePoints.size(), newObjPoints); - totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints, rvecs, - tvecs, cameraMatrix, distCoeffs, reprojErrs, s.useFisheye); + bool ok = checkRange(cameraMatrix) && checkRange(distCoeffs); - return ok; + objectPoints.clear(); + objectPoints.resize(imagePoints.size(), newObjPoints); + totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints, rvecs, + tvecs, cameraMatrix, distCoeffs, reprojErrs, s.useFisheye); + + return ok; } //! [run_and_save] @@ -630,17 +630,17 @@ bool runCalibration(const Calibrate::Settings& s, Size imageSize, Mat& cameraMatrix, Mat& distCoeffs, vector<vector<Point2f> > imagePoints, float grid_width, bool release_object) { - vector<Mat> rvecs, tvecs; - vector<float> reprojErrs; - double totalAvgErr = 0; - vector<Point3f> newObjPoints; - - bool ok = _runCalibration(s, imageSize, cameraMatrix, distCoeffs, - imagePoints, rvecs, tvecs, reprojErrs, totalAvgErr, newObjPoints, - grid_width, release_object); - LOG(INFO) << (ok ? "Calibration succeeded" : "Calibration failed") - << ". avg re projection error = " << totalAvgErr; - - return ok; + vector<Mat> rvecs, tvecs; + vector<float> reprojErrs; + double totalAvgErr = 0; + vector<Point3f> newObjPoints; + + bool ok = _runCalibration(s, imageSize, cameraMatrix, distCoeffs, + imagePoints, rvecs, tvecs, reprojErrs, totalAvgErr, newObjPoints, + grid_width, release_object); + LOG(INFO) << (ok ? "Calibration succeeded" : "Calibration failed") + << ". avg re projection error = " << totalAvgErr; + + return ok; } -//! [run_and_save] +//! [run_and_save] \ No newline at end of file