test_solvepnp_ransac.cpp 20.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "test_precomp.hpp"
44

45
namespace opencv_test { namespace {
46 47 48 49

class CV_solvePnPRansac_Test : public cvtest::BaseTest
{
public:
Alexander Shishkov's avatar
Alexander Shishkov committed
50 51
    CV_solvePnPRansac_Test()
    {
edgarriba's avatar
edgarriba committed
52 53 54
        eps[SOLVEPNP_ITERATIVE] = 1.0e-2;
        eps[SOLVEPNP_EPNP] = 1.0e-2;
        eps[SOLVEPNP_P3P] = 1.0e-2;
55
        eps[SOLVEPNP_AP3P] = 1.0e-2;
edgarriba's avatar
edgarriba committed
56
        eps[SOLVEPNP_DLS] = 1.0e-2;
57
        eps[SOLVEPNP_UPNP] = 1.0e-2;
Alexander Shishkov's avatar
Alexander Shishkov committed
58
        totalTestsCount = 10;
59
        pointsCount = 500;
Alexander Shishkov's avatar
Alexander Shishkov committed
60
    }
61 62
    ~CV_solvePnPRansac_Test() {}
protected:
63 64 65
    void generate3DPointCloud(vector<Point3f>& points,
        Point3f pmin = Point3f(-1, -1, 5),
        Point3f pmax = Point3f(1, 1, 10))
66
    {
67
        RNG rng = cv::theRNG(); // fix the seed to use "fixed" input 3D points
68

Alexander Shishkov's avatar
Alexander Shishkov committed
69 70
        for (size_t i = 0; i < points.size(); i++)
        {
71 72 73 74
            float _x = rng.uniform(pmin.x, pmax.x);
            float _y = rng.uniform(pmin.y, pmax.y);
            float _z = rng.uniform(pmin.z, pmax.z);
            points[i] = Point3f(_x, _y, _z);
Alexander Shishkov's avatar
Alexander Shishkov committed
75
        }
76 77
    }

Alexander Shishkov's avatar
Alexander Shishkov committed
78
    void generateCameraMatrix(Mat& cameraMatrix, RNG& rng)
79
    {
Alexander Shishkov's avatar
Alexander Shishkov committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        const double fcMinVal = 1e-3;
        const double fcMaxVal = 100;
        cameraMatrix.create(3, 3, CV_64FC1);
        cameraMatrix.setTo(Scalar(0));
        cameraMatrix.at<double>(0,0) = rng.uniform(fcMinVal, fcMaxVal);
        cameraMatrix.at<double>(1,1) = rng.uniform(fcMinVal, fcMaxVal);
        cameraMatrix.at<double>(0,2) = rng.uniform(fcMinVal, fcMaxVal);
        cameraMatrix.at<double>(1,2) = rng.uniform(fcMinVal, fcMaxVal);
        cameraMatrix.at<double>(2,2) = 1;
    }

    void generateDistCoeffs(Mat& distCoeffs, RNG& rng)
    {
        distCoeffs = Mat::zeros(4, 1, CV_64FC1);
        for (int i = 0; i < 3; i++)
            distCoeffs.at<double>(i,0) = rng.uniform(0.0, 1.0e-6);
    }
97

Alexander Shishkov's avatar
Alexander Shishkov committed
98 99 100 101 102 103 104 105 106 107 108 109 110
    void generatePose(Mat& rvec, Mat& tvec, RNG& rng)
    {
        const double minVal = 1.0e-3;
        const double maxVal = 1.0;
        rvec.create(3, 1, CV_64FC1);
        tvec.create(3, 1, CV_64FC1);
        for (int i = 0; i < 3; i++)
        {
            rvec.at<double>(i,0) = rng.uniform(minVal, maxVal);
            tvec.at<double>(i,0) = rng.uniform(minVal, maxVal/10);
        }
    }

Andrey Kamaev's avatar
Andrey Kamaev committed
111
    virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)
Alexander Shishkov's avatar
Alexander Shishkov committed
112 113 114 115 116 117
    {
        Mat rvec, tvec;
        vector<int> inliers;
        Mat trueRvec, trueTvec;
        Mat intrinsics, distCoeffs;
        generateCameraMatrix(intrinsics, rng);
118
        if (method == 4) intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
Alexander Shishkov's avatar
Alexander Shishkov committed
119 120 121 122 123 124 125 126 127 128
        if (mode == 0)
            distCoeffs = Mat::zeros(4, 1, CV_64FC1);
        else
            generateDistCoeffs(distCoeffs, rng);
        generatePose(trueRvec, trueTvec, rng);

        vector<Point2f> projectedPoints;
        projectedPoints.resize(points.size());
        projectPoints(Mat(points), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);
        for (size_t i = 0; i < projectedPoints.size(); i++)
129 130 131
        {
            if (i % 20 == 0)
            {
132
                projectedPoints[i] = projectedPoints[rng.uniform(0,(int)points.size()-1)];
133 134
            }
        }
135

136
        solvePnPRansac(points, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, pointsCount, 0.5f, 0.99, inliers, method);
137

Alexander Shishkov's avatar
Alexander Shishkov committed
138
        bool isTestSuccess = inliers.size() >= points.size()*0.95;
139

140
        double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);
Andrey Kamaev's avatar
Andrey Kamaev committed
141
        isTestSuccess = isTestSuccess && rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
Alexander Shishkov's avatar
Alexander Shishkov committed
142 143 144 145
        double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;
        //cout << error << " " << inliers.size() << " " << eps[method] << endl;
        if (error > maxError)
            maxError = error;
146

Alexander Shishkov's avatar
Alexander Shishkov committed
147 148
        return isTestSuccess;
    }
149

150
    virtual void run(int)
Alexander Shishkov's avatar
Alexander Shishkov committed
151
    {
Andrey Kamaev's avatar
Andrey Kamaev committed
152
        ts->set_failed_test_info(cvtest::TS::OK);
153

edgarriba's avatar
edgarriba committed
154
        vector<Point3f> points, points_dls;
Alexander Shishkov's avatar
Alexander Shishkov committed
155 156 157
        points.resize(pointsCount);
        generate3DPointCloud(points);

Andrey Kamaev's avatar
Andrey Kamaev committed
158
        RNG rng = ts->get_rng();
Alexander Shishkov's avatar
Alexander Shishkov committed
159 160 161 162


        for (int mode = 0; mode < 2; mode++)
        {
163
            for (int method = 0; method < SOLVEPNP_MAX_COUNT; method++)
164
            {
Alexander Shishkov's avatar
Alexander Shishkov committed
165 166 167 168
                double maxError = 0;
                int successfulTestsCount = 0;
                for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)
                {
edgarriba's avatar
edgarriba committed
169
                    if (runTest(rng, mode, method, points, eps, maxError))
170
                    {
Alexander Shishkov's avatar
Alexander Shishkov committed
171
                        successfulTestsCount++;
172
                    }
Alexander Shishkov's avatar
Alexander Shishkov committed
173 174 175
                }
                if (successfulTestsCount < 0.7*totalTestsCount)
                {
Andrey Kamaev's avatar
Andrey Kamaev committed
176
                    ts->printf( cvtest::TS::LOG, "Invalid accuracy for method %d, failed %d tests from %d, maximum error equals %f, distortion mode equals %d\n",
Alexander Shishkov's avatar
Alexander Shishkov committed
177
                        method, totalTestsCount - successfulTestsCount, totalTestsCount, maxError, mode);
Andrey Kamaev's avatar
Andrey Kamaev committed
178
                    ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
Alexander Shishkov's avatar
Alexander Shishkov committed
179
                }
180 181 182
                cout << "mode: " << mode << ", method: " << method << " -> "
                     << ((double)successfulTestsCount / totalTestsCount) * 100 << "%"
                     << " (err < " << maxError << ")" << endl;
183 184
            }
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
185
    }
186
    double eps[SOLVEPNP_MAX_COUNT];
Alexander Shishkov's avatar
Alexander Shishkov committed
187
    int totalTestsCount;
188
    int pointsCount;
Alexander Shishkov's avatar
Alexander Shishkov committed
189
};
190

Alexander Shishkov's avatar
Alexander Shishkov committed
191 192 193 194 195
class CV_solvePnP_Test : public CV_solvePnPRansac_Test
{
public:
    CV_solvePnP_Test()
    {
edgarriba's avatar
edgarriba committed
196 197
        eps[SOLVEPNP_ITERATIVE] = 1.0e-6;
        eps[SOLVEPNP_EPNP] = 1.0e-6;
198
        eps[SOLVEPNP_P3P] = 2.0e-4;
199
        eps[SOLVEPNP_AP3P] = 1.0e-4;
200
        eps[SOLVEPNP_DLS] = 1.0e-4;
201
        eps[SOLVEPNP_UPNP] = 1.0e-4;
Alexander Shishkov's avatar
Alexander Shishkov committed
202 203 204 205 206
        totalTestsCount = 1000;
    }

    ~CV_solvePnP_Test() {}
protected:
Andrey Kamaev's avatar
Andrey Kamaev committed
207
    virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)
Alexander Shishkov's avatar
Alexander Shishkov committed
208 209 210 211 212
    {
        Mat rvec, tvec;
        Mat trueRvec, trueTvec;
        Mat intrinsics, distCoeffs;
        generateCameraMatrix(intrinsics, rng);
213 214 215 216
        if (method == SOLVEPNP_DLS)
        {
            intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
217
        if (mode == 0)
218
        {
Alexander Shishkov's avatar
Alexander Shishkov committed
219
            distCoeffs = Mat::zeros(4, 1, CV_64FC1);
220
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
221
        else
222
        {
Alexander Shishkov's avatar
Alexander Shishkov committed
223
            generateDistCoeffs(distCoeffs, rng);
224
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
225 226 227
        generatePose(trueRvec, trueTvec, rng);

        std::vector<Point3f> opoints;
228
        switch(method)
229
        {
230 231 232 233 234 235 236 237 238 239
            case SOLVEPNP_P3P:
            case SOLVEPNP_AP3P:
                opoints = std::vector<Point3f>(points.begin(), points.begin()+4);
                break;
            case SOLVEPNP_UPNP:
                opoints = std::vector<Point3f>(points.begin(), points.begin()+50);
                break;
            default:
                opoints = points;
                break;
240
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
241 242 243 244 245

        vector<Point2f> projectedPoints;
        projectedPoints.resize(opoints.size());
        projectPoints(Mat(opoints), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);

246 247 248 249 250
        bool isEstimateSuccess = solvePnP(opoints, projectedPoints, intrinsics, distCoeffs, rvec, tvec, false, method);
        if (isEstimateSuccess == false)
        {
            return isEstimateSuccess;
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
251

252
        double rvecDiff = cvtest::norm(rvec, trueRvec, NORM_L2), tvecDiff = cvtest::norm(tvec, trueTvec, NORM_L2);
Andrey Kamaev's avatar
Andrey Kamaev committed
253
        bool isTestSuccess = rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
Alexander Shishkov's avatar
Alexander Shishkov committed
254 255 256

        double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;
        if (error > maxError)
257
        {
Alexander Shishkov's avatar
Alexander Shishkov committed
258
            maxError = error;
259
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
260 261

        return isTestSuccess;
262 263 264
    }
};

265 266 267 268 269
class CV_solveP3P_Test : public CV_solvePnPRansac_Test
{
 public:
  CV_solveP3P_Test()
  {
270
    eps[SOLVEPNP_P3P] = 2.0e-4;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    eps[SOLVEPNP_AP3P] = 1.0e-4;
    totalTestsCount = 1000;
  }

  ~CV_solveP3P_Test() {}
 protected:
  virtual bool runTest(RNG& rng, int mode, int method, const vector<Point3f>& points, const double* epsilon, double& maxError)
  {
    std::vector<Mat> rvecs, tvecs;
    Mat trueRvec, trueTvec;
    Mat intrinsics, distCoeffs;
    generateCameraMatrix(intrinsics, rng);
    if (mode == 0)
      distCoeffs = Mat::zeros(4, 1, CV_64FC1);
    else
      generateDistCoeffs(distCoeffs, rng);
    generatePose(trueRvec, trueTvec, rng);

    std::vector<Point3f> opoints;
    opoints = std::vector<Point3f>(points.begin(), points.begin()+3);

    vector<Point2f> projectedPoints;
    projectedPoints.resize(opoints.size());
    projectPoints(Mat(opoints), trueRvec, trueTvec, intrinsics, distCoeffs, projectedPoints);

    int num_of_solutions = solveP3P(opoints, projectedPoints, intrinsics, distCoeffs, rvecs, tvecs, method);
    if (num_of_solutions != (int) rvecs.size() || num_of_solutions != (int) tvecs.size() || num_of_solutions == 0)
      return false;

300 301 302
    bool isTestSuccess = false;
    double error = DBL_MAX;
    for (unsigned int i = 0; i < rvecs.size() && !isTestSuccess; ++i) {
303 304
      double rvecDiff = cvtest::norm(rvecs[i], trueRvec, NORM_L2);
      double tvecDiff = cvtest::norm(tvecs[i], trueTvec, NORM_L2);
305 306
      isTestSuccess = rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
      error = std::min(error, std::max(rvecDiff, tvecDiff));
307 308 309 310 311 312 313 314 315 316 317 318
    }

    if (error > maxError)
      maxError = error;

    return isTestSuccess;
  }

  virtual void run(int)
  {
    ts->set_failed_test_info(cvtest::TS::OK);

319
    vector<Point3f> points;
320 321 322 323
    points.resize(pointsCount);
    generate3DPointCloud(points);

    const int methodsCount = 2;
324
    int methods[] = {SOLVEPNP_P3P, SOLVEPNP_AP3P};
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    RNG rng = ts->get_rng();

    for (int mode = 0; mode < 2; mode++)
    {
      for (int method = 0; method < methodsCount; method++)
      {
        double maxError = 0;
        int successfulTestsCount = 0;
        for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)
        {
          if (runTest(rng, mode, methods[method], points, eps, maxError))
            successfulTestsCount++;
        }
        if (successfulTestsCount < 0.7*totalTestsCount)
        {
          ts->printf( cvtest::TS::LOG, "Invalid accuracy for method %d, failed %d tests from %d, maximum error equals %f, distortion mode equals %d\n",
                      method, totalTestsCount - successfulTestsCount, totalTestsCount, maxError, mode);
          ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
        }
        cout << "mode: " << mode << ", method: " << method << " -> "
             << ((double)successfulTestsCount / totalTestsCount) * 100 << "%"
             << " (err < " << maxError << ")" << endl;
      }
    }
  }
};


TEST(Calib3d_SolveP3P, accuracy) { CV_solveP3P_Test test; test.safe_run();}
edgarriba's avatar
edgarriba committed
354
TEST(Calib3d_SolvePnPRansac, accuracy) { CV_solvePnPRansac_Test test; test.safe_run(); }
Alexander Shishkov's avatar
Alexander Shishkov committed
355
TEST(Calib3d_SolvePnP, accuracy) { CV_solvePnP_Test test; test.safe_run(); }
356

357
TEST(Calib3d_SolvePnPRansac, concurrency)
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
{
    int count = 7*13;

    Mat object(1, count, CV_32FC3);
    randu(object, -100, 100);

    Mat camera_mat(3, 3, CV_32FC1);
    randu(camera_mat, 0.5, 1);
    camera_mat.at<float>(0, 1) = 0.f;
    camera_mat.at<float>(1, 0) = 0.f;
    camera_mat.at<float>(2, 0) = 0.f;
    camera_mat.at<float>(2, 1) = 0.f;

    Mat dist_coef(1, 8, CV_32F, cv::Scalar::all(0));

    vector<cv::Point2f> image_vec;
    Mat rvec_gold(1, 3, CV_32FC1);
    randu(rvec_gold, 0, 1);
    Mat tvec_gold(1, 3, CV_32FC1);
    randu(tvec_gold, 0, 1);
    projectPoints(object, rvec_gold, tvec_gold, camera_mat, dist_coef, image_vec);

    Mat image(1, count, CV_32FC2, &image_vec[0]);

    Mat rvec1, rvec2;
    Mat tvec1, tvec2;

385
    int threads = getNumThreads();
386
    {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
387
        // limit concurrency to get deterministic result
388 389
        theRNG().state = 20121010;
        setNumThreads(1);
390 391 392 393
        solvePnPRansac(object, image, camera_mat, dist_coef, rvec1, tvec1);
    }

    {
394
        setNumThreads(threads);
395 396 397 398 399 400 401 402 403 404 405 406
        Mat rvec;
        Mat tvec;
        // parallel executions
        for(int i = 0; i < 10; ++i)
        {
            cv::theRNG().state = 20121010;
            solvePnPRansac(object, image, camera_mat, dist_coef, rvec, tvec);
        }
    }

    {
        // single thread again
407 408
        theRNG().state = 20121010;
        setNumThreads(1);
409 410 411
        solvePnPRansac(object, image, camera_mat, dist_coef, rvec2, tvec2);
    }

412 413
    double rnorm = cvtest::norm(rvec1, rvec2, NORM_INF);
    double tnorm = cvtest::norm(tvec1, tvec2, NORM_INF);
414 415 416 417

    EXPECT_LT(rnorm, 1e-6);
    EXPECT_LT(tnorm, 1e-6);
}
418

419 420 421 422 423 424 425 426
TEST(Calib3d_SolvePnPRansac, input_type)
{
    const int numPoints = 10;
    Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,
        5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);

    std::vector<cv::Point3f> points3d;
    std::vector<cv::Point2f> points2d;
427
    for (int i = 0; i < numPoints; i+=2)
428
    {
429 430 431 432
        points3d.push_back(cv::Point3i(5+i, 3, 2));
        points3d.push_back(cv::Point3i(5+i, 3+i, 2+i));
        points2d.push_back(cv::Point2i(0, i));
        points2d.push_back(cv::Point2i(-i, i));
433 434 435
    }
    Mat R1, t1, R2, t2, R3, t3, R4, t4;

436
    EXPECT_TRUE(solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R1, t1));
437 438 439

    Mat points3dMat(points3d);
    Mat points2dMat(points2d);
440
    EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R2, t2));
441 442 443

    points3dMat = points3dMat.reshape(3, 1);
    points2dMat = points2dMat.reshape(2, 1);
444
    EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R3, t3));
