[1442bb]: BondBall.c Maximize Restore History

Download this file

BondBall.c    385 lines (324 with data), 12.0 kB

  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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* autopano-sift, Automatic panorama image creation
* Copyright (C) 2004 -- Sebastian Nowozin
*
* This program is free software released under the GNU General Public
* License, which is included in this software package (doc/LICENSE).
*/
/* BondBall.cs
*
* Generic preliminary position finding algorithm for panoramic imaging.
*
* (C) Copyright 2004 -- Sebastian Nowozin (nowozin@cs.tu-berlin.de)
*
* Any kind of panorama made from small angle input pictures (ie. no special
* fisheye or wideangle lenses). The input images have to fullfil a strict
* left-to-right or right-to-left order for the first row, which has to be
* horizontal.
*/
#include "AutoPanoSift.h"
const int Direction_Unknown = 0;
const int Direction_LeftToRight = 1;
const int Direction_RightToLeft = 2;
BondBall* BondBall_new0()
{
BondBall* self = (BondBall*)malloc(sizeof(BondBall));
self->dir = Direction_Unknown;
self->center = 0.0;
self->rotation = 0.0;
self->bottomDefault = -1;
self->bondOrientationTolerance = 35.0;
self->sets = ArrayList_new0(NULL);
self->first = NULL;
self->last = NULL;
self->firstRow = NULL;
self->positions = NULL;
return self;
}
void BondBall_delete(BondBall* self)
{
if (self) {
ArrayList_delete(self->sets);
ArrayList_delete(self->firstRow);
HashTable_delete(self->positions);
free(self);
}
}
BondBall* BondBall_new(int bottomDir)
{
BondBall* self = BondBall_new0();
self->bottomDefault = bottomDir;
return self;
}
bool BondBall_IsWithinAngleDegree (double left, double right, double test)
{
while (left < 0.0)
left += 360.0;
while (right < 0.0)
right += 360.0;
while (test < 0.0)
test += 360.0;
// easy case, no wraparound
if (left < right) {
if (test >= left && test < right)
return (true);
return (false);
}
// left is bigger than right, this means they wrap at 0.0/360.0 degrees
if (test >= left && test <= 360.0)
return (true);
if (test >= 0.0 && test < right)
return (true);
return (false);
}
// Initiate a new panorama positioning by starting with the first two
// pictures gives in the 'first' matchset.
//
// Return true if we positively set up the first row orientation and its
// picture orientation.
// Return false if we cannot determine the orientations.
bool BondBall_InitiateBond (BondBall* self, MatchSet* first)
{
ImageMatchModel* fit = first->bestMatchFit;
self->first = self->last = first;
ArrayList_AddItem (self->sets, self->first);
double centerDegree = (fit->trans->centerAngle / (2.0 * M_PI)) * 360.0;
if (centerDegree < 0.0)
centerDegree += 360.0;
// Simple cases: normalized rotation, left to right or right to left
// A. Left to right
if (BondBall_IsWithinAngleDegree (0.0 - self->bondOrientationTolerance,
0.0 + self->bondOrientationTolerance, centerDegree))
{
self->center = 0.0;
self->rotation = 0.0;
self->dir = Direction_LeftToRight;
return (true);
// B. Right to left
} else if (BondBall_IsWithinAngleDegree (180.0 - self->bondOrientationTolerance,
180.0 + self->bondOrientationTolerance, centerDegree))
{
self->center = 180.0;
self->rotation = 0.0;
self->dir = Direction_RightToLeft;
return (true);
}
// Ambiguous case: 90/270 degrees, tilted by -90 or 90 degrees. Now,
// we first determine where the bottom lies in the pictures (left or
// right)
int bottomDir[2] = {0,0};
int n;
for ( n = 0 ; n < 2 ; ++n) {
if (self->bottomDefault != -1) {
bottomDir[n] = self->bottomDefault;
} else {
bottomDir[n] = BondBall_GuessBottomOrientation (MatchSet_GetOriginalKeys(first,n),
n == 0 ? first->xDim1 : first->xDim2);
}
}
// Cannot tell or directions mismatch
if (bottomDir[0] == -1 || bottomDir[1] == -1 ||
bottomDir[0] != bottomDir[1])
{
WriteLine ("Error: The picture orientation is ambiguous.");
WriteLine (" We have either -90 or 90 degree input pictures.\n");
WriteLine (" To possibly resolve this, please try to use the --bottom-is-left");
WriteLine (" or --bottom-is-right option.");
return (false);
}
WriteLine ("First row pictures have the bottom on the %s side.",
bottomDir[0] == 0 ? "left" : "right");
// Resolve the ambiguity among four cases
if (BondBall_IsWithinAngleDegree (90.0 - self->bondOrientationTolerance,
90.0 + self->bondOrientationTolerance, centerDegree))
{
self->dir = Direction_LeftToRight;
self->center = 90.0;
if (bottomDir[0] == 0)
self->rotation = -90.0;
else
self->rotation = 90.0;
} else if (BondBall_IsWithinAngleDegree (270.0 - self->bondOrientationTolerance,
270.0 + self->bondOrientationTolerance, centerDegree))
{
self->dir = Direction_RightToLeft;
self->center = 270.0;
if (bottomDir[0] == 0)
self->rotation = -90.0;
else
self->rotation = 90.0;
}
return (true);
}
// Return true on end.
bool BondBall_AddRowImage (BondBall* self, MatchSet* next)
{
if (strcmp (self->last->file2, next->file1) != 0)
FatalError("The row is not continuous in the next matchset.");
// Get angle between pictures
ImageMatchModel* fit = next->bestMatchFit;
double centerDegree = (fit->trans->centerAngle / (2.0 * M_PI)) * 360.0;
if (centerDegree < 0.0)
centerDegree += 360.0;
if (BondBall_IsWithinAngleDegree (self->center - self->bondOrientationTolerance,
self->center + self->bondOrientationTolerance,
centerDegree))
{
self->last = next;
ArrayList_AddItem (self->sets, next);
WriteLine ("Angle %.2f degrees fits, adding \"%s\" to row.",
centerDegree, next->file2);
return (false);
} else {
WriteLine ("Angle %.2f degrees of \"{%s}\" is outside of %.2f+/-%.2f range, row end reached",
centerDegree - 360.0, next->file2, self->center, self->bondOrientationTolerance);
return (true);
}
}
// One of the pictures in ms must be in the positions hashtable already,
// and the other must be outside.
Position* BondBall_EstimateImage (BondBall* self, MatchSet* ms)
{
if (self->positions == NULL)
FatalError ("Positions hashtable is empty, cannot align.");
bool image1known = HashTable_Contains(self->positions, ms->file1);
char* knownFile = image1known ? ms->file1 : ms->file2;
//char* unknownFile = image1known ? ms->file2 : ms->file1;
Position* knownPos = (Position*) HashTable_GetItem(self->positions, knownFile);
ImageMatchModel* fit = (ImageMatchModel*) ms->bestMatchFit;
/*
double centerDegree = (fit.CenterAngle / (2.0 * M_PI)) * 360.0;
double newRotation = centerDegree; +
(knownPos.Rotation * 2.0 * M_PI) / 360.0;
*/
//Console.WriteLine ("fit.RotationAngle = %f", fit->rotationAngle);
double newRotation = fit->trans->rotationAngle +
(knownPos->rotation * 2.0 * M_PI) / 360.0;
newRotation = (newRotation / (2.0 * M_PI)) * 360.0;
/*Console.WriteLine ("new rotation for image \"%s\" is %f degrees",
unknownFile, newRotation);
Console.WriteLine (" centerangle = %f", fit->centerAngle);
Console.WriteLine (" sin(centerangle) = %f", sin (fit->centerAngle));
Console.WriteLine (" cos(centerangle) = %f", cos (fit->centerAngle));
*/
double yaw = knownPos->yaw;
double pitch = knownPos->pitch;
/*Console.WriteLine ("fit.ShiftWidth = %f", fit->shiftWidth);
Console.WriteLine ("ms.xDim1 = %d", ms->xDim1);*/
double angleShiftHorizontal = min (
((double) fit->trans->shiftWidth / (double) ms->xDim1) * self->yawStep * 2.0,
20.0);
double angleShiftVertical = min (
((double) fit->trans->shiftWidth / (double) ms->yDim1) * self->yawStep * 2.0,
20.0);
/*Console.WriteLine ("shift h/v: {0}, {1}, yawStep = {2}",
angleShiftHorizontal, angleShiftVertical, yawStep);*/
/*
Console.WriteLine ("## CenterAngle = {0}, RotationAngle = {1}",
fit.CenterAngle, fit.RotationAngle);
Console.WriteLine ("## knownPos.Rotation = {0}", knownPos.Rotation);
Console.WriteLine ("## newPos.Rotation = {0}", newRotation);
*/
double ca = fit->trans->centerAngle + ((knownPos->rotation / 360.0) * 2.0 * M_PI);
/*ca += M_PI; // invert direction
if (ca >= (2.0 * M_PI))
ca -= 2.0 * M_PI;*/
//Console.WriteLine ("ca = {0}", ca);
double reverseFactor = image1known ? 1.0 : -1.0;
//Console.WriteLine ("yaw -= {0}", reverseFactor * cos (ca) * angleShiftHorizontal);
//Console.WriteLine ("pitch -= {0}", reverseFactor * sin (ca) * angleShiftVertical);
yaw -= cos (ca) * angleShiftHorizontal;
pitch -= reverseFactor * sin (ca) * angleShiftVertical;
return (Position_new (yaw, pitch, newRotation));
/*
if (centerDegree < 0.0)
centerDegree += 360.0;
*/
}
Position* Position_new0()
{
Position* self = (Position*)malloc(sizeof(Position));
return self;
}
void Position_delete(Position* self)
{
if (self) {
free(self);
}
}
Position* Position_new(double yaw, double pitch, double rotation)
{
Position* self = Position_new0();
self->yaw = yaw;
self->pitch = pitch;
self->rotation = rotation;
return self;
}
char* Position_ToString (Position* self)
{
char* str = (char*) malloc(60);
sprintf(str, "pos (yaw = %.2f, pitch = %.2f, rotation = %.2f)",
self->yaw, self->pitch, self->rotation);
return str;
}
void BondBall_StretchImages (BondBall* self, bool is360)
{
self->positions = HashTable_new0 (NULL, Position_delete);
// In case its a 360 degree panorama, things are easy
self->yawStep = 360.0 / ArrayList_Count(self->firstRow);
// In case it's not a full pano, we lower the value. To have an exact
// value is really not so important, its just a rough minimum for the
// later optimization process to base its work upon.
if (is360 == false) {
// Now we employ heuristics/educated guessing... humpf.
self->yawStep = min (20.0, self->yawStep);
}
// Set the yaw, pitch and rotation values
int xs = 0;
int i;
for(i=0; i<ArrayList_Count(self->firstRow); i++) {
char* filename = (char *) ArrayList_GetItem(self->firstRow, i);
double yawCur = xs * self->yawStep;
// Roughly align in center for non 360 degree panoramas.
if (is360 == false)
yawCur -= (ArrayList_Count(self->firstRow) / 2.0) * self->yawStep;
WriteLine ("DEBUG %s: yawCur = %f, xs: %d, yawStep = %f", filename, yawCur, xs, self->yawStep);
//WriteLine ("%s: yaw = %f, rotation = %f", filename, yawCur, self->rotation);
HashTable_AddItem(self->positions , filename,
Position_new (yawCur, 0.0, self->rotation));
xs += 1;
}
}
// Guesses where the bottom of the image is, based on keypoint density.
// This is highly experimental code.
// The only cases considered is left/right, as we assume no image has the
// bottom on the top.
// TODO: maybe add a third case: bottom at bottom, though this could be
// reliably estimated based on image order alone.
//
// Return -1 in case its not certain,
// return 0 in case the bottom is estimated to be on the west/left side
// return 1 in case the bottom is estimated to be on the east/right side
int BondBall_GuessBottomOrientation (ArrayList* keys, int xDim)
{
double xAccum = 0.0;
int i;
for(i=0; i<ArrayList_Count(keys); i++) {
KeypointN* kp = (KeypointN*) ArrayList_GetItem(keys, i);
xAccum += kp->x;
}
xAccum /= ArrayList_Count(keys);
//WriteLine ("xDim: %d, xAverage: %f", xDim, xAccum);
double averageDivergenceBoundary = xDim / 12.0;
if (xAccum <= ((xDim / 2) - averageDivergenceBoundary))
return (0);
else if (xAccum >= ((xDim / 2) + averageDivergenceBoundary))
return (1);
return (-1);
}
char* BondBall_ToString (BondBall* self)
{
char* str = (char*)malloc(40);
sprintf(str,"%d, center %.2f, rotation %.2f",
self->dir, self->center, self->rotation);
return str;
}