test_solvepnp_ransac.cpp 10.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 46 47

#ifdef HAVE_TBB
#include "tbb/task_scheduler_init.h"
#endif
48 49 50 51 52 53 54

using namespace cv;
using namespace std;

class CV_solvePnPRansac_Test : public cvtest::BaseTest
{
public:
Alexander Shishkov's avatar
Alexander Shishkov committed
55 56
    CV_solvePnPRansac_Test()
    {
edgarriba's avatar
edgarriba committed
57 58 59 60
        eps[SOLVEPNP_ITERATIVE] = 1.0e-2;
        eps[SOLVEPNP_EPNP] = 1.0e-2;
        eps[SOLVEPNP_P3P] = 1.0e-2;
        eps[SOLVEPNP_DLS] = 1.0e-2;
61
        eps[SOLVEPNP_UPNP] = 1.0e-2;
Alexander Shishkov's avatar
Alexander Shishkov committed
62 63
        totalTestsCount = 10;
    }
64 65 66
    ~CV_solvePnPRansac_Test() {}
protected:
    void generate3DPointCloud(vector<Point3f>& points, Point3f pmin = Point3f(-1,
Alexander Shishkov's avatar
Alexander Shishkov committed
67
        -1, 5), Point3f pmax = Point3f(1, 1, 10))
68
    {
Alexander Shishkov's avatar
Alexander Shishkov committed
69 70 71 72 73 74 75 76 77 78 79
        const Point3f delta = pmax - pmin;
        for (size_t i = 0; i < points.size(); i++)
        {
            Point3f p(float(rand()) / RAND_MAX, float(rand()) / RAND_MAX,
                float(rand()) / RAND_MAX);
            p.x *= delta.x;
            p.y *= delta.y;
            p.z *= delta.z;
            p = p + pmin;
            points[i] = p;
        }
80 81
    }

Alexander Shishkov's avatar
Alexander Shishkov committed
82
    void generateCameraMatrix(Mat& cameraMatrix, RNG& rng)
83
    {
Alexander Shishkov's avatar
Alexander Shishkov committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        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);
    }
101

Alexander Shishkov's avatar
Alexander Shishkov committed
102 103 104 105 106 107 108 109 110 111 112 113 114
    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
115
    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
116 117 118 119 120 121
    {
        Mat rvec, tvec;
        vector<int> inliers;
        Mat trueRvec, trueTvec;
        Mat intrinsics, distCoeffs;
        generateCameraMatrix(intrinsics, rng);
122
        if (method == 4) intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
Alexander Shishkov's avatar
Alexander Shishkov committed
123 124 125 126 127 128 129 130 131 132
        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++)
133 134 135
        {
            if (i % 20 == 0)
            {
136
                projectedPoints[i] = projectedPoints[rng.uniform(0,(int)points.size()-1)];
137 138
            }
        }
139

Alexander Shishkov's avatar
Alexander Shishkov committed
140
        solvePnPRansac(points, projectedPoints, intrinsics, distCoeffs, rvec, tvec,
edgarriba's avatar
edgarriba committed
141
            false, 500, 0.5, 0.99, inliers, method);
142

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

Alexander Shishkov's avatar
Alexander Shishkov committed
145
        double rvecDiff = norm(rvec-trueRvec), tvecDiff = norm(tvec-trueTvec);
Andrey Kamaev's avatar
Andrey Kamaev committed
146
        isTestSuccess = isTestSuccess && rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
Alexander Shishkov's avatar
Alexander Shishkov committed
147 148 149 150
        double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;
        //cout << error << " " << inliers.size() << " " << eps[method] << endl;
        if (error > maxError)
            maxError = error;
151

Alexander Shishkov's avatar
Alexander Shishkov committed
152 153
        return isTestSuccess;
    }
154

