test_estimaterigid.cpp 5.64 KB
Newer Older
wester committed
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*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"

#include <string>
#include <iostream>
#include <fstream>
#include <iterator>
#include <limits>
#include <numeric>

using namespace cv;
using namespace std;

class CV_RigidTransform_Test : public cvtest::BaseTest
{
public:
    CV_RigidTransform_Test();
    ~CV_RigidTransform_Test();
protected:
    void run(int);

    bool testNPoints(int);
    bool testImage();
};

CV_RigidTransform_Test::CV_RigidTransform_Test()
{
}
CV_RigidTransform_Test::~CV_RigidTransform_Test() {}

struct WrapAff2D
{
    const double *F;
    WrapAff2D(const Mat& aff) : F(aff.ptr<double>()) {}
    Point2f operator()(const Point2f& p)
    {
wester committed
78 79
        return Point2d( p.x * F[0] + p.y * F[1] +  F[2],
                        p.x * F[3] + p.y * F[4] +  F[5]);
wester committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }
};

bool CV_RigidTransform_Test::testNPoints(int from)
{
    cv::RNG rng = ts->get_rng();

    int progress = 0;
    int k, ntests = 10000;

    for( k = from; k < ntests; k++ )
    {
        ts->update_context( this, k, true );
        progress = update_progress(progress, k, ntests, 0);

        Mat aff(2, 3, CV_64F);
wester committed
96
        rng.fill(aff, CV_RAND_UNI, Scalar(-2), Scalar(2));
wester committed
97 98 99 100 101 102

        int n = (unsigned)rng % 100 + 10;

        Mat fpts(1, n, CV_32FC2);
        Mat tpts(1, n, CV_32FC2);

wester committed
103
        rng.fill(fpts, CV_RAND_UNI, Scalar(0,0), Scalar(10,10));
wester committed
104 105 106
        transform(fpts.ptr<Point2f>(), fpts.ptr<Point2f>() + n, tpts.ptr<Point2f>(), WrapAff2D(aff));

        Mat noise(1, n, CV_32FC2);
wester committed
107
        rng.fill(noise, CV_RAND_NORMAL, Scalar::all(0), Scalar::all(0.001*(n<=7 ? 0 : n <= 30 ? 1 : 10)));
wester committed
108 109 110 111
        tpts += noise;

        Mat aff_est = estimateRigidTransform(fpts, tpts, true);

wester committed
112 113
        double thres = 0.1*norm(aff);
        double d = norm(aff_est, aff, NORM_L2);
wester committed
114 115 116 117 118 119 120 121 122
        if (d > thres)
        {
            double dB=0, nB=0;
            if (n <= 4)
            {
                Mat A = fpts.reshape(1, 3);
                Mat B = A - repeat(A.row(0), 3, 1), Bt = B.t();
                B = Bt*B;
                dB = cv::determinant(B);
wester committed
123
                nB = norm(B);
wester committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                if( fabs(dB) < 0.01*nB )
                    continue;
            }
            ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
            ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres, d );
            return false;
        }
    }
    return true;
}

bool CV_RigidTransform_Test::testImage()
{
    Mat img;
    Mat testImg = imread( string(ts->get_data_path()) + "shared/graffiti.png", 1);
    if (testImg.empty())
    {
       ts->printf( ts->LOG, "test image can not be read");
       ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
       return false;
    }
    pyrDown(testImg, img);

    Mat aff = cv::getRotationMatrix2D(Point(img.cols/2, img.rows/2), 1, 0.99);
    aff.ptr<double>()[2]+=3;
    aff.ptr<double>()[5]+=3;

    Mat rotated;
    warpAffine(img, rotated, aff, img.size());

    Mat aff_est = estimateRigidTransform(img, rotated, true);

wester committed
156 157
    const double thres = 0.03;
    if (norm(aff_est, aff, NORM_INF) > thres)
wester committed
158 159 160
    {
        ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
        ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres,
wester committed
161
            norm(aff_est, aff, NORM_INF) );
wester committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        return false;
    }

    return true;
}

void CV_RigidTransform_Test::run( int start_from )
{
    cvtest::DefaultRngAuto dra;

    if (!testNPoints(start_from))
        return;

    if (!testImage())
        return;

    ts->set_failed_test_info(cvtest::TS::OK);
}

TEST(Video_RigidFlow, accuracy) { CV_RigidTransform_Test test; test.safe_run(); }