445 446 447

    points3dMat = points3dMat.reshape(1, numPoints);
    points2dMat = points2dMat.reshape(1, numPoints);
448 449
    EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R4, t4));

450 451 452 453 454 455
    EXPECT_LE(cvtest::norm(R1, R2, NORM_INF), 1e-6);
    EXPECT_LE(cvtest::norm(t1, t2, NORM_INF), 1e-6);
    EXPECT_LE(cvtest::norm(R1, R3, NORM_INF), 1e-6);
    EXPECT_LE(cvtest::norm(t1, t3, NORM_INF), 1e-6);
    EXPECT_LE(cvtest::norm(R1, R4, NORM_INF), 1e-6);
    EXPECT_LE(cvtest::norm(t1, t4, NORM_INF), 1e-6);
456 457
}

458 459 460 461 462 463 464 465
TEST(Calib3d_SolvePnP, double_support)
{
    Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0.,
                       5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.);
    std::vector<cv::Point3d> points3d;
    std::vector<cv::Point2d> points2d;
    std::vector<cv::Point3f> points3dF;
    std::vector<cv::Point2f> points2dF;
466
    for (int i = 0; i < 10 ; i+=2)
467
    {
468 469 470 471 472 473 474 475
        points3d.push_back(cv::Point3d(5+i, 3, 2));
        points3dF.push_back(cv::Point3d(5+i, 3, 2));
        points3d.push_back(cv::Point3d(5+i, 3+i, 2+i));
        points3dF.push_back(cv::Point3d(5+i, 3+i, 2+i));
        points2d.push_back(cv::Point2d(0, i));
        points2dF.push_back(cv::Point2d(0, i));
        points2d.push_back(cv::Point2d(-i, i));
        points2dF.push_back(cv::Point2d(-i, i));
476 477 478 479
    }
    Mat R,t, RF, tF;
    vector<int> inliers;

