Path: blob/master/modules/calib3d/test/test_solvepnp_ransac.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546class CV_solvePnPRansac_Test : public cvtest::BaseTest47{48public:49CV_solvePnPRansac_Test()50{51eps[SOLVEPNP_ITERATIVE] = 1.0e-2;52eps[SOLVEPNP_EPNP] = 1.0e-2;53eps[SOLVEPNP_P3P] = 1.0e-2;54eps[SOLVEPNP_AP3P] = 1.0e-2;55eps[SOLVEPNP_DLS] = 1.0e-2;56eps[SOLVEPNP_UPNP] = 1.0e-2;57totalTestsCount = 10;58pointsCount = 500;59}60~CV_solvePnPRansac_Test() {}61protected:62void generate3DPointCloud(vector<Point3f>& points,63Point3f pmin = Point3f(-1, -1, 5),64Point3f pmax = Point3f(1, 1, 10))65{66RNG rng = cv::theRNG(); // fix the seed to use "fixed" input 3D points6768for (size_t i = 0; i < points.size(); i++)69{70float _x = rng.uniform(pmin.x, pmax.x);71float _y = rng.uniform(pmin.y, pmax.y);72float _z = rng.uniform(pmin.z, pmax.z);73points[i] = Point3f(_x, _y, _z);74}75}7677void generateCameraMatrix(Mat& cameraMatrix, RNG& rng)78{79const double fcMinVal = 1e-3;80const double fcMaxVal = 100;81cameraMatrix.create(3, 3, CV_64FC1);82cameraMatrix.setTo(Scalar(0));83cameraMatrix.at<double>(0,0) = rng.uniform(fcMinVal, fcMaxVal);84cameraMatrix.at<double>(1,1) = rng.uniform(fcMinVal, fcMaxVal);85cameraMatrix.at<double>(0,2) = rng.uniform(fcMinVal, fcMaxVal);86cameraMatrix.at<double>(1,2) = rng.uniform(fcMinVal, fcMaxVal);87cameraMatrix.at<double>(2,2) = 1;88}8990void generateDistCoeffs(Mat& distCoeffs, RNG& rng)91{92distCoeffs = Mat::zeros(4, 1, CV_64FC1);93for (int i = 0; i < 3; i++)94distCoeffs.at<double>(i,0) = rng.uniform(0.0, 1.0e-6);95}9697void generatePose(Mat& rvec, Mat& tvec, RNG& rng)98{99const double minVal = 1.0e-3;100const double maxVal = 1.0;101rvec.create(3, 1, CV_64FC1);102tvec.create(3, 1, CV_64FC1);103for (int i = 0; i < 3; i++)104{105rvec.at<double>(i,0) = rng.uniform(minVal, maxVal);106tvec.at<double>(i,0) = rng.uniform(minVal, maxVal/10);107}108}109110virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)111{112Mat rvec, tvec;113vector<int> inliers;114Mat trueRvec, trueTvec;115Mat intrinsics, distCoeffs;116generateCameraMatrix(intrinsics, rng);117if (method == 4) intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);118if (mode == 0)119distCoeffs = Mat::zeros(4, 1, CV_64FC1);120else121generateDistCoeffs(distCoeffs, rng);122generatePose(trueRvec, trueTvec, rng);123124vector<Point2f> projectedPoints;125projectedPoints.resize(points.size());126projectPoints(Mat(points), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);127for (size_t i = 0; i < projectedPoints.size(); i++)128{129if (i % 20 == 0)130{131projectedPoints[i] = projectedPoints[rng.uniform(0,(int)points.size()-1)];132}133}134135solvePnPRansac(points, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, pointsCount, 0.5f, 0.99, inliers, method);136137bool isTestSuccess = inliers.size() >= points.size()*0.95;138139double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);140isTestSuccess = isTestSuccess && rvecDiff < epsilon[method] && tvecDiff < epsilon[method];141double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;142//cout << error << " " << inliers.size() << " " << eps[method] << endl;143if (error > maxError)144maxError = error;145146return isTestSuccess;147}148149virtual void run(int)150{151ts->set_failed_test_info(cvtest::TS::OK);152153vector<Point3f> points, points_dls;154points.resize(pointsCount);155generate3DPointCloud(points);156157RNG rng = ts->get_rng();158159160for (int mode = 0; mode < 2; mode++)161{162for (int method = 0; method < SOLVEPNP_MAX_COUNT; method++)163{164double maxError = 0;165int successfulTestsCount = 0;166for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)167{168if (runTest(rng, mode, method, points, eps, maxError))169{170successfulTestsCount++;171}172}173if (successfulTestsCount < 0.7*totalTestsCount)174{175ts->printf( cvtest::TS::LOG, "Invalid accuracy for method %d, failed %d tests from %d, maximum error equals %f, distortion mode equals %d\n",176method, totalTestsCount - successfulTestsCount, totalTestsCount, maxError, mode);177ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);178}179cout << "mode: " << mode << ", method: " << method << " -> "180<< ((double)successfulTestsCount / totalTestsCount) * 100 << "%"181<< " (err < " << maxError << ")" << endl;182}183}184}185double eps[SOLVEPNP_MAX_COUNT];186int totalTestsCount;187int pointsCount;188};189190class CV_solvePnP_Test : public CV_solvePnPRansac_Test191{192public:193CV_solvePnP_Test()194{195eps[SOLVEPNP_ITERATIVE] = 1.0e-6;196eps[SOLVEPNP_EPNP] = 1.0e-6;197eps[SOLVEPNP_P3P] = 2.0e-4;198eps[SOLVEPNP_AP3P] = 1.0e-4;199eps[SOLVEPNP_DLS] = 1.0e-4;200eps[SOLVEPNP_UPNP] = 1.0e-4;201totalTestsCount = 1000;202}203204~CV_solvePnP_Test() {}205protected:206virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)207{208Mat rvec, tvec;209Mat trueRvec, trueTvec;210Mat intrinsics, distCoeffs;211generateCameraMatrix(intrinsics, rng);212if (method == SOLVEPNP_DLS)213{214intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);215}216if (mode == 0)217{218distCoeffs = Mat::zeros(4, 1, CV_64FC1);219}220else221{222generateDistCoeffs(distCoeffs, rng);223}224generatePose(trueRvec, trueTvec, rng);225226std::vector<Point3f> opoints;227switch(method)228{229case SOLVEPNP_P3P:230case SOLVEPNP_AP3P:231opoints = std::vector<Point3f>(points.begin(), points.begin()+4);232break;233case SOLVEPNP_UPNP:234opoints = std::vector<Point3f>(points.begin(), points.begin()+50);235break;236default:237opoints = points;238break;239}240241vector<Point2f> projectedPoints;242projectedPoints.resize(opoints.size());243projectPoints(Mat(opoints), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);244245bool isEstimateSuccess = solvePnP(opoints, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, method);246if (isEstimateSuccess == false)247{248return isEstimateSuccess;249}250251double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);252bool isTestSuccess = rvecDiff < epsilon[method] && tvecDiff < epsilon[method];253254double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;255if (error > maxError)256{257maxError = error;258}259260return isTestSuccess;261}262};263264class CV_solveP3P_Test : public CV_solvePnPRansac_Test265{266public:267CV_solveP3P_Test()268{269eps[SOLVEPNP_P3P] = 2.0e-4;270eps[SOLVEPNP_AP3P] = 1.0e-4;271totalTestsCount = 1000;272}273274~CV_solveP3P_Test() {}275protected:276virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)277{278std::vector<Mat> rvecs, tvecs;279Mat trueRvec, trueTvec;280Mat intrinsics, distCoeffs;281generateCameraMatrix(intrinsics, rng);282if (mode == 0)283distCoeffs = Mat::zeros(4, 1, CV_64FC1);284else285generateDistCoeffs(distCoeffs, rng);286generatePose(trueRvec, trueTvec, rng);287288std::vector<Point3f> opoints;289opoints = std::vector<Point3f>(points.begin(), points.begin()+3);290291vector<Point2f> projectedPoints;292projectedPoints.resize(opoints.size());293projectPoints(Mat(opoints), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);294295int num_of_solutions = solveP3P(opoints, projectedPoints, intrinsics, distCoeffs, rvecs, tvecs, method);296if (num_of_solutions != (int) rvecs.size() || num_of_solutions != (int) tvecs.size() || num_of_solutions == 0)297return false;298299bool isTestSuccess = false;300double error = DBL_MAX;301for (unsigned int i = 0; i < rvecs.size() && !isTestSuccess; ++i) {302double rvecDiff = cvtest::norm(rvecs[i], trueRvec, NORM_L2);303double tvecDiff = cvtest::norm(tvecs[i], trueTvec, NORM_L2);304isTestSuccess = rvecDiff < epsilon[method] && tvecDiff < epsilon[method];305error = std::min(error, std::max(rvecDiff, tvecDiff));306}307308if (error > maxError)309maxError = error;310311return isTestSuccess;312}313314virtual void run(int)315{316ts->set_failed_test_info(cvtest::TS::OK);317318vector<Point3f> points;319points.resize(pointsCount);320generate3DPointCloud(points);321322const int methodsCount = 2;323int methods[] = {SOLVEPNP_P3P, SOLVEPNP_AP3P};324RNG rng = ts->get_rng();325326for (int mode = 0; mode < 2; mode++)327{328for (int method = 0; method < methodsCount; method++)329{330double maxError = 0;331int successfulTestsCount = 0;332for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)333{334if (runTest(rng, mode, methods[method], points, eps, maxError))335successfulTestsCount++;336}337if (successfulTestsCount < 0.7*totalTestsCount)338{339ts->printf( cvtest::TS::LOG, "Invalid accuracy for method %d, failed %d tests from %d, maximum error equals %f, distortion mode equals %d\n",340method, totalTestsCount - successfulTestsCount, totalTestsCount, maxError, mode);341ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);342}343cout << "mode: " << mode << ", method: " << method << " -> "344<< ((double)successfulTestsCount / totalTestsCount) * 100 << "%"345<< " (err < " << maxError << ")" << endl;346}347}348}349};350351352TEST(Calib3d_SolveP3P, accuracy) { CV_solveP3P_Test test; test.safe_run();}353TEST(Calib3d_SolvePnPRansac, accuracy) { CV_solvePnPRansac_Test test; test.safe_run(); }354TEST(Calib3d_SolvePnP, accuracy) { CV_solvePnP_Test test; test.safe_run(); }355356TEST(Calib3d_SolvePnPRansac, concurrency)357{358int count = 7*13;359360Mat object(1, count, CV_32FC3);361randu(object, -100, 100);362363Mat camera_mat(3, 3, CV_32FC1);364randu(camera_mat, 0.5, 1);365camera_mat.at<float>(0, 1) = 0.f;366camera_mat.at<float>(1, 0) = 0.f;367camera_mat.at<float>(2, 0) = 0.f;368camera_mat.at<float>(2, 1) = 0.f;369370Mat dist_coef(1, 8, CV_32F, cv::Scalar::all(0));371372vector<cv::Point2f> image_vec;373Mat rvec_gold(1, 3, CV_32FC1);374randu(rvec_gold, 0, 1);375Mat tvec_gold(1, 3, CV_32FC1);376randu(tvec_gold, 0, 1);377projectPoints(object, rvec_gold, tvec_gold, camera_mat, dist_coef, image_vec);378379Mat image(1, count, CV_32FC2, &image_vec[0]);380381Mat rvec1, rvec2;382Mat tvec1, tvec2;383384int threads = getNumThreads();385{386// limit concurrency to get deterministic result387theRNG().state = 20121010;388setNumThreads(1);389solvePnPRansac(object, image, camera_mat, dist_coef, rvec1, tvec1);390}391392{393setNumThreads(threads);394Mat rvec;395Mat tvec;396// parallel executions397for(int i = 0; i < 10; ++i)398{399cv::theRNG().state = 20121010;400solvePnPRansac(object, image, camera_mat, dist_coef, rvec, tvec);401}402}403404{405// single thread again406theRNG().state = 20121010;407setNumThreads(1);408solvePnPRansac(object, image, camera_mat, dist_coef, rvec2, tvec2);409}410411double rnorm = cvtest::norm(rvec1, rvec2, NORM_INF);412double tnorm = cvtest::norm(tvec1, tvec2, NORM_INF);413414EXPECT_LT(rnorm, 1e-6);415EXPECT_LT(tnorm, 1e-6);416}417418TEST(Calib3d_SolvePnPRansac, input_type)419{420const int numPoints = 10;421Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,4225.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);423424std::vector<cv::Point3f> points3d;425std::vector<cv::Point2f> points2d;426for (int i = 0; i < numPoints; i+=2)427{428points3d.push_back(cv::Point3i(5+i, 3, 2));429points3d.push_back(cv::Point3i(5+i, 3+i, 2+i));430points2d.push_back(cv::Point2i(0, i));431points2d.push_back(cv::Point2i(-i, i));432}433Mat R1, t1, R2, t2, R3, t3, R4, t4;434435EXPECT_TRUE(solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R1, t1));436437Mat points3dMat(points3d);438Mat points2dMat(points2d);439EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R2, t2));440441points3dMat = points3dMat.reshape(3, 1);442points2dMat = points2dMat.reshape(2, 1);443EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R3, t3));444445points3dMat = points3dMat.reshape(1, numPoints);446points2dMat = points2dMat.reshape(1, numPoints);447EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R4, t4));448449EXPECT_LE(cvtest::norm(R1, R2, NORM_INF), 1e-6);450EXPECT_LE(cvtest::norm(t1, t2, NORM_INF), 1e-6);451EXPECT_LE(cvtest::norm(R1, R3, NORM_INF), 1e-6);452EXPECT_LE(cvtest::norm(t1, t3, NORM_INF), 1e-6);453EXPECT_LE(cvtest::norm(R1, R4, NORM_INF), 1e-6);454EXPECT_LE(cvtest::norm(t1, t4, NORM_INF), 1e-6);455}456457TEST(Calib3d_SolvePnP, double_support)458{459Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,4605.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);461std::vector<cv::Point3d> points3d;462std::vector<cv::Point2d> points2d;463std::vector<cv::Point3f> points3dF;464std::vector<cv::Point2f> points2dF;465for (int i = 0; i < 10 ; i+=2)466{467points3d.push_back(cv::Point3d(5+i, 3, 2));468points3dF.push_back(cv::Point3d(5+i, 3, 2));469points3d.push_back(cv::Point3d(5+i, 3+i, 2+i));470points3dF.push_back(cv::Point3d(5+i, 3+i, 2+i));471points2d.push_back(cv::Point2d(0, i));472points2dF.push_back(cv::Point2d(0, i));473points2d.push_back(cv::Point2d(-i, i));474points2dF.push_back(cv::Point2d(-i, i));475}476Mat R,t, RF, tF;477vector<int> inliers;478479solvePnPRansac(points3dF, points2dF, intrinsics, cv::Mat(), RF, tF, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);480solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R, t, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);481482EXPECT_LE(cvtest::norm(R, Mat_<double>(RF), NORM_INF), 1e-3);483EXPECT_LE(cvtest::norm(t, Mat_<double>(tF), NORM_INF), 1e-3);484}485486TEST(Calib3d_SolvePnP, translation)487{488Mat cameraIntrinsic = Mat::eye(3,3, CV_32FC1);489vector<float> crvec;490crvec.push_back(0.f);491crvec.push_back(0.f);492crvec.push_back(0.f);493vector<float> ctvec;494ctvec.push_back(100.f);495ctvec.push_back(100.f);496ctvec.push_back(0.f);497vector<Point3f> p3d;498p3d.push_back(Point3f(0,0,0));499p3d.push_back(Point3f(0,0,10));500p3d.push_back(Point3f(0,10,10));501p3d.push_back(Point3f(10,10,10));502p3d.push_back(Point3f(2,5,5));503504vector<Point2f> p2d;505projectPoints(p3d, crvec, ctvec, cameraIntrinsic, noArray(), p2d);506Mat rvec;507Mat tvec;508rvec =(Mat_<float>(3,1) << 0, 0, 0);509tvec = (Mat_<float>(3,1) << 100, 100, 0);510511solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);512EXPECT_TRUE(checkRange(rvec));513EXPECT_TRUE(checkRange(tvec));514515rvec =(Mat_<double>(3,1) << 0, 0, 0);516tvec = (Mat_<double>(3,1) << 100, 100, 0);517solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);518EXPECT_TRUE(checkRange(rvec));519EXPECT_TRUE(checkRange(tvec));520521solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, false);522EXPECT_TRUE(checkRange(rvec));523EXPECT_TRUE(checkRange(tvec));524}525526TEST(Calib3d_SolvePnP, iterativeInitialGuess3pts)527{528{529Matx33d intrinsics(605.4, 0.0, 317.35,5300.0, 601.2, 242.63,5310.0, 0.0, 1.0);532533double L = 0.1;534vector<Point3d> p3d;535p3d.push_back(Point3d(-L, -L, 0.0));536p3d.push_back(Point3d(L, -L, 0.0));537p3d.push_back(Point3d(L, L, 0.0));538539Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);540Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);541542vector<Point2d> p2d;543projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);544545Mat rvec_est = (Mat_<double>(3,1) << 0.2, -0.1, 0.6);546Mat tvec_est = (Mat_<double>(3,1) << 0.05, -0.05, 1.0);547548solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);549550std::cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;551std::cout << "rvec_est: " << rvec_est.t() << std::endl;552std::cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;553std::cout << "tvec_est: " << tvec_est.t() << std::endl;554555EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);556EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);557}558559{560Matx33f intrinsics(605.4f, 0.0f, 317.35f,5610.0f, 601.2f, 242.63f,5620.0f, 0.0f, 1.0f);563564float L = 0.1f;565vector<Point3f> p3d;566p3d.push_back(Point3f(-L, -L, 0.0f));567p3d.push_back(Point3f(L, -L, 0.0f));568p3d.push_back(Point3f(L, L, 0.0f));569570Mat rvec_ground_truth = (Mat_<float>(3,1) << -0.75f, 0.4f, 0.34f);571Mat tvec_ground_truth = (Mat_<float>(3,1) << -0.15f, 0.35f, 1.58f);572573vector<Point2f> p2d;574projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);575576Mat rvec_est = (Mat_<float>(3,1) << -0.5f, 0.2f, 0.2f);577Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.2f, 1.0f);578579solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);580581std::cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;582std::cout << "rvec_est: " << rvec_est.t() << std::endl;583std::cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;584std::cout << "tvec_est: " << tvec_est.t() << std::endl;585586EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);587EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);588}589}590591}} // namespace592593594