diff --git a/modules/calib3d/include/opencv2/calib3d.hpp b/modules/calib3d/include/opencv2/calib3d.hpp index f44dacbedf..3dec8ed31b 100644 --- a/modules/calib3d/include/opencv2/calib3d.hpp +++ b/modules/calib3d/include/opencv2/calib3d.hpp @@ -4098,6 +4098,53 @@ optimization. It is the \f$max(width,height)/\pi\f$ or the provided \f$f_x\f$, \ TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 10, 1e-8) ); + /** + @brief Finds an object pose from 3D-2D point correspondences using the RANSAC scheme for fisheye camera moodel. + + @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or + 1xN/Nx1 3-channel, where N is the number of points. vector\ can be also passed here. + @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, + where N is the number of points. vector\ can be also passed here. + @param cameraMatrix Input camera intrinsic matrix \f$\cameramatrix{A}\f$ . + @param distCoeffs Input vector of distortion coefficients (4x1/1x4). + @param rvec Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from + the model coordinate system to the camera coordinate system. + @param tvec Output translation vector. + @param useExtrinsicGuess Parameter used for #SOLVEPNP_ITERATIVE. If true (1), the function uses + the provided rvec and tvec values as initial approximations of the rotation and translation + vectors, respectively, and further optimizes them. + @param iterationsCount Number of iterations. + @param reprojectionError Inlier threshold value used by the RANSAC procedure. The parameter value + is the maximum allowed distance between the observed and computed point projections to consider it + an inlier. + @param confidence The probability that the algorithm produces a useful result. + @param inliers Output vector that contains indices of inliers in objectPoints and imagePoints . + @param flags Method for solving a PnP problem: see @ref calib3d_solvePnP_flags + This function returns the rotation and the translation vectors that transform a 3D point expressed in the object + coordinate frame to the camera coordinate frame, using different methods: + - P3P methods (@ref SOLVEPNP_P3P, @ref SOLVEPNP_AP3P): need 4 input points to return a unique solution. + - @ref SOLVEPNP_IPPE Input points must be >= 4 and object points must be coplanar. + - @ref SOLVEPNP_IPPE_SQUARE Special case suitable for marker pose estimation. + Number of input points must be 4. Object points must be defined in the following order: + - point 0: [-squareLength / 2, squareLength / 2, 0] + - point 1: [ squareLength / 2, squareLength / 2, 0] + - point 2: [ squareLength / 2, -squareLength / 2, 0] + - point 3: [-squareLength / 2, -squareLength / 2, 0] + - for all the other flags, number of input points must be >= 4 and object points can be in any configuration. + @param criteria Termination criteria for internal undistortPoints call. + The function interally undistorts points with @ref undistortPoints and call @ref cv::solvePnP, + thus the input are very similar. More information about Perspective-n-Points is described in @ref calib3d_solvePnP + for more information. + */ + CV_EXPORTS_W bool solvePnPRansac( InputArray objectPoints, InputArray imagePoints, + InputArray cameraMatrix, InputArray distCoeffs, + OutputArray rvec, OutputArray tvec, + bool useExtrinsicGuess = false, int iterationsCount = 100, + float reprojectionError = 8.0, double confidence = 0.99, + OutputArray inliers = noArray(),int flags = SOLVEPNP_ITERATIVE, + TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 10, 1e-8) + ); + //! @} calib3d_fisheye } // end namespace fisheye diff --git a/modules/calib3d/src/fisheye.cpp b/modules/calib3d/src/fisheye.cpp index d1bff24faf..5a30087b3f 100644 --- a/modules/calib3d/src/fisheye.cpp +++ b/modules/calib3d/src/fisheye.cpp @@ -1120,6 +1120,22 @@ bool cv::fisheye::solvePnP( InputArray opoints, InputArray ipoints, return cv::solvePnP(opoints, imagePointsNormalized, cameraMatrix, noArray(), rvec, tvec, useExtrinsicGuess, flags); } +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/// cv::fisheye::solvePnPRansac + +bool cv::fisheye::solvePnPRansac( InputArray opoints, InputArray ipoints, + InputArray cameraMatrix, InputArray distCoeffs, + OutputArray rvec, OutputArray tvec, bool useExtrinsicGuess, + int iterationsCount, float reprojectionError, + double confidence, OutputArray inliers, + int flags, TermCriteria criteria) +{ + Mat imagePointsNormalized; + cv::fisheye::undistortPoints(ipoints, imagePointsNormalized, cameraMatrix, distCoeffs, noArray(), cameraMatrix, criteria); + return cv::solvePnPRansac(opoints, imagePointsNormalized, cameraMatrix, noArray(), rvec, tvec, + useExtrinsicGuess, iterationsCount, reprojectionError, confidence, inliers, flags); +} + namespace cv{ namespace { void subMatrix(const Mat& src, Mat& dst, const std::vector& cols, const std::vector& rows) { diff --git a/modules/calib3d/test/test_fisheye.cpp b/modules/calib3d/test/test_fisheye.cpp index d7368c3190..f3d2ea58da 100644 --- a/modules/calib3d/test/test_fisheye.cpp +++ b/modules/calib3d/test/test_fisheye.cpp @@ -256,7 +256,52 @@ TEST_F(fisheyeTest, solvePnP) bool converged = cv::fisheye::solvePnP(obj_points, img_points, this->K, this->D, rvec_pred, tvec_pred); EXPECT_MAT_NEAR(rvec, rvec_pred, 1e-6); EXPECT_MAT_NEAR(this->T, tvec_pred, 1e-6); + ASSERT_TRUE(converged); +} +TEST_F(fisheyeTest, solvePnPRansac) +{ + const int inliers_n = 16; + const int outliers_n = 4; + const bool use_extrinsic_guess = false; + const int iterations_count = 100; + const float reprojection_error = 1.0; + const double confidence = 0.99; + + cv::Mat rvec; + cv::Rodrigues(this->R, rvec); + + // inliers + cv::Mat inlier_obj_points(1, inliers_n, CV_64FC3); + theRNG().fill(inlier_obj_points, cv::RNG::NORMAL, 2, 1); + inlier_obj_points = cv::abs(inlier_obj_points) * 10; + cv::Mat inlier_img_points; + cv::fisheye::projectPoints(inlier_obj_points, inlier_img_points, rvec, this->T, this->K, this->D); + + // outliers + cv::Mat outlier_obj_points(1, outliers_n, CV_64FC3); + theRNG().fill(outlier_obj_points, cv::RNG::NORMAL, 2, 1); + outlier_obj_points = cv::abs(outlier_obj_points) * 10; + cv::Mat outlier_img_points; + cv::fisheye::projectPoints(outlier_obj_points, outlier_img_points, rvec, (this->T * 10), this->K, this->D); + + cv::Mat obj_points; + cv::hconcat(outlier_obj_points, inlier_obj_points, obj_points); + + cv::Mat img_points; + cv::hconcat(outlier_img_points, inlier_img_points, img_points); + + cv::Mat rvec_pred; + cv::Mat tvec_pred; + cv::Mat inliers_pred; + + bool converged = cv::fisheye::solvePnPRansac(obj_points, img_points, this->K, this->D, + rvec_pred, tvec_pred, use_extrinsic_guess, + iterations_count, reprojection_error, confidence, inliers_pred); + + EXPECT_MAT_NEAR(rvec, rvec_pred, 1e-5); + EXPECT_MAT_NEAR(this->T, tvec_pred, 1e-5); + EXPECT_EQ(inliers_pred.size[0], inliers_n); ASSERT_TRUE(converged); }