Alexander Shishkov's avatar
Alexander Shishkov committed
155 156
    void run(int)
    {
Andrey Kamaev's avatar
Andrey Kamaev committed
157
        ts->set_failed_test_info(cvtest::TS::OK);
158

edgarriba's avatar
edgarriba committed
159
        vector<Point3f> points, points_dls;
edgarriba's avatar
edgarriba committed
160
        const int pointsCount = 500;
Alexander Shishkov's avatar
Alexander Shishkov committed
161 162 163
        points.resize(pointsCount);
        generate3DPointCloud(points);

164
        const int methodsCount = 5;
Andrey Kamaev's avatar
Andrey Kamaev committed
165
        RNG rng = ts->get_rng();
Alexander Shishkov's avatar
Alexander Shishkov committed
166 167 168 169 170


        for (int mode = 0; mode < 2; mode++)
        {
            for (int method = 0; method < methodsCount; method++)
171
            {
Alexander Shishkov's avatar
Alexander Shishkov committed
172 173 174 175
                double maxError = 0;
                int successfulTestsCount = 0;
                for (int testIndex = 0; testIndex < totalTestsCount; testIndex++)
                {
edgarriba's avatar
edgarriba committed
176
                    if (runTest(rng, mode, method, points, eps, maxError))
Alexander Shishkov's avatar
Alexander Shishkov committed
177 178 179 180 181
                        successfulTestsCount++;
                }
                //cout <<  maxError << " " << successfulTestsCount << endl;
                if (successfulTestsCount < 0.7*totalTestsCount)
                {
Andrey Kamaev's avatar
Andrey Kamaev committed
182
                    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
183
                        method, totalTestsCount - successfulTestsCount, totalTestsCount, maxError, mode);
Andrey Kamaev's avatar
Andrey Kamaev committed
184
                    ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
Alexander Shishkov's avatar
Alexander Shishkov committed
185
                }
186 187
            }
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
188
    }
189
    double eps[5];
Alexander Shishkov's avatar
Alexander Shishkov committed
190 191
    int totalTestsCount;
};
192

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

    ~CV_solvePnP_Test() {}
protected:
Andrey Kamaev's avatar
Andrey Kamaev committed
208
    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
209 210 211 212 213
    {
        Mat rvec, tvec;
        Mat trueRvec, trueTvec;
        Mat intrinsics, distCoeffs;
        generateCameraMatrix(intrinsics, rng);
214
        if (method == 4) intrinsics.at<double>(1,1) = intrinsics.at<double>(0,0);
Alexander Shishkov's avatar
Alexander Shishkov committed
215 216 217 218 219 220 221 222
        if (mode == 0)
            distCoeffs = Mat::zeros(4, 1, CV_64FC1);
        else
            generateDistCoeffs(distCoeffs, rng);
        generatePose(trueRvec, trueTvec, rng);

        std::vector<Point3f> opoints;
        if (method == 2)
223
        {
Alexander Shishkov's avatar
Alexander Shishkov committed
224
            opoints = std::vector<Point3f>(points.begin(), points.begin()+4);
225
        }
edgarriba's avatar
edgarriba committed
226 227
        else if(method == 3)
        {
edgarriba's avatar
edgarriba committed
228
            opoints = std::vector<Point3f>(points.begin(), points.begin()+50);
edgarriba's avatar
edgarriba committed
229
        }
Alexander Shishkov's avatar
Alexander Shishkov committed
230 231 232 233 234 235 236 237 238 239 240
        else
            opoints = points;

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

        solvePnP(opoints, projectedPoints, intrinsics, distCoeffs, rvec, tvec,
            false, method);

        double rvecDiff = norm(rvec-trueRvec), tvecDiff = norm(tvec-trueTvec);
Andrey Kamaev's avatar
Andrey Kamaev committed
241
        bool isTestSuccess = rvecDiff < epsilon[method] && tvecDiff < epsilon[method];
Alexander Shishkov's avatar
Alexander Shishkov committed
242 243 244 245 246 247

        double error = rvecDiff > tvecDiff ? rvecDiff : tvecDiff;
        if (error > maxError)
            maxError = error;

        return isTestSuccess;
248 249 250
    }
};

edgarriba's avatar
edgarriba committed
251
TEST(Calib3d_SolvePnPRansac, accuracy) { CV_solvePnPRansac_Test test; test.safe_run(); }
Alexander Shishkov's avatar
Alexander Shishkov committed
252
TEST(Calib3d_SolvePnP, accuracy) { CV_solvePnP_Test test; test.safe_run(); }
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

#ifdef HAVE_TBB

TEST(DISABLED_Calib3d_SolvePnPRansac, concurrency)
{
    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;

    {
Ilya Lavrenov's avatar
Ilya Lavrenov committed
286
        // limit concurrency to get deterministic result
287
        cv::theRNG().state = 20121010;
288
        tbb::task_scheduler_init one_thread(1);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
        solvePnPRansac(object, image, camera_mat, dist_coef, rvec1, tvec1);
    }

    if(1)
    {
        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
        cv::theRNG().state = 20121010;
307
        tbb::task_scheduler_init one_thread(1);
308 309 310
        solvePnPRansac(object, image, camera_mat, dist_coef, rvec2, tvec2);
    }

311 312
    double rnorm = cvtest::norm(rvec1, rvec2, NORM_INF);
    double tnorm = cvtest::norm(tvec1, tvec2, NORM_INF);
313 314 315 316 317

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

}
318
#endif