test_reg.cpp 8.08 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 44
/*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.
// Copyright (C) 2013, Alfonso Sanchez-Beato, 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"
45 46

namespace opencv_test { namespace {
47

48 49
#define REG_DEBUG_OUTPUT 0

50 51 52 53

class RegTest : public testing::Test
{
public:
54
    void loadImage(int dstDataType = CV_32FC3);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    void testShift();
    void testEuclidean();
    void testSimilarity();
    void testAffine();
    void testProjective();
private:
    Mat img1;
};

void RegTest::testShift()
{
    Mat img2;

    // Warp original image
    Vec<double, 2> shift(5., 5.);
    MapShift mapTest(shift);
    mapTest.warp(img1, img2);

    // Register
75
    Ptr<Mapper> mapper = makePtr<MapperGradShift>();
76
    MapperPyramid mappPyr(mapper);
77
    Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
78 79

    // Print result
80
    Ptr<MapShift> mapShift = MapTypeCaster::toShift(mapPtr);
81
#if REG_DEBUG_OUTPUT
82 83 84
    cout << endl << "--- Testing shift mapper ---" << endl;
    cout << Mat(shift) << endl;
    cout << Mat(mapShift->getShift()) << endl;
85
#endif
86 87
    // Check accuracy
    Ptr<Map> mapInv(mapShift->inverseMap());
88
    mapTest.compose(mapInv);
89
    double shNorm = cv::norm(mapTest.getShift());
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    EXPECT_LE(shNorm, 0.1);
}

void RegTest::testEuclidean()
{
    Mat img2;

    // Warp original image
    double theta = 3*CV_PI/180;
    double cosT = cos(theta);
    double sinT = sin(theta);
    Matx<double, 2, 2> linTr(cosT, -sinT, sinT, cosT);
    Vec<double, 2> shift(5., 5.);
    MapAffine mapTest(linTr, shift);
    mapTest.warp(img1, img2);

    // Register
107
    Ptr<Mapper> mapper = makePtr<MapperGradEuclid>();
108
    MapperPyramid mappPyr(mapper);
109
    Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
110 111

    // Print result
112
    Ptr<MapAffine> mapAff = MapTypeCaster::toAffine(mapPtr);
113
#if REG_DEBUG_OUTPUT
114 115 116 117 118
    cout << endl << "--- Testing Euclidean mapper ---" << endl;
    cout << Mat(linTr) << endl;
    cout << Mat(shift) << endl;
    cout << Mat(mapAff->getLinTr()) << endl;
    cout << Mat(mapAff->getShift()) << endl;
119
#endif
120 121
    // Check accuracy
    Ptr<Map> mapInv(mapAff->inverseMap());
122
    mapTest.compose(mapInv);
123
    double shNorm = cv::norm(mapTest.getShift());
124
    EXPECT_LE(shNorm, 0.1);
125
    double linTrNorm = cv::norm(mapTest.getLinTr());
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    EXPECT_LE(linTrNorm, sqrt(2.) + 0.01);
    EXPECT_GE(linTrNorm, sqrt(2.) - 0.01);
}

void RegTest::testSimilarity()
{
    Mat img2;

    // Warp original image
    double theta = 3*CV_PI/180;
    double scale = 0.95;
    double a = scale*cos(theta);
    double b = scale*sin(theta);
    Matx<double, 2, 2> linTr(a, -b, b, a);
    Vec<double, 2> shift(5., 5.);
    MapAffine mapTest(linTr, shift);
    mapTest.warp(img1, img2);

    // Register
145
    Ptr<Mapper> mapper = makePtr<MapperGradSimilar>();
146
    MapperPyramid mappPyr(mapper);
147
    Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
148 149

    // Print result
150
    Ptr<MapAffine> mapAff = MapTypeCaster::toAffine(mapPtr);
151
#if REG_DEBUG_OUTPUT
152 153 154 155 156
    cout << endl << "--- Testing similarity mapper ---" << endl;
    cout << Mat(linTr) << endl;
    cout << Mat(shift) << endl;
    cout << Mat(mapAff->getLinTr()) << endl;
    cout << Mat(mapAff->getShift()) << endl;
157
#endif
158 159 160

    // Check accuracy
    Ptr<Map> mapInv(mapAff->inverseMap());
161
    mapTest.compose(mapInv);
162
    double shNorm = cv::norm(mapTest.getShift());
163
    EXPECT_LE(shNorm, 0.1);
164
    double linTrNorm = cv::norm(mapTest.getLinTr());
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    EXPECT_LE(linTrNorm, sqrt(2.) + 0.01);
    EXPECT_GE(linTrNorm, sqrt(2.) - 0.01);
}

void RegTest::testAffine()
{
    Mat img2;

    // Warp original image
    Matx<double, 2, 2> linTr(1., 0.1, -0.01, 1.);
    Vec<double, 2> shift(1., 1.);
    MapAffine mapTest(linTr, shift);
    mapTest.warp(img1, img2);

    // Register
180
    Ptr<Mapper> mapper = makePtr<MapperGradAffine>();
181
    MapperPyramid mappPyr(mapper);
182
    Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
183 184

    // Print result
185
    Ptr<MapAffine> mapAff = MapTypeCaster::toAffine(mapPtr);
186
#if REG_DEBUG_OUTPUT
187 188 189 190 191
    cout << endl << "--- Testing affine mapper ---" << endl;
    cout << Mat(linTr) << endl;
    cout << Mat(shift) << endl;
    cout << Mat(mapAff->getLinTr()) << endl;
    cout << Mat(mapAff->getShift()) << endl;
192
#endif
193 194 195

    // Check accuracy
    Ptr<Map> mapInv(mapAff->inverseMap());
196
    mapTest.compose(mapInv);
197
    double shNorm = cv::norm(mapTest.getShift());
198
    EXPECT_LE(shNorm, 0.1);
199
    double linTrNorm = cv::norm(mapTest.getLinTr());
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    EXPECT_LE(linTrNorm, sqrt(2.) + 0.01);
    EXPECT_GE(linTrNorm, sqrt(2.) - 0.01);
}


void RegTest::testProjective()
{
    Mat img2;

    // Warp original image
    Matx<double, 3, 3> projTr(1., 0., 0., 0., 1., 0., 0.0001, 0.0001, 1);
    MapProjec mapTest(projTr);
    mapTest.warp(img1, img2);

    // Register
215
    Ptr<Mapper> mapper = makePtr<MapperGradProj>();
216
    MapperPyramid mappPyr(mapper);
217
    Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
218 219

    // Print result
220
    Ptr<MapProjec> mapProj = MapTypeCaster::toProjec(mapPtr);
221
    mapProj->normalize();
222
#if REG_DEBUG_OUTPUT
223 224 225
    cout << endl << "--- Testing projective transformation mapper ---" << endl;
    cout << Mat(projTr) << endl;
    cout << Mat(mapProj->getProjTr()) << endl;
226
#endif
227 228 229

    // Check accuracy
    Ptr<Map> mapInv(mapProj->inverseMap());
230
    mapTest.compose(mapInv);
231
    double projNorm = cv::norm(mapTest.getProjTr());
232 233 234 235
    EXPECT_LE(projNorm, sqrt(3.) + 0.01);
    EXPECT_GE(projNorm, sqrt(3.) - 0.01);
}

236
void RegTest::loadImage(int dstDataType)
237
{
238
    const string imageName = cvtest::TS::ptr()->get_data_path() + "reg/home.png";
239 240

    img1 = imread(imageName, -1);
241 242
    ASSERT_TRUE(!img1.empty());
    img1.convertTo(img1, dstDataType);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
}


TEST_F(RegTest, shift)
{
    loadImage();
    testShift();
}

TEST_F(RegTest, euclidean)
{
    loadImage();
    testEuclidean();
}

TEST_F(RegTest, similarity)
{
    loadImage();
    testSimilarity();
}

TEST_F(RegTest, affine)
{
    loadImage();
    testAffine();
}

TEST_F(RegTest, projective)
{
    loadImage();
    testProjective();
}
275 276 277 278 279 280 281 282 283 284 285 286

TEST_F(RegTest, projective_dt64fc3)
{
    loadImage(CV_64FC3);
    testProjective();
}

TEST_F(RegTest, projective_dt64fc1)
{
    loadImage(CV_64FC1);
    testProjective();
}
287 288

}} // namespace