-
Notifications
You must be signed in to change notification settings - Fork 0
/
CThinPlateSpline.cpp
executable file
·330 lines (269 loc) · 7.67 KB
/
CThinPlateSpline.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
/*
* CThinPlateSpline.cpp
*
* Created on: 24.01.2010
* Author: schmiedm
*/
#include <opencv2/opencv.hpp>
#include <vector>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include "CThinPlateSpline.h"
using namespace cv;
CThinPlateSpline::CThinPlateSpline() {
FLAG_MAPS_FORWARD_SET = false;
FLAG_MAPS_BACK_WARP_SET = false;
FLAG_COEFFS_BACK_WARP_SET = false;
FLAG_COEFFS_FORWARD_WARP_SET = false;
}
CThinPlateSpline::CThinPlateSpline(const std::vector<Point>& pS, const std::vector<Point>& pD)
{
if(pS.size() == pS.size())
{
pSrc = pS;
pDst = pD;
}
FLAG_MAPS_FORWARD_SET = false;
FLAG_MAPS_BACK_WARP_SET = false;
FLAG_COEFFS_BACK_WARP_SET = false;
FLAG_COEFFS_FORWARD_WARP_SET = false;
}
CThinPlateSpline::~CThinPlateSpline() {
}
void CThinPlateSpline::addCorrespondence(const Point& pS, const Point& pD)
{
pSrc.push_back(pS);
pDst.push_back(pD);
// tell the class to recompute the coefficients if neccesarry
FLAG_COEFFS_BACK_WARP_SET = false;
FLAG_COEFFS_FORWARD_WARP_SET = false;
FLAG_MAPS_FORWARD_SET = false;
FLAG_MAPS_BACK_WARP_SET = false;
}
void CThinPlateSpline::setCorrespondences(const std::vector<Point>& pS, const std::vector<Point>& pD)
{
pSrc = pS;
pDst = pD;
// tell the class to recompute the coefficients if neccesarry
FLAG_COEFFS_BACK_WARP_SET = false;
FLAG_COEFFS_FORWARD_WARP_SET = false;
FLAG_MAPS_FORWARD_SET = false;
FLAG_MAPS_BACK_WARP_SET = false;
}
double CThinPlateSpline::fktU(const Point& p1, const Point& p2)
{
double r = pow(((double)p1.x - (double)p2.x), 2) + pow(((double)p1.y - (double)p2.y), 2);
if (r == 0)
return 0.0;
else
{
r = sqrt(r); // vector length
double r2 = pow(r, 2);
return (r2 * log(r2));
}
}
void CThinPlateSpline::computeSplineCoeffs(std::vector<Point>& iPIn, std::vector<Point>& iiPIn, float lambda,const TPS_INTERPOLATION tpsInter)
{
std::vector<Point>* iP = NULL;
std::vector<Point>* iiP = NULL;
if(tpsInter == FORWARD_WARP)
{
iP = &iPIn;
iiP = &iiPIn;
// keep information which coefficients are set
FLAG_COEFFS_BACK_WARP_SET = true;
FLAG_COEFFS_FORWARD_WARP_SET = false;
}
else if(tpsInter == BACK_WARP)
{
iP = &iiPIn;
iiP = &iPIn;
// keep information which coefficients are set
FLAG_COEFFS_BACK_WARP_SET = false;
FLAG_COEFFS_FORWARD_WARP_SET = true;
}
//get number of corresponding points
int dim = 2;
int n = iP->size();
//Initialize mathematical datastructures
Mat_<float> V(dim,n+dim+1,0.0);
Mat_<float> P(n,dim+1,1.0);
Mat_<float> K = (K.eye(n,n)*lambda);
Mat_<float> L(n+dim+1,n+dim+1,0.0);
// fill up K und P matrix
std::vector<Point>::iterator itY;
std::vector<Point>::iterator itX;
int y = 0;
for (itY = iP->begin(); itY != iP->end(); ++itY, y++) {
int x = 0;
for (itX = iP->begin(); itX != iP->end(); ++itX, x++) {
if (x != y) {
K(y, x) = (float)fktU(*itY, *itX);
}
}
P(y,1) = (float)itY->y;
P(y,2) = (float)itY->x;
}
Mat Pt;
transpose(P,Pt);
// insert K into L
Rect range = Rect(0, 0, n, n);
Mat Lr(L,range);
K.copyTo(Lr);
// insert P into L
range = Rect(n, 0, dim + 1, n);
Lr = Mat(L,range);
P.copyTo(Lr);
// insert Pt into L
range = Rect(0,n,n,dim+1);
Lr = Mat(L,range);
Pt.copyTo(Lr);
// fill V array
std::vector<Point>::iterator it;
int u = 0;
for(it = iiP->begin(); it != iiP->end(); ++it)
{
V(0,u) = (float)it->y;
V(1,u) = (float)it->x;
u++;
}
// transpose V
Mat Vt;
transpose(V,Vt);
cMatrix = Mat_<float>(n+dim+1,dim,0.0);
// invert L
Mat invL;
invert(L,invL,DECOMP_LU);
//multiply(invL,Vt,cMatrix);
cMatrix = invL * Vt;
//compensate for rounding errors
for(int row = 0; row < cMatrix.rows; row++)
{
for(int col = 0; col < cMatrix.cols; col++)
{
double v = cMatrix(row,col);
if(v > (-1.0e-006) && v < (1.0e-006) )
{
cMatrix(row,col) = 0.0;
}
}
}
}
Point CThinPlateSpline::interpolate_forward_(const Point& p)
{
Point2f interP;
std::vector<Point>* pList = &pSrc;
int k1 = cMatrix.rows - 3;
int kx = cMatrix.rows - 2;
int ky = cMatrix.rows - 1;
double a1 = 0, ax = 0, ay = 0, cTmp = 0, uTmp = 0, tmp_i = 0, tmp_ii = 0;
for (int i = 0; i < 2; i++) {
a1 = cMatrix(k1,i);
ax = cMatrix(kx,i);
ay = cMatrix(ky,i);
tmp_i = a1 + ax * p.y + ay * p.x;
tmp_ii = 0;
for (int j = 0; j < (int)pSrc.size(); j++) {
cTmp = cMatrix(j,i);
uTmp = fktU( (*pList)[j], p);
tmp_ii = tmp_ii + (cTmp * uTmp);
}
if (i == 0) {
interP.y = (float)(tmp_i + tmp_ii);
}
if (i == 1) {
interP.x = (float)(tmp_i + tmp_ii);
}
}
return interP;
}
Point CThinPlateSpline::interpolate_back_(const Point& p)
{
Point2f interP;
std::vector<Point>* pList = &pDst;
int k1 = cMatrix.rows - 3;
int kx = cMatrix.rows - 2;
int ky = cMatrix.rows - 1;
double a1 = 0, ax = 0, ay = 0, cTmp = 0, uTmp = 0, tmp_i = 0, tmp_ii = 0;
for (int i = 0; i < 2; i++) {
a1 = cMatrix(k1,i);
ax = cMatrix(kx,i);
ay = cMatrix(ky,i);
tmp_i = a1 + ax * p.y + ay * p.x;
tmp_ii = 0;
for (int j = 0; j < (int)pSrc.size(); j++) {
cTmp = cMatrix(j,i);
uTmp = fktU( (*pList)[j], p);
tmp_ii = tmp_ii + (cTmp * uTmp);
}
if (i == 0) {
interP.y = (float)(tmp_i + tmp_ii);
}
if (i == 1) {
interP.x = (float)(tmp_i + tmp_ii);
}
}
return interP;
}
Point CThinPlateSpline::interpolate(const Point& p, const TPS_INTERPOLATION tpsInter)
{
if(tpsInter == BACK_WARP)
{
return interpolate_back_(p);
}
else if(tpsInter == FORWARD_WARP)
{
return interpolate_forward_(p);
}
else
{
return interpolate_back_(p);
}
}
void CThinPlateSpline::warpImage(const Mat& src, Mat& dst, float lambda, const int interpolation,const TPS_INTERPOLATION tpsInter)
{
Size size = src.size();
dst = Mat(size,src.type());
// only compute the coefficients new if they weren't already computed
// or there had been changes to the points
if(tpsInter == BACK_WARP && !FLAG_COEFFS_BACK_WARP_SET)
{
computeSplineCoeffs(pSrc,pDst,lambda,tpsInter);
}
else if(tpsInter == FORWARD_WARP && !FLAG_COEFFS_FORWARD_WARP_SET)
{
computeSplineCoeffs(pSrc,pDst,lambda,tpsInter);
}
computeMaps(size,mapx,mapy);
remap(src,dst,mapx,mapy,interpolation, 1);
}
void CThinPlateSpline::getMaps(Mat& mx, Mat& my)
{
mx = mapx;
my = mapy;
}
void CThinPlateSpline::computeMaps(const Size& dstSize, Mat_<float>& mx, Mat_<float>& my,const TPS_INTERPOLATION tpsInter)
{
mx = Mat_<float>(dstSize);
my = Mat_<float>(dstSize);
Point p(0, 0);
Point_<float> intP(0, 0);
for (int row = 0; row < dstSize.height; row++) {
for (int col = 0; col < dstSize.width; col++) {
p = Point(col, row);
intP = interpolate(p,tpsInter);
mx(row, col) = intP.x;
my(row, col) = intP.y;
}
}
if(tpsInter == BACK_WARP)
{
FLAG_MAPS_BACK_WARP_SET = true;
FLAG_MAPS_FORWARD_SET = false;
}
else if(tpsInter == FORWARD_WARP)
{
FLAG_MAPS_BACK_WARP_SET = false;
FLAG_MAPS_FORWARD_SET = true;
}
}