480 481
    solvePnPRansac(points3dF, points2dF, intrinsics, cv::Mat(), RF, tF, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);
    solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R, t, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P);
482

483 484
    EXPECT_LE(cvtest::norm(R, Mat_<double>(RF), NORM_INF), 1e-3);
    EXPECT_LE(cvtest::norm(t, Mat_<double>(tF), NORM_INF), 1e-3);
485
}
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

TEST(Calib3d_SolvePnP, translation)
{
    Mat cameraIntrinsic = Mat::eye(3,3, CV_32FC1);
    vector<float> crvec;
    crvec.push_back(0.f);
    crvec.push_back(0.f);
    crvec.push_back(0.f);
    vector<float> ctvec;
    ctvec.push_back(100.f);
    ctvec.push_back(100.f);
    ctvec.push_back(0.f);
    vector<Point3f> p3d;
    p3d.push_back(Point3f(0,0,0));
    p3d.push_back(Point3f(0,0,10));
    p3d.push_back(Point3f(0,10,10));
    p3d.push_back(Point3f(10,10,10));
    p3d.push_back(Point3f(2,5,5));

    vector<Point2f> p2d;
    projectPoints(p3d, crvec, ctvec, cameraIntrinsic, noArray(), p2d);
    Mat rvec;
    Mat tvec;
    rvec =(Mat_<float>(3,1) << 0, 0, 0);
    tvec = (Mat_<float>(3,1) << 100, 100, 0);

    solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);
513 514
    EXPECT_TRUE(checkRange(rvec));
    EXPECT_TRUE(checkRange(tvec));
515 516 517 518

    rvec =(Mat_<double>(3,1) << 0, 0, 0);
    tvec = (Mat_<double>(3,1) << 100, 100, 0);
    solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true);
519 520
    EXPECT_TRUE(checkRange(rvec));
    EXPECT_TRUE(checkRange(tvec));
521 522

    solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, false);
523 524
    EXPECT_TRUE(checkRange(rvec));
    EXPECT_TRUE(checkRange(tvec));
525
}
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

TEST(Calib3d_SolvePnP, iterativeInitialGuess3pts)
{
    {
        Matx33d intrinsics(605.4, 0.0, 317.35,
                           0.0, 601.2, 242.63,
                           0.0, 0.0, 1.0);

        double L = 0.1;
        vector<Point3d> p3d;
        p3d.push_back(Point3d(-L, -L, 0.0));
        p3d.push_back(Point3d(L, -L, 0.0));
        p3d.push_back(Point3d(L, L, 0.0));

        Mat rvec_ground_truth = (Mat_<double>(3,1) << 0.3, -0.2, 0.75);
        Mat tvec_ground_truth = (Mat_<double>(3,1) << 0.15, -0.2, 1.5);

        vector<Point2d> p2d;
        projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);

        Mat rvec_est = (Mat_<double>(3,1) << 0.2, -0.1, 0.6);
        Mat tvec_est = (Mat_<double>(3,1) << 0.05, -0.05, 1.0);

        solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);

        std::cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
        std::cout << "rvec_est: " << rvec_est.t() << std::endl;
        std::cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
        std::cout << "tvec_est: " << tvec_est.t() << std::endl;

556 557
        EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
        EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    }

    {
        Matx33f intrinsics(605.4f, 0.0f, 317.35f,
                           0.0f, 601.2f, 242.63f,
                           0.0f, 0.0f, 1.0f);

        float L = 0.1f;
        vector<Point3f> p3d;
        p3d.push_back(Point3f(-L, -L, 0.0f));
        p3d.push_back(Point3f(L, -L, 0.0f));
        p3d.push_back(Point3f(L, L, 0.0f));

        Mat rvec_ground_truth = (Mat_<float>(3,1) << -0.75f, 0.4f, 0.34f);
        Mat tvec_ground_truth = (Mat_<float>(3,1) << -0.15f, 0.35f, 1.58f);

        vector<Point2f> p2d;
        projectPoints(p3d, rvec_ground_truth, tvec_ground_truth, intrinsics, noArray(), p2d);

        Mat rvec_est = (Mat_<float>(3,1) << -0.5f, 0.2f, 0.2f);
        Mat tvec_est = (Mat_<float>(3,1) << 0.0f, 0.2f, 1.0f);

        solvePnP(p3d, p2d, intrinsics, noArray(), rvec_est, tvec_est, true, SOLVEPNP_ITERATIVE);

        std::cout << "rvec_ground_truth: " << rvec_ground_truth.t() << std::endl;
        std::cout << "rvec_est: " << rvec_est.t() << std::endl;
        std::cout << "tvec_ground_truth: " << tvec_ground_truth.t() << std::endl;
        std::cout << "tvec_est: " << tvec_est.t() << std::endl;

587 588
        EXPECT_LE(cvtest::norm(rvec_ground_truth, rvec_est, NORM_INF), 1e-6);
        EXPECT_LE(cvtest::norm(tvec_ground_truth, tvec_est, NORM_INF), 1e-6);
589 590
    }
}
591 592

}} // namespace