-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathalgorithms.cpp
345 lines (246 loc) · 11.7 KB
/
algorithms.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
This file is part of Image Alignment.
Copyright Christoph Heindl 2015
Image Alignment is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Image Alignment is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Image Alignment. If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch.hpp"
#include <imagealign/forward_additive.h>
#include <imagealign/forward_compositional.h>
#include <imagealign/inverse_compositional.h>
#include <imagealign/warp_image.h>
#include <iostream>
template< class A, class W >
W testAlgorithm(cv::Mat tpl, cv::Mat target, W w, int levels, const typename W::Traits::ParamType &expected, double tolerance = 0.01)
{
typedef typename W::Traits::ScalarType S;
A a;
a.prepare(tpl, target, w, levels);
a.align(w, 100, S(0));
REQUIRE(cv::norm(w.parameters() - expected, cv::NORM_L1) == Catch::Detail::Approx(0).epsilon(tolerance));
return w;
}
TEST_CASE("algorithm-translation")
{
namespace ia = imagealign;
cv::Mat target(100, 100, CV_8UC1);
cv::randu(target, cv::Scalar::all(0), cv::Scalar::all(255));
cv::blur(target, target, cv::Size(5,5));
cv::Mat tmpl = target(cv::Rect(20, 20, 10, 10));
// Floating point
{
typedef ia::WarpTranslationF W;
W::Traits::ParamType expected(20, 20);
W w;
w.setParameters(W::Traits::ParamType(18, 18));
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected);
}
// Double precision floating point
{
typedef ia::WarpTranslationD W;
W::Traits::ParamType expected(20, 20);
W w;
w.setParameters(W::Traits::ParamType(18, 18));
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected);
}
}
TEST_CASE("algorithm-euclidean")
{
namespace ia = imagealign;
cv::Mat target(100, 100, CV_8UC1);
cv::randu(target, cv::Scalar::all(0), cv::Scalar::all(255));
cv::blur(target, target, cv::Size(5,5));
cv::Mat tmpl;
// Floating point
{
typedef ia::WarpEuclideanF W;
W::Traits::ParamType expected(10.f, 15.f, 0.18f);
W::Traits::ParamType noise(1.5f, -1.2f, 0.02f);
W w;
w.setParameters(expected);
ia::warpImage<uchar, ia::SAMPLE_BILINEAR>(target, tmpl, cv::Size(20, 20), w);
w.setParameters(w.parameters() + noise);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected);
}
// Double precision floating point
{
typedef ia::WarpEuclideanD W;
W::Traits::ParamType expected(10.f, 15.f, 0.18f);
W::Traits::ParamType noise(1.5f, -1.2f, 0.02f);
W w;
w.setParameters(expected);
ia::warpImage<uchar, ia::SAMPLE_BILINEAR>(target, tmpl, cv::Size(20, 20), w);
w.setParameters(w.parameters() + noise);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected);
}
}
TEST_CASE("algorithm-similarity")
{
namespace ia = imagealign;
cv::Mat target(100, 100, CV_8UC1);
cv::randu(target, cv::Scalar::all(0), cv::Scalar::all(255));
cv::blur(target, target, cv::Size(5,5));
cv::Mat tmpl;
// Floating point
{
typedef ia::WarpSimilarityF W;
W::Traits::ParamType expectedCanonical(10.f, 15.f, 0.18f, 1.f);
W::Traits::ParamType noiseCanonical(0.8f, -0.7f, 0.02f, 0.01f);
W w;
w.setParametersInCanonicalRepresentation(expectedCanonical);
ia::warpImage<uchar, ia::SAMPLE_BILINEAR>(target, tmpl, cv::Size(20, 20), w);
W::Traits::ParamType expected = w.parameters();
w.setParametersInCanonicalRepresentation(w.parametersInCanonicalRepresentation() + noiseCanonical);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected, 0.02);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected, 0.02);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected, 0.02);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected, 0.02);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected, 0.02);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected, 0.02);
}
// Double precision floating point
{
typedef ia::WarpSimilarityD W;
W::Traits::ParamType expectedCanonical(10.f, 15.f, 0.18f, 1.f);
W::Traits::ParamType noiseCanonical(0.8f, -0.7f, 0.02f, 0.01f);
W w;
w.setParametersInCanonicalRepresentation(expectedCanonical);
ia::warpImage<uchar, ia::SAMPLE_BILINEAR>(target, tmpl, cv::Size(20, 20), w);
W::Traits::ParamType expected = w.parameters();
w.setParametersInCanonicalRepresentation(w.parametersInCanonicalRepresentation() + noiseCanonical);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected, 0.02);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected, 0.02);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected, 0.02);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected, 0.02);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected, 0.02);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected, 0.02);
}
}
// Test dummy dynamic warp;
namespace ia = imagealign;
namespace imagealign {
const int WARP_TRANSLATION_DYAMIC = 255;
template<class Scalar>
struct WarpTraits<WARP_TRANSLATION_DYAMIC, Scalar> : WarpTraitsForRunTimeKnownParameterCount<WARP_TRANSLATION_DYAMIC, Scalar> {};
template<class Scalar>
class Warp<WARP_TRANSLATION_DYAMIC, Scalar> {
public:
typedef WarpTraits<WARP_TRANSLATION_DYAMIC, Scalar> Traits;
Warp() {
_m.create(2, 1);
setIdentity();
}
Warp(const Warp<WARP_TRANSLATION_DYAMIC, Scalar> &other) {
_m = other._m.clone();
}
int numParameters() const {
return 2;
}
void setIdentity() {
_m.setTo(0);
}
Warp<WARP_TRANSLATION_DYAMIC, Scalar> scaled(int numLevels) const
{
Scalar s = std::pow(Scalar(2), numLevels);
Warp<WARP_TRANSLATION_DYAMIC, Scalar> ws(*this);
ws._m *= s;
return ws;
}
typename Traits::PointType operator()(const typename Traits::PointType &p) const {
return typename Traits::PointType(p(0) + _m(0, 0), p(1) + _m(1, 0));
}
typename Traits::JacobianType jacobian(const typename Traits::PointType &p) const {
return Traits::JacobianType::eye(2, 2, CV_MAKETYPE(cv::DataType<Scalar>::depth, 1));
}
void updateInverseCompositional(const typename Traits::ParamType &delta) {
_m -= delta;
}
void updateForwardAdditive(const typename Traits::ParamType &delta) {
_m += delta;
}
void updateForwardCompositional(const typename Traits::ParamType &delta) {
_m += delta;
}
// Helper functions
void setParameters(const typename Traits::ParamType &p) {
p.copyTo(_m);
}
typename Traits::ParamType parameters() const {
return _m.clone();
}
private:
cv::Mat_<Scalar> _m;
};
}
TEST_CASE("algorithm-dynamic-warp")
{
namespace ia = imagealign;
cv::Mat target(100, 100, CV_8UC1);
cv::randu(target, cv::Scalar::all(0), cv::Scalar::all(255));
cv::blur(target, target, cv::Size(5,5));
cv::Mat tmpl = target(cv::Rect(20, 20, 10, 10));
// Floating point
{
typedef ia::Warp<ia::WARP_TRANSLATION_DYAMIC, float> W;
W::Traits::ParamType expected(2, 1, CV_32FC1);
expected.at<float>(0, 0) = 20;
expected.at<float>(1, 0) = 20;
W::Traits::ParamType noisy(2, 1, CV_32FC1);
noisy.at<float>(0, 0) = 19;
noisy.at<float>(1, 0) = 19;
W w;
w.setParameters(noisy);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected);
}
// Double precision floating point
{
typedef ia::Warp<ia::WARP_TRANSLATION_DYAMIC, double> W;
W::Traits::ParamType expected(2, 1, CV_64FC1);
expected.at<double>(0, 0) = 20;
expected.at<double>(1, 0) = 20;
W::Traits::ParamType noisy(2, 1, CV_64FC1);
noisy.at<double>(0, 0) = 19;
noisy.at<double>(1, 0) = 19;
W w;
w.setParameters(noisy);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardAdditive<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignInverseCompositional<W> >(tmpl, target, w, 2, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 1, expected);
testAlgorithm< ia::AlignForwardCompositional<W> >(tmpl, target, w, 2, expected);
}
}