[3f9ab6]: src / hugin1 / hugin / wxPanoCommand.cpp Maximize Restore History

Download this file

wxPanoCommand.cpp    529 lines (454 with data), 20.4 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
// -*- c-basic-offset: 4 -*-
/** @file PanorCommand.cpp
*
* @brief implementation of some PanoCommands
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* 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 <config.h>
#include "panoinc_WX.h"
#include "panoinc.h"
#include "common/wxPlatform.h"
#include <base_wx/ImageCache.h>
#include <base_wx/platform.h>
#include <hugin/wxPanoCommand.h>
#include <vigra/cornerdetection.hxx>
#include <vigra/localminmax.hxx>
using namespace std;
using namespace vigra;
using namespace utils;
namespace PT {
void wxAddCtrlPointGridCmd::execute()
{
PanoCommand::execute();
// TODO: rewrite, and use same function for modular image creation.
#if 1
const PanoImage & i1 = pano.getImage(img1);
const PanoImage & i2 = pano.getImage(img1);
// run both images through the harris corner detector
ImageCache::EntryPtr eptr = ImageCache::getInstance().getSmallImage(i1.getFilename());
vigra::BImage leftImg(eptr->get8BitImage()->size());
vigra::GreenAccessor<vigra::RGBValue<vigra::UInt8> > ga;
vigra::copyImage(srcImageRange(*(eptr->get8BitImage()), ga ),
destImage(leftImg));
double scale = i1.getWidth() / (double) leftImg.width();
//const vigra::BImage & leftImg = ImageCache::getInstance().getPyramidImage(
// i1.getFilename(),1);
BImage leftCorners(leftImg.size());
FImage leftCornerResponse(leftImg.size());
// empty corner image
leftCorners.init(0);
DEBUG_DEBUG("running corner detector threshold: " << cornerThreshold << " scale: " << scale );
// find corner response at scale scale
vigra::cornerResponseFunction(srcImageRange(leftImg),
destImage(leftCornerResponse),
scale);
// saveScaledImage(leftCornerResponse,"corner_response.png");
DEBUG_DEBUG("finding local maxima");
// find local maxima of corner response, mark with 1
vigra::localMaxima(srcImageRange(leftCornerResponse), destImage(leftCorners), 255);
// exportImage(srcImageRange(leftCorners), vigra::ImageExportInfo("c:/corner_response_maxima.png"));
DEBUG_DEBUG("thresholding corner response");
// threshold corner response to keep only strong corners (above 400.0)
transformImage(srcImageRange(leftCornerResponse), destImage(leftCornerResponse),
vigra::Threshold<double, double>(
cornerThreshold, DBL_MAX, 0.0, 1.0));
vigra::combineTwoImages(srcImageRange(leftCorners), srcImage(leftCornerResponse),
destImage(leftCorners), std::multiplies<float>());
// exportImage(srcImageRange(leftCorners), vigra::ImageExportInfo("c:/corner_response_threshold.png"));
// create transform from img1 -> sphere
PTools::Transform img1ToSphere;
PTools::Transform sphereToImg2;
PanoramaOptions opts;
opts.setProjection(PanoramaOptions::EQUIRECTANGULAR);
opts.setHFOV(360);
opts.setWidth(360);
opts.setVFOV(180);
img1ToSphere.createInvTransform(pano, img1, opts);
sphereToImg2.createTransform(pano, img2, opts);
int border = 5;
double sphx, sphy;
double img2x, img2y;
// need to scale the images.
// sample grid on img1 and try to add ctrl points
for (unsigned int x=0; x < (unsigned int)leftImg.width(); x++ ) {
for (unsigned int y=0; y < (unsigned int)leftImg.height(); y++) {
if (leftCorners(x,y) > 0) {
img1ToSphere.transformImgCoord(sphx, sphy, scale*x, scale*y);
sphereToImg2.transformImgCoord(img2x, img2y, sphx, sphy);
// check if it is inside..
if ( img2x > border && img2x < i2.getWidth() - border
&& img2y > border && img2y < i2.getHeight() - border )
{
// add control point
ControlPoint p(img1, scale*x, scale*y, img2, img2x, img2y);
pano.addCtrlPoint(p);
}
}
}
}
#endif
pano.changeFinished();
}
void wxAddImagesCmd::execute()
{
PanoCommand::execute();
// check if the files should be sorted by date
long sort = wxConfigBase::Get()->Read(wxT("General/SortNewImgOnAdd"), HUGIN_GUI_SORT_NEW_IMG_ON_ADD);
switch (sort) {
case 1:
// sort by filename
std::sort(files.begin(), files.end());
break;
case 2:
// sort by date
std::sort(files.begin(), files.end(), FileIsNewer());
break;
default:
// no or unknown sort method
break;
}
std::vector<std::string>::const_iterator it;
double cropFactor = 0;
double focalLength = 0;
Lens lens;
VariableMap vars;
fillVariableMap(vars);
ImageOptions imgopts;
SrcPanoImage srcImg;
// load additional images...
for (it = files.begin(); it != files.end(); ++it) {
bool added = false;
const std::string &filename = *it;
wxString fname(filename.c_str(), HUGIN_CONV_FILENAME);
int assumeSimilar = wxConfigBase::Get()->Read(wxT("/LensDefaults/AssumeSimilar"), HUGIN_LENS_ASSUME_SIMILAR);
if (!assumeSimilar) {
focalLength = 0;
cropFactor = 0;
}
// try to read settings automatically.
srcImg.setFilename(filename);
bool ok = srcImg.readEXIF(focalLength, cropFactor, true);
if (srcImg.getSize().x == 0 || srcImg.getSize().y == 0) {
wxMessageBox(wxString::Format(_("Could not decode image:\n%s\nAbort"), fname.c_str()), _("Unsupported image file format"));
return;
}
//#if 0
if (! ok && assumeSimilar) {
// search for image with matching size and exif data
// and re-use it.
for (unsigned int i=0; i < pano.getNrOfImages(); i++) {
SrcPanoImage other = pano.getSrcImage(i);
double dummyfl=0;
double dummycrop = 0;
other.readEXIF(dummyfl, dummycrop, false);
if ( other.getSize() == srcImg.getSize()
&&
other.getExifModel() == srcImg.getExifModel() &&
other.getExifMake() == srcImg.getExifMake() &&
other.getExifFocalLength() == srcImg.getExifFocalLength()
)
{
double ev = srcImg.getExposureValue();
srcImg = pano.getSrcImage(i);
srcImg.setFilename(filename);
srcImg.setExposureValue(ev);
// add image
PanoImage img(filename, srcImg.getSize().x, srcImg.getSize().y, other.getLensNr());
srcImg.setLensNr(other.getLensNr());
int imgNr = pano.addImage(img, vars);
pano.setSrcImage(imgNr, srcImg);
added=true;
break;
}
}
if (added) continue;
}
//#endif
int matchingLensNr=-1;
// if no similar image found, ask user
if (! ok) {
if (!getLensDataFromUser(MainFrame::Get(), srcImg, focalLength, cropFactor)) {
// assume a standart lens
srcImg.setHFOV(50);
srcImg.setExifCropFactor(1);
}
}
// check the image hasn't disappeared on us since the HFOV dialog was
// opened
wxString fn(srcImg.getFilename().c_str(),HUGIN_CONV_FILENAME);
if (!wxFileName::FileExists(fn)) {
DEBUG_INFO("Image: " << fn.mb_str() << " has disappeared, skipping...");
continue;
}
// FIXME: check if the exif information
// indicates this image matches a already used lens
for (unsigned int i=0; i < pano.getNrOfImages(); i++) {
SrcPanoImage other = pano.getSrcImage(i);
// force reading of exif data, as it is currently not stored in the
// Panorama data class
if (other.readEXIF(focalLength, cropFactor, false)) {
if (other.getSize() == srcImg.getSize()
&& other.getExifModel() == srcImg.getExifModel()
&& other.getExifMake() == srcImg.getExifMake()
&& other.getExifFocalLength() == srcImg.getExifFocalLength()
)
{
matchingLensNr = pano.getImage(i).getLensNr();
// copy data from other image, just keep
// the file name and reload the exif data (for exposure)
double ev = srcImg.getExposureValue();
srcImg = pano.getSrcImage(i);
srcImg.setFilename(filename);
srcImg.setExposureValue(ev);
break;
}
} else if (assumeSimilar) {
// no exiv information, just check image size.
if (other.getSize() == srcImg.getSize() ) {
matchingLensNr = pano.getImage(i).getLensNr();
// copy data from other image, just keep
// the file name
srcImg = pano.getSrcImage(i);
srcImg.setFilename(filename);
break;
}
}
}
if (matchingLensNr == -1) {
// create and add new lens
Lens lens;
lens.setImageSize(srcImg.getSize());
matchingLensNr = pano.addLens(lens);
}
PanoImage img(filename, srcImg.getSize().x, srcImg.getSize().y, (unsigned int) matchingLensNr);
srcImg.setLensNr(matchingLensNr);
int imgNr = pano.addImage(img, vars);
pano.setSrcImage(imgNr, srcImg);
if (imgNr == 0) {
// get initial value for output exposure
PanoramaOptions opts = pano.getOptions();
opts.outputExposureValue = srcImg.getExposureValue();
pano.setOptions(opts);
}
}
pano.changeFinished();
}
void wxLoadPTProjectCmd::execute()
{
PanoCommand::execute();
#ifdef _Hgn1_PANOCOMMAND_H
Panorama& pano = o_pano;
#endif
PanoramaMemento newPano;
int ptoVersion = 0;
std::ifstream in(filename.c_str());
if (newPano.loadPTScript(in, ptoVersion, prefix)) {
pano.setMemento(newPano);
PanoramaOptions opts = pano.getOptions();
// always reset to TIFF_m ...
opts.outputFormat = PanoramaOptions::TIFF_m;
// get enblend and enfuse options from preferences
if (ptoVersion < 2) {
// no options stored in file, use default arguments in config file
opts.enblendOptions = wxConfigBase::Get()->Read(wxT("/Enblend/Args"), wxT(HUGIN_ENBLEND_ARGS)).mb_str(wxConvLocal);
opts.enfuseOptions = wxConfigBase::Get()->Read(wxT("/Enfuse/Args"), wxT(HUGIN_ENFUSE_ARGS)).mb_str(wxConvLocal);
}
pano.setOptions(opts);
unsigned int nImg = pano.getNrOfImages();
wxString basedir;
double focalLength=0;
double cropFactor=0;
bool autopanoSiftFile=false;
SrcPanoImage autopanoSiftRefImg;
for (unsigned int i = 0; i < nImg; i++) {
wxFileName fname(wxString (pano.getImage(i).getFilename().c_str(), HUGIN_CONV_FILENAME));
while (! fname.FileExists()){
// Is file in the new path
if (basedir != wxT("")) {
DEBUG_DEBUG("Old filename: " << pano.getImage(i).getFilename());
std::string fn = utils::stripPath(pano.getImage(i).getFilename());
DEBUG_DEBUG("Old filename, without path): " << fn);
wxString newname(fn.c_str(), HUGIN_CONV_FILENAME);
// GetFullName does only work with local paths (not compatible across platforms)
// wxString newname = fname.GetFullName();
fname.AssignDir(basedir);
fname.SetFullName(newname);
DEBUG_TRACE("filename with new path: " << fname.GetFullPath().mb_str(wxConvLocal));
if (fname.FileExists()) {
pano.setImageFilename(i, (const char *)fname.GetFullPath().mb_str(HUGIN_CONV_FILENAME));
DEBUG_TRACE("New filename set: " << fname.GetFullPath().mb_str(wxConvLocal));
// TODO - set pano dirty flag so that new paths are saved
continue;
}
}
wxMessageBox(wxString::Format(_("Image file not found:\n%s\nPlease select correct image"), fname.GetFullPath().c_str()), _("Image file not found"));
if (basedir == wxT("")) {
basedir = fname.GetPath();
}
// open file dialog
wxFileDialog dlg(MainFrame::Get(), _("Add images"),
basedir, fname.GetFullName(),
HUGIN_WX_FILE_IMG_FILTER, wxOPEN, wxDefaultPosition);
dlg.SetDirectory(basedir);
if (dlg.ShowModal() == wxID_OK) {
pano.setImageFilename(i, (const char *)dlg.GetPath().mb_str(HUGIN_CONV_FILENAME));
// save used path
basedir = dlg.GetDirectory();
DEBUG_INFO("basedir is: " << basedir.mb_str(wxConvLocal));
} else {
PanoramaMemento emptyPano;
pano.setMemento(emptyPano);
// set an empty panorama
pano.changeFinished();
return;
}
fname.Assign(dlg.GetPath());
}
// check if image size is correct
SrcPanoImage srcImg = pano.getSrcImage(i);
//
vigra::ImageImportInfo imginfo(srcImg.getFilename().c_str());
if (srcImg.getSize() != imginfo.size()) {
// adjust size properly.
srcImg.resize(imginfo.size());
}
// check if script contains invalid HFOV
unsigned lNr = pano.getImage(i).getLensNr();
Lens cLens = pano.getLens(lNr);
double hfov = const_map_get(pano.getVariables()[i], "v").getValue();
if (cLens.getProjection() == Lens::RECTILINEAR
&& hfov >= 180 && autopanoSiftFile == false)
{
autopanoSiftFile = true;
// something is wrong here, try to read from exif data (all images)
bool ok = initImageFromFile(srcImg, focalLength, cropFactor);
if (! ok) {
getLensDataFromUser(MainFrame::Get(), srcImg, focalLength, cropFactor);
}
autopanoSiftRefImg = srcImg;
} else if (autopanoSiftFile) {
// need to copy the lens parameters from the first lens.
srcImg.setHFOV(autopanoSiftRefImg.getHFOV());
} else {
// load exif data, but do not apply it
srcImg.readEXIF(focalLength, cropFactor);
}
pano.setSrcImage(i, srcImg);
}
} else {
DEBUG_ERROR("could not load panotools script");
}
in.close();
pano.changeFinished();
}
void wxApplyTemplateCmd::execute()
{
PanoCommand::execute();
#ifdef _Hgn1_PANOCOMMAND_H
Panorama& pano = o_pano;
#endif
wxConfigBase* config = wxConfigBase::Get();
if (pano.getNrOfImages() == 0) {
// TODO: prompt for images!
// add a dummy lens
Lens dummyLens;
pano.addLens(dummyLens);
wxString path = config->Read(wxT("actualPath"), wxT(""));
wxFileDialog dlg(MainFrame::Get(), _("Add images"),
path, wxT(""),
HUGIN_WX_FILE_IMG_FILTER, wxOPEN|wxMULTIPLE , wxDefaultPosition);
dlg.SetDirectory(path);
// remember the image extension
wxString img_ext;
if (config->HasEntry(wxT("lastImageType"))){
img_ext = config->Read(wxT("lastImageType")).c_str();
}
if (img_ext == wxT("all images"))
dlg.SetFilterIndex(0);
else if (img_ext == wxT("jpg"))
dlg.SetFilterIndex(1);
else if (img_ext == wxT("all files"))
dlg.SetFilterIndex(2);
DEBUG_INFO ( "Image extention: " << img_ext.mb_str(wxConvLocal) );
// call the file dialog
if (dlg.ShowModal() == wxID_OK) {
// get the selections
wxArrayString Pathnames;
wxArrayString Filenames;
dlg.GetPaths(Pathnames);
dlg.GetFilenames(Filenames);
// e safe the current path to config
config->Write(wxT("actualPath"), dlg.GetDirectory()); // remember for later
DEBUG_INFO ( wxString::Format(wxT("img_ext: %d"), dlg.GetFilterIndex()).mb_str(wxConvLocal) );
// save the image extension
switch ( dlg.GetFilterIndex() ) {
case 0: config->Write(wxT("lastImageType"), wxT("all images")); break;
case 1: config->Write(wxT("lastImageType"), wxT("jpg")); break;
case 2: config->Write(wxT("lastImageType"), wxT("all files")); break;
}
// add images.
for (unsigned int i=0; i< Pathnames.GetCount(); i++) {
std::string filename = (const char *)Pathnames[i].mb_str(HUGIN_CONV_FILENAME);
vigra::ImageImportInfo inf(filename.c_str());
PanoImage img(filename, inf.width(), inf.height(), 0);
VariableMap vars;
fillVariableMap(vars);
(void)pano.addImage(img, vars);
}
}
}
unsigned int nOldImg = pano.getNrOfImages();
PanoramaMemento newPanoMem;
int ptoVersion = 0;
if (newPanoMem.loadPTScript(in, ptoVersion, "")) {
Panorama newPano;
newPano.setMemento(newPanoMem);
unsigned int nNewImg = newPano.getNrOfImages();
if (nOldImg != nNewImg) {
wxString errMsg = wxString::Format(_("Error, template expects %d images,\ncurrent project contains %d images\n"), nNewImg, nOldImg);
wxMessageBox(errMsg, _("Could not apply template"), wxICON_ERROR);
pano.changeFinished();
return;
}
// check image sizes, and correct parameters if required.
for (unsigned int i = 0; i < nNewImg; i++) {
// check if image size is correct
SrcPanoImage oldSrcImg = pano.getSrcImage(i);
SrcPanoImage newSrcImg = newPano.getSrcImage(i);
// just keep the file name
DEBUG_DEBUG("apply template fn:" << newSrcImg.getFilename() << " real fn: " << oldSrcImg.getFilename());
newSrcImg.setFilename(oldSrcImg.getFilename());
if (oldSrcImg.getSize() != newSrcImg.getSize()) {
// adjust size properly.
newSrcImg.resize(oldSrcImg.getSize());
}
newPano.setSrcImage(i, newSrcImg);
}
// keep old control points.
newPano.setCtrlPoints(pano.getCtrlPoints());
newPanoMem = newPano.getMemento();
pano.setMemento(newPanoMem);
} else {
wxMessageBox(_("Error loading project file"), _("Could not apply template"), wxICON_ERROR);
}
pano.changeFinished();
}
} // namespace