[6d12f1]: src / tools / pto_gen.cpp  Maximize  Restore  History

Download this file

525 lines (494 with data), 17.8 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
// -*- c-basic-offset: 4 -*-
/** @file pto_gen.cpp
*
* @brief program to generate a pto file from given image files
*
* @author T. Modes
*
*/
/* 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 <hugin_version.h>
#include <fstream>
#include <getopt.h>
#ifdef _WINDOWS
#include <io.h>
#endif
#include <panodata/Panorama.h>
#include <panodata/StandardImageVariableGroups.h>
#include <panodata/OptimizerSwitches.h>
#include <algorithms/basic/CalculateMeanExposure.h>
#include "hugin_utils/alphanum.h"
#include <vigra/impex.hxx>
#include <lensdb/LensDB.h>
#ifdef __APPLE__
#include <hugin_config.h>
#include <mach-o/dyld.h> /* _NSGetExecutablePath */
#include <limits.h> /* PATH_MAX */
#include <libgen.h> /* dirname */
#endif
using namespace std;
using namespace HuginBase;
static void usage(const char* name)
{
cout << name << ": generate project file from images" << endl
<< name << " version " << DISPLAY_VERSION << endl
<< endl
<< "Usage: " << name << " [options] image1 [...]" << endl
<< endl
<< " Options:" << endl
<< " -o, --output=file.pto Output Hugin PTO file." << endl
<< " -p, --projection=INT Projection type (default: 0)" << endl
<< " -f, --fov=FLOAT Horizontal field of view of images (default: 50)" << endl
<< " -c, --crop=left,right,top,bottom Sets the crop of input" << endl
<< " images (especially for fisheye lenses)" << endl
<< " -s, --stacklength=INT Number of images in stack" << endl
<< " (default: 1, no stacks)" << endl
<< " -l, --linkstacks Link image positions in stacks" << endl
<< " --distortion Try to load distortion information from" << endl
<< " lensfun database" << endl
<< " --vignetting Try to load vignetting information from" << endl
<< " lensfun database" << endl
<< " -h, --help Shows this help" << endl
<< endl;
}
void InitLensDB()
{
#ifdef _WINDOWS
char buffer[MAX_PATH];//always use MAX_PATH for filepaths
GetModuleFileName(NULL,buffer,sizeof(buffer));
string working_path=string(buffer);
//remove filename
std::string::size_type pos=working_path.rfind("\\");
if(pos!=std::string::npos)
{
working_path.erase(pos);
//remove last dir: should be bin
pos=working_path.rfind("\\");
if(pos!=std::string::npos)
{
working_path.erase(pos);
//append path delimiter and path
working_path.append("\\share\\lensfun\\");
HuginBase::LensDB::LensDB::GetSingleton().SetMainDBPath(working_path);
}
}
#elif defined MAC_SELF_CONTAINED_BUNDLE
char path[PATH_MAX + 1];
uint32_t size = sizeof(path);
string install_path_lensfun("");
if (_NSGetExecutablePath(path, &size) == 0)
{
install_path_lensfun=dirname(path);
install_path_lensfun.append("/../Resources/lensfun/");
HuginBase::LensDB::LensDB::GetSingleton().SetMainDBPath(install_path_lensfun);
}
#endif
};
int main(int argc, char* argv[])
{
// parse arguments
const char* optstring = "o:p:f:c:s:lh";
static struct option longOptions[] =
{
{"output", required_argument, NULL, 'o' },
{"projection", required_argument, NULL, 'p' },
{"fov", required_argument, NULL, 'f' },
{"crop", required_argument, NULL, 'c' },
{"stacklength", required_argument, NULL, 's' },
{"linkstacks", no_argument, NULL, 'l' },
{"distortion", no_argument, NULL, 300 },
{"vignetting", no_argument, NULL, 301 },
{"help", no_argument, NULL, 'h' },
0
};
int c;
int optionIndex = 0;
string output;
int projection=-1;
float fov=-1;
int stackLength=1;
bool linkStacks=false;
vigra::Rect2D cropRect(0,0,0,0);
bool loadDistortion=false;
bool loadVignetting=false;
while ((c = getopt_long (argc, argv, optstring, longOptions,&optionIndex)) != -1)
{
switch (c)
{
case 'o':
output = optarg;
break;
case 'h':
usage(argv[0]);
return 0;
case 'p':
{
projection=atoi(optarg);
if((projection==0) && (strcmp(optarg,"0")!=0))
{
cerr << "Could not parse image number.";
return 1;
};
if(projection<0)
{
cerr << "Invalid projection number." << endl;
return 1;
};
};
break;
case 'f':
fov=atof(optarg);
if(fov<1 || fov>360)
{
cerr << "Invalid field of view";
return 1;
};
break;
case 'c':
{
int left, right, top, bottom;
int n=sscanf(optarg, "%d,%d,%d,%d", &left, &right, &top, &bottom);
if (n==4)
{
if(right>left && bottom>top)
{
cropRect.setUpperLeft(vigra::Point2D(left,top));
cropRect.setLowerRight(vigra::Point2D(right,bottom));
}
else
{
cerr << "Invalid crop area" << endl;
return 1;
};
}
else
{
cerr << "Could not parse crop values" << endl;
return 1;
};
};
break;
case 's':
stackLength=atoi(optarg);
if(stackLength<1)
{
cerr << "Could not parse stack length." << endl;
return 1;
};
break;
case 'l':
linkStacks=true;
break;
case 300:
loadDistortion=true;
break;
case 301:
loadVignetting=true;
break;
case ':':
cerr <<"Option " << longOptions[optionIndex].name << " requires a number" << endl;
return 1;
break;
case '?':
break;
default:
abort ();
}
}
if (argc - optind < 1)
{
usage(argv[0]);
return 1;
};
cout << "Generating pto file..." << endl;
cout.flush();
std::vector<string> filelist;
while(optind<argc)
{
string input;
#ifdef _WINDOWS
//do globbing
input=GetAbsoluteFilename(argv[optind]);
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char newFile[_MAX_PATH];
_splitpath(input.c_str(), drive, dir, NULL, NULL);
struct _finddata_t finddata;
intptr_t findhandle = _findfirst(input.c_str(), &finddata);
if (findhandle != -1)
{
do
{
//ignore folder, can be happen when using *.*
if((finddata.attrib & _A_SUBDIR)==0)
{
_splitpath(finddata.name, NULL, NULL, fname, ext);
_makepath(newFile, drive, dir, fname, ext);
//check if valid image file
if(vigra::isImage(newFile))
{
filelist.push_back(std::string(newFile));
};
};
}
while (_findnext(findhandle, &finddata) == 0);
_findclose(findhandle);
}
#else
input=argv[optind];
if(hugin_utils::FileExists(input))
{
if(vigra::isImage(input.c_str()))
{
filelist.push_back(GetAbsoluteFilename(input));
};
};
#endif
optind++;
};
if(filelist.size()==0)
{
cerr << "No valid image files given." << endl;
return 1;
};
//sort filenames
sort(filelist.begin(),filelist.end(),doj::alphanum_less());
if(projection<0)
{
InitLensDB();
};
Panorama pano;
for(size_t i=0; i<filelist.size();i++)
{
SrcPanoImage srcImage;
cout << "Reading " << filelist[i] << "..." << endl;
srcImage.setFilename(filelist[i]);
try
{
vigra::ImageImportInfo info(filelist[i].c_str());
if(info.width()==0 || info.height()==0)
{
cerr << "ERROR: Could not decode image " << filelist[i] << endl
<< "Skipping this image." << endl << endl;
continue;
}
srcImage.setSize(info.size());
std::string pixelType=info.getPixelType();
if((pixelType=="UINT8") || (pixelType=="UINT16") || (pixelType=="INT16"))
{
srcImage.setResponseType(HuginBase::SrcPanoImage::RESPONSE_EMOR);
}
else
{
srcImage.setResponseType(HuginBase::SrcPanoImage::RESPONSE_LINEAR);
};
}
catch(std::exception & e)
{
cerr << "ERROR: caught exception: " << e.what() << endl;
cerr << "Could not read image informations for file " << filelist[i] << endl;
cerr << "Skipping this image." << endl << endl;
continue;
};
srcImage.readEXIF();
srcImage.applyEXIFValues();
if(projection>=0)
{
srcImage.setProjection((HuginBase::BaseSrcPanoImage::Projection)projection);
}
else
{
srcImage.readProjectionFromDB();
};
if(fov>0)
{
srcImage.setHFOV(fov);
if(srcImage.getCropFactor()==0)
{
srcImage.setCropFactor(1.0);
};
}
else
{
//set plausible default value if they could not read from exif
if(srcImage.getExifFocalLength()==0 || srcImage.getCropFactor()==0)
{
cout << "\tNo value for field of view found in EXIF data. " << endl
<< "\tAssuming a HFOV of 50 degrees. " << endl;
srcImage.setHFOV(50);
srcImage.setCropFactor(1.0);
};
};
if(cropRect.width()>0 && cropRect.height()>0)
{
if(srcImage.isCircularCrop())
{
srcImage.setCropMode(SrcPanoImage::CROP_CIRCLE);
}
else
{
srcImage.setCropMode(SrcPanoImage::CROP_RECTANGLE);
};
srcImage.setAutoCenterCrop(false);
srcImage.setCropRect(cropRect);
};
if(loadDistortion)
{
if(srcImage.readDistortionFromDB())
{
cout << "\tRead distortion data from lensfun database." << endl;
}
else
{
cout << "\tNo valid distortion data found in lensfun database." << endl;
};
};
if(loadVignetting)
{
if(srcImage.readVignettingFromDB())
{
cout << "\tRead vignetting data from lensfun database." << endl;
}
else
{
cout << "\tNo valid vignetting data found in lensfun database." << endl;
};
};
pano.addImage(srcImage);
};
if(pano.getNrOfImages()==0)
{
cerr << "Adding images to project files failed." << endl;
HuginBase::LensDB::LensDB::Clean();
return 1;
};
//link lenses
if(pano.getNrOfImages()>1)
{
double redBalanceAnchor=pano.getImage(pano.getOptions().colorReferenceImage).getExifRedBalance();
double blueBalanceAnchor=pano.getImage(pano.getOptions().colorReferenceImage).getExifBlueBalance();
if(fabs(redBalanceAnchor)<1e-2)
{
redBalanceAnchor=1;
};
if(fabs(blueBalanceAnchor)<1e-2)
{
blueBalanceAnchor=1;
};
StandardImageVariableGroups variable_groups(pano);
ImageVariableGroup& lenses = variable_groups.getLenses();
for(size_t i=1;i<pano.getNrOfImages();i++)
{
int image=-1;
const SrcPanoImage & srcImg=pano.getImage(i);
for(size_t j=0;j<i;j++)
{
const SrcPanoImage & compareImg=pano.getImage(j);
if(srcImg.getHFOV()==compareImg.getHFOV() &&
srcImg.getProjection()==compareImg.getProjection() &&
srcImg.getExifModel()==compareImg.getExifModel() &&
srcImg.getExifMake()==compareImg.getExifMake() &&
srcImg.getSize()==compareImg.getSize())
{
image=j;
break;
};
};
if(image!=-1)
{
SrcPanoImage img=pano.getSrcImage(i);
double ev=img.getExposureValue();
lenses.switchParts(i,lenses.getPartNumber(image));
lenses.unlinkVariableImage(HuginBase::ImageVariableGroup::IVE_ExposureValue, i);
img.setExposureValue(ev);
lenses.unlinkVariableImage(HuginBase::ImageVariableGroup::IVE_WhiteBalanceRed, i);
lenses.unlinkVariableImage(HuginBase::ImageVariableGroup::IVE_WhiteBalanceBlue, i);
img.setWhiteBalanceRed(img.getExifRedBalance()/redBalanceAnchor);
img.setWhiteBalanceBlue(img.getExifBlueBalance()/blueBalanceAnchor);
pano.setSrcImage(i, img);
};
};
cout << endl << "Assigned " << lenses.getNumberOfParts() << " lenses." << endl;
if(lenses.getNumberOfParts()>1 && stackLength>1)
{
cout << "Project contains more than one lens, but you requested to assign" << endl
<< "stacks. This is not supported. Therefore stacks will not be" << endl
<< "assigned." << endl << endl;
stackLength=1;
};
};
//link stacks
if(pano.getNrOfImages()>1 && stackLength>1)
{
stackLength=std::min<int>(stackLength,pano.getNrOfImages());
int stackCount=pano.getNrOfImages() / stackLength;
if(pano.getNrOfImages() % stackLength > 0)
{
stackCount++;
};
if(stackCount<pano.getNrOfImages())
{
for(size_t stackNr=0;stackNr<stackCount;stackNr++)
{
size_t firstImgStack=stackNr*stackLength;
for(size_t i=0;i<stackLength;i++)
{
if(firstImgStack+i<pano.getNrOfImages())
{
pano.linkImageVariableStack(firstImgStack,firstImgStack+i);
if(linkStacks)
{
pano.linkImageVariableYaw(firstImgStack,firstImgStack+i);
pano.linkImageVariablePitch(firstImgStack,firstImgStack+i);
pano.linkImageVariableRoll(firstImgStack,firstImgStack+i);
};
};
};
};
cout << "Assigned " << stackCount << " stacks." << endl;
};
};
//set output exposure value
PanoramaOptions opt = pano.getOptions();
opt.outputExposureValue = CalculateMeanExposure::calcMeanExposure(pano);
pano.setOptions(opt);
// set optimizer switches
pano.setOptimizerSwitch(HuginBase::OPT_PAIR);
pano.setPhotometricOptimizerSwitch(HuginBase::OPT_EXPOSURE | HuginBase::OPT_VIGNETTING | HuginBase::OPT_RESPONSE);
//output
if(output=="")
{
output=hugin_utils::stripExtension(pano.getImage(0).getFilename());
if(pano.getNrOfImages()>1)
{
output.append("-");
output.append(hugin_utils::stripExtension(hugin_utils::stripPath(pano.getImage(pano.getNrOfImages()-1).getFilename())));
};
output=output.append(".pto");
};
output=GetAbsoluteFilename(output);
//write output
UIntSet imgs;
fill_set(imgs,0, pano.getNrOfImages()-1);
ofstream of(output.c_str());
pano.printPanoramaScript(of, pano.getOptimizeVector(), pano.getOptions(), imgs, false, hugin_utils::getPathPrefix(output));
cout << endl << "Written output to " << output << endl;
HuginBase::LensDB::LensDB::Clean();
return 0;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks