[03d2ea]: APSCpp / APSCpp_main.c  Maximize  Restore  History

Download this file

846 lines (707 with data), 27.5 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/* APSCpp_main.c 18 Feb2008 TKSharpless
enhanced version of APSC for Hugin 0.7 that can transform images to
stereographic projection for better keypoint matching.
The enhanced mode is invoked by giving it a project file in place
of the image files. The project file 'i' lines supply information
about the images' projections, as well as their file names. This
info is stored in DIlist. The keypoint generator (and the refine
keypoints step if used) converts intensity maps to stereographic
projection using info from DIlist. At tne final output-to-pto stage
the control point coordinates get projected back to image space.
The projections are "ideal" (no lens or center point corrections), but
the reprojection machinery could handle those if available.
* (C) Copyright 2004 -- Sebastian Nowozin (nowozin@cs.tu-berlin.de)
*
* "The University of British Columbia has applied for a patent on the SIFT
* algorithm in the United States. Commercial applications of this software
* may require a license from the University of British Columbia."
* For more information, see the LICENSE file supplied with the distribution.
*
* This program is free software released under the GNU General Public
* License, which is included in this software package (doc/LICENSE).
*/
/*
* (C) Copyright 2008, 2009 Thomas K Sharpless (tksharpless@gmail.com)
*
* This program 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 2 of the License, or (at your option) any later version.
*
* This software 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 this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <time.h>
#include "AutoPanoSift.h"
#include "sphereAlign.h"
// APSCpp.cpp
int LoadProjectImages( char* script, ArrayList * DIlist, bool equalAngle );
KeypointXMLList * GenerateKeyspp( char * imgname, int maxdim,
DIinfo * pdi, double stgfov, int * KPcount );
int MultiMatch_ANN ( MultiMatch* );
// globals
double globFov = 0; // width for all images in degrees
void Usage ()
{
WriteLine ("autopano-sift-c: Find control points giving a Hugin project file\n");
WriteLine (" Version %s \n", PACKAGE_VERSION);
WriteLine ("autopano-sift-c [options] output.pto image1 image2 [..]");
WriteLine (" hugin project file output.pto gets the results ");
WriteLine (" input images can be jpeg, tiff, or other formats.\n");
WriteLine ("autopano-sift-c [options] output.pto projectfile");
WriteLine (" projectfile: a Hugin or other PT family project script with");
WriteLine (" image file names, projections, angular widths and optionally");
WriteLine (" alignments. Enables the options of stereographic projection");
WriteLine (" and layout-guided control point placement.\n");
WriteLine ("Options");
WriteLine (" --ransac <on|off|1|0> Switch RANSAC filtration on or off (default: off).");
WriteLine (" --maxmatches <matches> Output no more that this many control points per");
WriteLine (" image pair (default: 25, zero means unlimited)");
WriteLine (" --maxdim <n> Make largest image dimension <= n (default: 1600).");
WriteLine (" --projection <n>,<d> n = PT format code, d = hfov in degrees. These");
WriteLine (" apply to all images. Enables stereographic option.");
WriteLine (" --stereographic <on|off|1|0> reprojection on (if enabled) or off. Default on.");
WriteLine (" --stgfov <d> minimum hfov (degrees) for reprojection. Default 65.");
WriteLine (" --equal-angle-fisheye Set fisheye lens type to equal-angle instead of");
WriteLine (" equal-area. Ignored for PTAssembler projects.");
WriteLine (" --disable-areafilter Do not use max-area filtration, which is default.");
WriteLine (" See manpage for details.");
WriteLine (" --integer-coordinates Truncate match coordinates to integer numbers.");
WriteLine (" --absolute-pathnames <on|off|1|0> Use the absolute pathname of the image");
WriteLine (" file in the PTO output file. Disabled by default.");
WriteLine (" --use-layout <on|off|1|0> image alignment in pto file guides control point");
WriteLine (" placement; or does not. Default is off.");
WriteLine ("");
WriteLine ("Refinement options");
WriteLine (" --refine Refine the found control points using the");
WriteLine (" original images.");
WriteLine (" --refine-by-middle Use the best middle point to refine (default).");
WriteLine (" --refine-by-mean Use the mean of the patches control points.");
WriteLine (" --keep-unrefinable <on|off|1|0>");
WriteLine (" Keep unrefinable matches (default: on).");
WriteLine ("output.pto: The output PTO panorama project file.");
WriteLine (" This filename can be \"-\", then stdout is used");
WriteLine ("image<n>: input image files (any common format: JPEG, PNG, TIFF, ..)");
WriteLine ("");
}
typedef struct Resolution Resolution;
struct Resolution
{
int x, y;
};
Resolution* Resolution_new0()
{
Resolution* self = (Resolution*)malloc(sizeof(Resolution));
return self;
}
Resolution* Resolution_new(int x, int y)
{
Resolution* self = Resolution_new0();
self->x = x;
self->y = y;
return self;
}
void Resolution_delete(Resolution* self)
{
if (self) {
free(self);
}
}
int Resolution_CompareTo (Resolution* self, int x, int y)
{
if (self->x == x && self->y == y)
return (0);
return (1);
}
// The maximum radius to consider around a keypoint that is refined. That
// is, at most a patch of a maximum size of twice this value in both
// horizontal and vertical direction is extracted.
int RefinementRadiusMaximum = 96;
int (*refineHandler)(int index, int total);
void RefineKeypoints (ArrayList* msList,
bool selectMiddlePoint, bool neverLosePoints);
DisplayImage* ExtractPatch (DisplayImage* large,
int px, int py, double scale, int* radius);
ArrayList* ExtractKeypoints (DisplayImage* pic);
bool YesNoOption (char* optionName, char* val, int * nerr);
// selectMiddlePoint: if true, select the middle point in the patch,
// otherwise build the mean
// neverLosePoints: if true, and if we cannot do the refinement, still use
// the control point.
void RefineKeypoints (ArrayList* msList,
bool selectMiddlePoint, bool neverLosePoints)
{
DisplayImage* pic1 = NULL;
DisplayImage* pic2 = NULL;
char* pic1Name = NULL;
char* pic2Name = NULL;
/* Keep stats for the refineHandler delegate
*/
int totalRefines = 0;
int doneRefines = 0;
int i;
for(i=0; i<ArrayList_Count(msList); i++) {
MatchSet* ms = (MatchSet*) ArrayList_GetItem(msList, i);
int j;
for(j=0; j<ArrayList_Count(ms->matches); j++) {
ArrayList_GetItem(ms->matches, j);
totalRefines += 1;
}
}
for(i=0; i<ArrayList_Count(msList); i++) {
MatchSet* ms = (MatchSet*) ArrayList_GetItem(msList, i);
WriteLine (" between \"%s\" and \"%s\"",
ms->file1, ms->file2);
if (pic1Name != ms->file1) {
pic1Name = ms->file1;
pic1 = DisplayImage_new (ms->file1);
}
if (pic2Name != ms->file2) {
pic2Name = ms->file2;
pic2 = DisplayImage_new (ms->file2);
}
/*WriteLine ("pair: %s, %s, %d keypoint matches",
ms->file1, ms->file2, ArrayList_Count(ms->Matches));*/
ArrayList* refinedMatches = ArrayList_new0 (NULL);
int j;
for(j=0; j<ArrayList_Count(ms->matches); j++) {
Match* m = (Match*) ArrayList_GetItem(ms->matches, j);
int p1x = (int) (m->kp1->x + 0.5);
int p1y = (int) (m->kp1->y + 0.5);
int p1radius;
DisplayImage* patch1 = ExtractPatch (pic1, p1x, p1y,
m->kp1->scale, &p1radius);
int p2x = (int) (m->kp2->x + 0.5);
int p2y = (int) (m->kp2->y + 0.5);
int p2radius;
DisplayImage* patch2 = ExtractPatch (pic2, p2x, p2y,
m->kp2->scale, &p2radius);
/* Call the refine handler delegate in case there is one to
* inform the callee of a single refining step (for progress
* bar displays and such).
*/
doneRefines += 1;
if (refineHandler != NULL)
refineHandler (doneRefines, totalRefines);
// Skip over keypoint matches we cannot refine as part of the
// image lies outside.
if (patch1 == NULL || patch2 == NULL) {
if (neverLosePoints)
ArrayList_AddItem(refinedMatches, m);
DisplayImage_delete(patch1);
DisplayImage_delete(patch2);
continue;
}
// Otherwise, run the SIFT algorithm on both small patches.
ArrayList* p1kp = ExtractKeypoints (patch1);
ArrayList* p2kp = ExtractKeypoints (patch2);
/*WriteLine ("p1kp = %d, p2kp = %d", ArrayList_Count(p1kp),
ArrayList_Count(p2kp));*/
// Apply the matching, RANSAC enabled.
MultiMatch* mm = MultiMatch_new0 ();
mm->verbose = false;
ArrayList* matches = NULL;
matches = MultiMatch_TwoPatchMatch (mm, p1kp,
patch1->width, patch1->height, p2kp, patch2->width,
patch2->height, true);
DisplayImage_delete(patch1);
DisplayImage_delete(patch2);
/* In case there are less than three keypoints in the
* two patches, we ignore them all.
*/
if (0 /*was exception ???*/ ) {
matches = NULL;
}
if (matches == NULL || ArrayList_Count(matches) != 1) {
if (neverLosePoints)
ArrayList_AddItem(refinedMatches, m);
continue;
}
MatchSet* pSet = (MatchSet*) ArrayList_GetItem(matches, 0);
// Now get the real new control point coordinates from the
// patches. We have two options and assume all points are
// equal quality-wise:
// a) Select the one that is most in the middle
// (selectMiddlePoint == true)
// b) Build the mean of all the control point matches in the
// patches (selectMiddlePoint == false).
double kp1X = 0.0;
double kp1Y = 0.0;
double kp2X = 0.0;
double kp2Y = 0.0;
double kpMidDist = Double_PositiveInfinity;
int k;
for(k=0; k<ArrayList_Count(pSet->matches); k++) {
Match* pM = (Match*) ArrayList_GetItem(pSet->matches, k);
if (selectMiddlePoint) {
double dist = sqrt (
pow (pM->kp1->x - p1radius, 2.0) +
pow (pM->kp1->y - p1radius, 2.0));
if (dist < kpMidDist) {
kpMidDist = dist;
kp1X = pM->kp1->x;
kp1Y = pM->kp1->y;
kp2X = pM->kp2->x;
kp2Y = pM->kp2->y;
}
} else {
kp1X += pM->kp1->x;
kp1Y += pM->kp1->y;
kp2X += pM->kp2->x;
kp2Y += pM->kp2->y;
}
/*WriteLine ("(%g, %g) matches (%g, %g)",
pM->kp1->x, pM->kp1->y, pM->kp2->x, pM->kp2->y);*/
}
if (selectMiddlePoint == false) {
kp1X /= (double) ArrayList_Count(pSet->matches);
kp1Y /= (double) ArrayList_Count(pSet->matches);
kp2X /= (double) ArrayList_Count(pSet->matches);
kp2Y /= (double) ArrayList_Count(pSet->matches);
}
kp1X += p1x - p1radius;
kp1Y += p1y - p1radius;
kp2X += p2x - p2radius;
kp2Y += p2y - p2radius;
Match* mn = Match_clone (m);
// Adjust the original keypoints location to be the mean of
// all the highly precise superresolution points.
mn->kp1->x = kp1X;
mn->kp1->y = kp1Y;
mn->kp2->x = kp2X;
mn->kp2->y = kp2Y;
/*WriteLine ("MASTER POINT MATCH: (%g,%g) to (%g,%g)",
kp1X, kp1Y, kp2X, kp2Y);*/
ArrayList_AddItem(refinedMatches, mn);
/*
DisplayImage_Save (patch1, "patch-1.jpg");
DisplayImage_Save (patch2, "patch-2.jpg");
exit (0);
*/
}
ms->matches = refinedMatches;
}
}
/** Extract a small image patch from a larger image, centered at the given
* coordinates.
*/
DisplayImage* ExtractPatch (DisplayImage* large,
int px, int py, double scale, int* radius)
{
*radius = (int) (9.0 * scale + 0.5);
if (*radius > RefinementRadiusMaximum)
*radius = RefinementRadiusMaximum;
/*WriteLine ("patch centered at (%d,%d), scale %g, radius = %d",
px, py, scale, *radius);*/
int pxe = px + *radius;
int pye = py + *radius;
px -= *radius;
py -= *radius;
if (px < 0 || py < 0 || pxe >= large->width || pye >= large->height) {
/*WriteLine (" (%d,%d)-(%d,%d) out of (0,0)-(%d,%d)",
px, py, pxe, pye, large->width, large->height);*/
return (NULL);
} else {
//WriteLine (" extracting patch");
}
DisplayImage* patch = DisplayImage_Carve (large, px, py, *radius*2, *radius*2);
return (patch);
}
/** Produce keypoints for a small image patch.
*/
ArrayList* ExtractKeypoints (DisplayImage* pic)
{
ImageMap* picMap = DisplayImage_ConvertToImageMap (pic);
LoweFeatureDetector* lf = LoweFeatureDetector_new0 ();
LoweFeatureDetector_SetPrintWarning(false);
LoweFeatureDetector_SetVerbose(false);
LoweFeatureDetector_DetectFeatures (lf, picMap);
ArrayList* res = LoweFeatureDetector_GlobalNaturalKeypoints(lf);
lf->globalNaturalKeypoints = NULL; // Make sure res won't get deleted.
LoweFeatureDetector_delete(lf);
return res;
}
// check a commandline truth value
// return true or false; increment *nerr if invalid
bool YesNoOption (char* optionName, char* val, int * nerr)
{
if (strcmp (val, "1") == 0 || strcmp (val, "on") == 0)
return true;
else if (strcmp (val, "0") == 0 || strcmp (val, "off") == 0)
return false;
WriteLine ("Error '%s' is no valid value for option '%s'.",
val, optionName);
(*nerr)++;
return false;
}
/* mods for APSCpp
new arg DIlist gets * the image info list (++mode) or == 0 (legacy mode)
if ++ mode, copies this info to the output 'i' lines
*/
void WritePTOFile (FILE* pto, MultiMatch* mm,
ArrayList* msList, ArrayList * DIlist,
BondBall* bb, int generateHorizon, bool integerCoordinates,
bool useAbsolutePathnames)
{
DIinfo * pdi = 0;
char buffer[262];
fprintf(pto, "# Stitcher project file generated by APSCpp version %s\n\n", PACKAGE_VERSION);
fprintf(pto, "p f2 w3000 h1500 v360 n\"JPEG q90\"\n");
fprintf(pto, "m g1 i0\n\n");
int imageIndex = 0;
HashTable* imageNameTab = HashTable_new0 (NULL, NULL);
ArrayList* resolutions = ArrayList_new0 ( (void *) Resolution_delete);
int i, iks;
for( iks = 0; iks < ArrayList_Count(mm->keySets); iks++ ) {
KeypointXMLList* kx = (KeypointXMLList*) ArrayList_GetItem(mm->keySets, iks );
HashTable_AddItem(imageNameTab, kx->imageFile, (void*)imageIndex);
ArrayList_AddItem(resolutions, Resolution_new (kx->xDim, kx->yDim));
char* imageFile = kx->imageFile;
// get corresponding DIlist record
if( DIlist ){
for( i = 0; i < ArrayList_Count( DIlist ); i++ ){
pdi = (pDIinfo)ArrayList_GetItem( DIlist, i );
if( !strcmp( pdi->name, imageFile ) ) break;
}
if( i >= ArrayList_Count( DIlist ) )
pdi = 0; // NOT FOUND!!
}
// If the resolution was already there, use the first image with
// the exact same resolution as reference for camera-related
// values.
int refIdx;
for (refIdx = 0 ; refIdx < (ArrayList_Count(resolutions) - 1) ; ++refIdx) {
if (Resolution_CompareTo((Resolution*) ArrayList_GetItem(resolutions, refIdx), kx->xDim, kx->yDim) == 0)
break;
}
if (refIdx == (ArrayList_Count(resolutions) - 1))
refIdx = -1;
Position* pos = bb == NULL ? NULL :
(Position*) HashTable_GetItem(bb->positions, imageFile);
/*
if (pos != NULL) {
WriteLine ("yaw %g, pitch %g, rotation %g",
pos->yaw, pos->pitch, pos->rotation);
}*/
double yaw = 0.0, pitch = 0.0, rotation = 0.0, hfov = 180.0;
double a = 0, b = -0.01, c = 0;
int format = 0;
if (pos != NULL) {
yaw = pos->yaw;
pitch = pos->pitch;
rotation = pos->rotation;
} else if( pdi ){
yaw = pdi->yaw;
pitch = pdi->pitch;
rotation = pdi->roll;
format = pdi->format;
if( format == 8 ) format = 3;
hfov = fovdeg( pdi->flpix, kx->xDim, format );
a = pdi->a;
b = pdi->b;
c = pdi->c;
}
if (imageIndex == 0 || refIdx == -1) {
sprintf(buffer, "i w%d h%d f%d a%g b%g c%g d0 e0 p%g r%g v%g y%g u10 n\"%s\"",
kx->xDim, kx->yDim, format, a, b, c, pitch, rotation, hfov, yaw, imageFile);
} else {
sprintf(buffer, "i w%d h%d f%d a=%d b=%d c=%d d0 e0 p%g r%g v=%d y%g u10 n\"%s\"",
kx->xDim, kx->yDim, format, refIdx, refIdx, refIdx, pitch, rotation, refIdx, yaw, imageFile);
}
fprintf( pto, "%s\n", buffer );
//// WriteLine("%s", buffer );
imageIndex += 1;
}
fprintf(pto, "\nv p1 r1 y1\n\n");
fprintf(pto, "# automatically generated control points\n");
int j;
for(j=0; j<ArrayList_Count(msList); j++) {
MatchSet* ms = (MatchSet*) ArrayList_GetItem(msList, j);
int k;
for(k=0; k<ArrayList_Count(ms->matches); k++) {
Match* m = (Match*) ArrayList_GetItem(ms->matches, k);
if (integerCoordinates == false) {
fprintf(pto, "c n%d N%d x%.6f y%.6f X%.6f Y%.6f t0\n",
(int)HashTable_GetItem(imageNameTab, ms->file1), (int)HashTable_GetItem(imageNameTab, ms->file2),
m->kp1->x, m->kp1->y, m->kp2->x, m->kp2->y);
} else {
fprintf(pto, "c n%d N%d x%d y%d X%d Y%d t0\n",
(int)HashTable_GetItem(imageNameTab, ms->file1), (int)HashTable_GetItem(imageNameTab, ms->file2),
(int) (m->kp1->x + 0.5), (int) (m->kp1->y + 0.5),
(int) (m->kp2->x + 0.5), (int) (m->kp2->y + 0.5));
}
}
}
fprintf(pto, "\n# :-)\n\n");
WriteLine ("\nYou can now load the output file into hugin.");
if( DIlist == 0 ){
WriteLine ("Notice: guessed image format and field-of-view, please check and adjust.");
}
ArrayList_delete(resolutions);
HashTable_delete(imageNameTab);
}
int main (int argc, char* argv[])
{
clock_t start,finish;
double time;
// list used in ++ mode only -- image info from pto file
ArrayList * DIlist = 0; // item = DIinfo struct.
char *projectfile = 0;
// "--projection" option sets these for all images
int globFmt = -1; // PT format code
int Nerr = 0; // counts commandline errors
// downscale option = largest allowed image dimension (0: none)
int maxdim = 1600;
// output to stdout flag
bool streamout = false;
// Use RANSAC algorithm match filtration.
bool useRansac = false;
// Use stereographic projection if feasible
bool useStereographic = true;
double stgfov = 65; // don't reproject below this hfov
// Use input alignment if feasible
bool useLayout = false;
// treat fisheye lens as equal-angle
bool equalAngle = false;
// Use area based weighting for final match selection.
bool useAreaFiltration = true;
// Truncate match coordinates to integer numbers.
bool useIntegerCoordinates = false;
// Use the absolute pathname of the image files in the output PTO
// file.
bool useAbsolutePathnames = false;
// Use "keep-best" filtration, keep the maxMatches best.
int maxMatches = 25; // default
// Refinement options
bool refine = false;
bool refineMiddle = true;
bool keepUnrefinable = true;
int optionCount = 0;
int optionN = 1;
int i;
int totalKPs = 0;
WriteLine ("\nAPSCpp, enhanced autopano-sift-c version %s \n", PACKAGE_VERSION);
LoweFeatureDetector_SetVerbose(false); // default low chatter
if (argc+1 < 3) { // can't be legal commandline
Usage ();
exit (1);
}
while (optionN < argc &&
strlen(argv[optionN]) >= 2
&& argv[optionN][0] == '-'
&& argv[optionN][1] == '-')
{
char* optionStr = argv[optionN];
int joptN = 1; // number of args processed
if (strcmp (optionStr, "--maxdim") == 0) {
joptN = 2;
maxdim = atoi(argv[optionN + 1]);
} else if( strcmp (optionStr, "--projection") == 0) {
joptN = 2;
char *p = argv[optionN + 1];
if(sscanf( p, "%d", &globFmt ) != 1 ){
WriteLine ("Parameter to projection option invalid. See the usage help.");
++Nerr;
}
p = strchr(p,',');
if( p ) globFov = atof(p+1);
else {
WriteLine ("Parameter to projection option invalid. See the usage help.");
++Nerr;
}
} else if (strcmp (optionStr, "--ransac") == 0) {
joptN = 2;
useRansac = YesNoOption ("--ransac", argv[optionN + 1], &Nerr );
} else if (strcmp (optionStr, "--stgfov") == 0) {
joptN = 2;
stgfov = atof( argv[optionN + 1] );
} else if (strcmp (optionStr, "--stereographic") == 0) {
joptN = 2;
useStereographic = YesNoOption ("--stereographic", argv[optionN + 1], &Nerr );
} else if (strcmp (optionStr, "--use-layout") == 0) {
joptN = 2;
useLayout = YesNoOption ("--use-layout", argv[optionN + 1], &Nerr );
} else if (strcmp (optionStr, "--maxmatches") == 0) {
joptN = 2;
if (sscanf(argv[optionN + 1], "%d", &maxMatches) != 1) {
WriteLine ("Parameter to maxmatches option invalid. See the usage help.");
++Nerr;
}
if (maxMatches < 0) {
WriteLine ("Maximum number of matches must be positive or zero (unlimited).");
++Nerr;
}
} else if (strcmp (optionStr, "--disable-areafilter") == 0) {
useAreaFiltration = false;
} else if (strcmp (optionStr, "--equal-angle-fisheye") == 0) {
equalAngle = true;
} else if (strcmp (optionStr, "--integer-coordinates") == 0) {
useIntegerCoordinates = true;
} else if (strcmp (optionStr, "--absolute-pathnames") == 0) {
joptN = 2;
useAbsolutePathnames = YesNoOption ("--absolute-pathnames", argv[optionN + 1], &Nerr);
} else if (strcmp (optionStr, "--refine") == 0) {
refine = true;
} else if (strcmp (optionStr, "--refine-by-middle") == 0) {
refineMiddle = true;
} else if (strcmp (optionStr, "--refine-by-mean") == 0) {
refineMiddle = false;
} else if (strcmp (optionStr, "--keep-unrefinable") == 0) {
joptN = 2;
keepUnrefinable = YesNoOption ("--keep-unrefinable", argv[optionN + 1], &Nerr);
} else {
WriteLine ("Bad option \"%s\"", optionStr);
++Nerr;
break; // let's not take chances....
}
optionN += joptN;
}
optionCount = optionN;
// is there an output name and at least one input name?
if( argc - optionN < 2 ){
WriteLine ("Error. Output name and at least one more name required.");
++Nerr;
}
// show usage and quit if commandline errors
if (Nerr){
WriteLine ("%d commandline errors.\n", Nerr );
Usage();
exit( 1 );
}
// next arg is either output file name or "-" for stdout
// anything else beginning with '-' is an error
if( argv[optionCount][0] == '-' ){
if( strcmp( argv[optionCount], "-" ) == 0 )streamout = true;
else {
WriteLine ("Misplaced option '%s'.\n", argv[optionCount] );
Usage();
exit (1);
}
}
/* next arg is either a script name or the first of 2 or more image file names
*/
if( argc - optionCount == 2 ){
projectfile = argv[optionCount + 1];
WriteLine("Reading project file %s", projectfile );
// enable ++ mode by creating DIlist
DIlist = ArrayList_new0( (void *) DIinfo_delete );
// read the proect file image info into DIlist, or die
if( LoadProjectImages( argv[optionCount + 1], DIlist, equalAngle ) == 0 ){
WriteLine ("Error reading project file.");
exit(1);
}
} else if( globFmt >= 0 && globFov > 0 ){
// enable ++ mode
DIlist = ArrayList_new0( (void *) DIinfo_delete );
}
WriteLine(" Default fisheye lens type is %s.",
equalAngle ? "equal-angle" : "equal-area" );
if( DIlist ){
if( useStereographic )
WriteLine(" Stereographic projection enabled for hfov >= %.1f degrees.", stgfov );
if( useLayout )
WriteLine(" Will try to use source image layout to guide CP placement.");
}
/** Find keypoints **/
start = clock();
ArrayList* keylists = ArrayList_new0(NULL);
if( DIlist ){
// ++ mode: image info from DIlist
pDIinfo pd;
// build it first if only a global projection was given
if( globFmt >= 0 ){
for( i=0; i<argc - 1 - optionCount; i++) {
pd = DIinfo_new0();
pd->format = globFmt;
pd->flpix = 0;
strcpy(pd->name, argv[i+optionCount+1] );
// note dimensions are still zero
ArrayList_AddItem( DIlist, pd );
}
}
// process the images
for( i = 0; i < ArrayList_Count( DIlist ); i++ ){
pd = (pDIinfo) ArrayList_GetItem( DIlist, i);
if( !DIinfo_isDummy( pd ) )
ArrayList_AddItem(keylists, GenerateKeyspp( pd->name, maxdim, pd,
useStereographic ? stgfov : -1, &totalKPs ) );
}
} else {
// std mode: image names only from commandline
for( i=0; i<argc - 1 - optionCount; i++) {
ArrayList_AddItem( keylists, GenerateKeyspp( argv[i+optionCount+1],
maxdim, 0, 0, &totalKPs ) );
}
}
finish = clock();
time = ((double)finish - (double)start ) / CLOCKS_PER_SEC;
WriteLine("%.1f seconds to find keypoints", time);
// fail if no keypoints were found
if( totalKPs < 2 ){
FatalError("No keypoints found!");
}
/** Find control points **/
start = clock();
MultiMatch* mm = MultiMatch_new0 ();
MultiMatch_LoadKeysetsFromMemory (mm, keylists);
// note mm now owns keylists and will delete it when destroyed
WriteLine ("\nMatching; ransac %s, area filter %s, layout %s ...",
useRansac == true ? "on" : "off",
useAreaFiltration == true ? "on" : "off",
useLayout == true ? "on" : "off"
);
MultiMatch_ANN ( mm );
ArrayList* msList = MultiMatch_LocateMatchSets (mm, 3, maxMatches,
useRansac, useAreaFiltration);
finish = clock();
time = ((double)finish - (double)start ) / CLOCKS_PER_SEC;
WriteLine("\n%.1f seconds to match keypoints", time);
// Connected component check
WriteLine ("\nConnected component check...");
ArrayList* components = MultiMatch_ComponentCheck (mm, msList);
WriteLine ("Connected component identification resulted in %d component%s:",
ArrayList_Count(components), ArrayList_Count(components) > 1 ? "s" : "");
int compN = 1;
int j;
for(j=0; j<ArrayList_Count(components); j++) {
Component* comp = (Component*) ArrayList_GetItem(components, j);
char* compstr = Component_ToString(comp);
WriteLine ("component %d: %s", compN++, compstr);
free(compstr);
}
if (ArrayList_Count(components) > 1) {
WriteLine ("");
WriteLine ("Warning: One or more components are not connected through control points.");
WriteLine (" Optimization of the resulting PTO will not be possible without");
WriteLine (" manual addition of control points between the components listed");
WriteLine (" above. Please see the manual page for autopano(1) for details.");
WriteLine ("");
} else
WriteLine ("");
if (refine) {
WriteLine ("Refining keypoints");
RefineKeypoints (msList, refineMiddle, keepUnrefinable);
}
FILE* pto;
if ( streamout ) {
pto = stdout;
} else {
WriteLine ("Creating output file \"%s\"", argv[optionCount]);
pto = fopen(argv[optionCount], "w");
}
WritePTOFile (pto, mm, msList, DIlist,
0, 0, useIntegerCoordinates,
useAbsolutePathnames);
ArrayList_delete(components);
MultiMatch_delete(mm);
if ( !streamout )
fclose(pto);
return 0;
}