[1fab1b]: src / hugin1 / hugin / OptimizePhotometricPanel.cpp Maximize Restore History

Download this file

OptimizePhotometricPanel.cpp    754 lines (653 with data), 27.3 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
// -*- c-basic-offset: 4 -*-
/** @file OptimizePhotometricPanel.cpp
*
* @brief implementation of OptimizePhotometricPanel
*
* @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 <PT/RandomPointSampler.h>
#include <PT/PhotometricOptimizer.h>
#include <PT/PTOptimise.h>
#include "hugin/OptimizePhotometricPanel.h"
#include "hugin/CommandHistory.h"
#include "hugin/MainFrame.h"
#include "base_wx/MyProgressDialog.h"
#include "hugin/config_defaults.h"
#include "base_wx/wxImageCache.h"
using namespace std;
using namespace PT;
using namespace PTools;
using namespace hugin_utils;
using namespace vigra;
using namespace vigra_ext;
//============================================================================
//============================================================================
//============================================================================
BEGIN_EVENT_TABLE(OptimizePhotometricPanel, wxPanel)
EVT_CLOSE(OptimizePhotometricPanel::OnClose)
EVT_BUTTON(XRCID("optimize_photo_frame_optimize"), OptimizePhotometricPanel::OnOptimizeButton)
EVT_BUTTON(XRCID("opt_exp_select"), OptimizePhotometricPanel::OnSelExposure)
EVT_BUTTON(XRCID("opt_exp_clear"), OptimizePhotometricPanel::OnDelExposure)
EVT_BUTTON(XRCID("opt_wb_select"), OptimizePhotometricPanel::OnSelWB)
EVT_BUTTON(XRCID("opt_wb_clear"), OptimizePhotometricPanel::OnDelWB)
EVT_BUTTON(XRCID("opt_vig_select"), OptimizePhotometricPanel::OnSelVignetting)
EVT_BUTTON(XRCID("opt_vig_clear"), OptimizePhotometricPanel::OnDelVignetting)
EVT_BUTTON(XRCID("opt_vigc_select"), OptimizePhotometricPanel::OnSelVignettingCentre)
EVT_BUTTON(XRCID("opt_vigc_clear"), OptimizePhotometricPanel::OnDelVignettingCentre)
EVT_BUTTON(XRCID("opt_resp_select"), OptimizePhotometricPanel::OnSelResponse)
EVT_BUTTON(XRCID("opt_resp_clear"), OptimizePhotometricPanel::OnDelResponse)
EVT_CHOICE(XRCID("optimize_photo_panel_mode"), OptimizePhotometricPanel::OnChangeMode)
END_EVENT_TABLE()
// local optimize definition. need to be in sync with the xrc file
enum OptimizeMode { OPT_LDR=0, OPT_LDR_WB, OPT_HDR, OPT_HDR_WB, OPT_CUSTOM,
OPT_END_MARKER};
OptimizePhotometricPanel::OptimizePhotometricPanel()
: m_pano(0)
{
}
bool OptimizePhotometricPanel::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
long style, const wxString& name)
{
DEBUG_TRACE("");
if (! wxPanel::Create(parent, id, pos, size, style, name)) {
return false;
}
wxXmlResource::Get()->LoadPanel(this, wxT("optimize_photo_panel"));
wxPanel * panel = XRCCTRL(*this, "optimize_photo_panel", wxPanel);
wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
topsizer->Add(panel, 1, wxEXPAND, 0);
SetSizer(topsizer);
m_mosaic_photo_opt_text = XRCCTRL(*this, "mosaic_photo_opt_text", wxStaticText);
m_only_active_images_cb = XRCCTRL(*this, "optimize_photo_only_active_images", wxCheckBox);
DEBUG_ASSERT(m_only_active_images_cb);
m_only_active_images_cb->SetValue(wxConfigBase::Get()->Read(wxT("/OptimizeOptimizePhotometricPanelPanel/OnlyActiveImages"),1l) != 0);
m_vig_list = XRCCTRL(*this, "optimize_photo_vig_list", wxCheckListBox);
DEBUG_ASSERT(m_vig_list);
m_vigc_list = XRCCTRL(*this, "optimize_photo_vigc_list", wxCheckListBox);
DEBUG_ASSERT(m_vigc_list);
m_exp_list = XRCCTRL(*this, "optimize_photo_exp_list", wxCheckListBox);
DEBUG_ASSERT(m_exp_list);
m_wb_list = XRCCTRL(*this, "optimize_photo_wb_list", wxCheckListBox);
DEBUG_ASSERT(m_wb_list);
m_resp_list = XRCCTRL(*this, "optimize_photo_resp_list", wxCheckListBox);
DEBUG_ASSERT(m_resp_list);
m_mode_cb = XRCCTRL(*this, "optimize_photo_panel_mode", wxChoice);
DEBUG_ASSERT(m_mode_cb);
m_opt_ctrls = XRCCTRL(*this, "optimize_photo_controls_panel", wxScrolledWindow);
DEBUG_ASSERT(m_opt_ctrls);
m_opt_ctrls->SetSizeHints(20, 20);
m_opt_ctrls->FitInside();
m_opt_ctrls->SetScrollRate(10, 10);
// disable the optimize panel controls by default
XRCCTRL(*this, "optimize_photo_frame_optimize", wxButton)->Disable();
m_mode_cb->Disable();
return true;
}
void OptimizePhotometricPanel::Init(Panorama * panorama)
{
m_pano = panorama;
// observe the panorama
m_pano->addObserver(this);
variable_groups = new HuginBase::ConstStandardImageVariableGroups(*m_pano);
wxCommandEvent dummy;
dummy.SetInt(m_mode_cb->GetSelection());
OnChangeMode(dummy);
DisplayMosaicModeHelp();
}
OptimizePhotometricPanel::~OptimizePhotometricPanel()
{
DEBUG_TRACE("dtor, writing config");
wxConfigBase::Get()->Write(wxT("/OptimizePhotometricPanel/OnlyActiveImages"),m_only_active_images_cb->IsChecked() ? 1l : 0l);
m_pano->removeObserver(this);
DEBUG_TRACE("dtor end");
}
void OptimizePhotometricPanel::DisplayMosaicModeHelp()
{
if (m_pano->isMosaicNotPano()) {
//cout << "in mosaic mode in OptimizePhotometricPanel::panoramaChanged()" << endl;
//XRCCTRL(*this, "mosaic_warning_text", wxStaticText)->Enable();
XRCCTRL(*this, "mosaic_photo_opt_text", wxStaticText)->SetLabel(wxString::Format(wxT("Hugin is in MOSAIC mode. \nDefault Low dynamic range settings correct for differences in exposure, vignetting, and camera response.")));
XRCCTRL(*this, "mosaic_photo_opt_text", wxStaticText)->Wrap(600);
}
else {
XRCCTRL(*this, "mosaic_photo_opt_text", wxStaticText)->SetLabel(wxString::Format(wxT("")));
}
}
void OptimizePhotometricPanel::OnOptimizeButton(wxCommandEvent & e)
{
DEBUG_TRACE("");
// run optimizer
// take the OptimizeVector from somewhere.
//OptimizeVector optvars = getOptimizeVector();
//m_pano->setOptimizeVector(optvars);
UIntSet imgs;
if (m_only_active_images_cb->IsChecked()) {
// use only selected images.
imgs = m_pano->getActiveImages();
if (imgs.size() == 0) {
//FIXME: Pop-up a dialog stating no images have been selected for optimization.
return;
}
} else {
for (unsigned int i = 0 ; i < m_pano->getNrOfImages(); i++) {
imgs.insert(i);
}
}
runOptimizer(imgs);
}
void OptimizePhotometricPanel::OnSelExposure(wxCommandEvent & e)
{
SetCheckMark(m_exp_list,true, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnDelExposure(wxCommandEvent & e)
{
SetCheckMark(m_exp_list,false, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnSelWB(wxCommandEvent & e)
{
SetCheckMark(m_wb_list,true, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnDelWB(wxCommandEvent & e)
{
SetCheckMark(m_wb_list,false, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnSelVignetting(wxCommandEvent & e)
{
SetCheckMark(m_vig_list,true, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnDelVignetting(wxCommandEvent & e)
{
SetCheckMark(m_vig_list,false, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnSelVignettingCentre(wxCommandEvent & e)
{
SetCheckMark(m_vigc_list,true, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnDelVignettingCentre(wxCommandEvent & e)
{
SetCheckMark(m_vigc_list,false, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnSelResponse(wxCommandEvent & e)
{
SetCheckMark(m_resp_list,true, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::OnDelResponse(wxCommandEvent & e)
{
SetCheckMark(m_resp_list,false, m_pano->getOptions().colorReferenceImage);
}
void OptimizePhotometricPanel::SetCheckMark(wxCheckListBox * l, int check, int anchor)
{
// TODO: (un)check all linked variables as well!
int n = l->GetCount();
for (int i=0; i < n; i++) {
if (check) {
l->Check(i, i != anchor);
} else {
l->Check(i, 0);
}
}
}
OptimizeVector OptimizePhotometricPanel::getOptimizeVector()
{
int nrLI = m_exp_list->GetCount();
assert(nrLI >=0);
unsigned int nr = (unsigned int) nrLI;
unsigned int nImages = m_pano->getNrOfImages();
assert(nr == nImages);
OptimizeVector optvars;
for (unsigned int i=0; i < nImages; i++) {
set<string> imgopt;
// lens variables
unsigned int lensNr = variable_groups->getLenses().getPartNumber(i);
if (m_vig_list->IsChecked(lensNr)) {
imgopt.insert("Vb");
imgopt.insert("Vc");
imgopt.insert("Vd");
}
if (m_vigc_list->IsChecked(lensNr)) {
imgopt.insert("Vx");
imgopt.insert("Vy");
}
if (m_resp_list->IsChecked(lensNr)) {
imgopt.insert("Ra");
imgopt.insert("Rb");
imgopt.insert("Rc");
imgopt.insert("Rd");
imgopt.insert("Re");
}
// image variables
if (m_exp_list->IsChecked(i)) {
imgopt.insert("Eev");
}
if (m_wb_list->IsChecked(i)) {
imgopt.insert("Er");
imgopt.insert("Eb");
}
optvars.push_back(imgopt);
}
return optvars;
}
void OptimizePhotometricPanel::panoramaChanged(PT::Panorama & pano)
{
DEBUG_TRACE("");
variable_groups->update();
// update accordingly to the choosen mode
// wxCommandEvent dummy;
// OnChangeMode(dummy);
DisplayMosaicModeHelp();
}
void OptimizePhotometricPanel::panoramaImagesChanged(PT::Panorama &pano,
const PT::UIntSet & imgNr)
{
DisplayMosaicModeHelp();
DEBUG_TRACE("nr of changed images: " << imgNr.size());
variable_groups->update();
if (pano.getNrOfImages() <= 1)
{
XRCCTRL(*this, "optimize_photo_frame_optimize", wxButton)->Disable();
m_mode_cb->Disable();
} else {
XRCCTRL(*this, "optimize_photo_frame_optimize", wxButton)->Enable();
m_mode_cb->Enable();
}
// update lens values
int nrLensList = m_vig_list->GetCount();
assert(nrLensList >=0);
unsigned int nr = (unsigned int) nrLensList;
unsigned int nLens = variable_groups->getLenses().getNumberOfParts();
while (nr < nLens) {
// add checkboxes.
m_vig_list->Append(wxString::Format(wxT("%d"),nr));
m_vigc_list->Append(wxString::Format(wxT("%d"),nr));
m_resp_list->Append(wxString::Format(wxT("%d"),nr));
nr++;
}
while (nr > nLens) {
if (nr == 0)
break;
m_vig_list->Delete(nr-1);
m_vigc_list->Delete(nr-1);
m_resp_list->Delete(nr-1);
nr--;
}
// add/remove items
int nrLI = m_exp_list->GetCount();
assert(nrLI >=0);
nr = (unsigned int) nrLI;
unsigned int nImages = m_pano->getNrOfImages();
while (nr < nImages) {
// add checkboxes.
m_exp_list->Append(wxString::Format(wxT("%d"),nr));
m_wb_list->Append(wxString::Format(wxT("%d"),nr));
nr++;
}
while (nr > nImages) {
if (nr == 0)
break;
m_exp_list->Delete(nr-1);
m_wb_list->Delete(nr-1);
nr--;
}
// display values of the variables.
UIntSet::const_iterator it;
for (it = imgNr.begin(); it != imgNr.end(); it++) {
DEBUG_DEBUG("setting checkmarks for image " << *it)
const VariableMap & vars = pano.getImageVariables(*it);
// keep selections
bool sel = m_exp_list->IsChecked(*it);
m_exp_list->SetString(*it, wxString::Format(wxT("%d (%.3f)"),
*it, const_map_get(vars,"Eev").getValue()));
m_exp_list->Check(*it,sel);
sel = m_wb_list->IsChecked(*it);
m_wb_list->SetString(*it, wxString::Format(wxT("%d (%.3f, %.3f)"),
*it, const_map_get(vars,"Er").getValue(),
const_map_get(vars,"Eb").getValue()));
m_wb_list->Check(*it,sel);
}
// display lens values if they are linked
for (unsigned int i=0; i < nLens; i++) {
const Lens & lens = variable_groups->getLens(i);
const LensVariable & vb = const_map_get(lens.variables,"Vb");
const LensVariable & vc = const_map_get(lens.variables,"Vc");
const LensVariable & vd = const_map_get(lens.variables,"Vd");
bool sel = m_vig_list->IsChecked(i);
if (vb.isLinked()) {
m_vig_list->SetString(i,wxString::Format(wxT("%d (%.1f, %.1f, %.1f)"),i, vb.getValue(),
vc.getValue(), vd.getValue()));
} else {
m_vig_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_vig_list->Check(i,sel);
sel = m_vigc_list->IsChecked(i);
const LensVariable & vx = const_map_get(lens.variables,"Vx");
const LensVariable & vy = const_map_get(lens.variables,"Vy");
if (vx.isLinked()) {
m_vigc_list->SetString(i,wxString::Format(wxT("%d (%.1f, %.1f)"),i,
vx.getValue(), vy.getValue()));
} else {
m_vigc_list->SetString(i,wxString::Format(wxT("%d"), i));
}
m_vigc_list->Check(i,sel);
sel = m_resp_list->IsChecked(i);
const LensVariable & ra = const_map_get(lens.variables,"Ra");
const LensVariable & rb = const_map_get(lens.variables,"Rb");
const LensVariable & rc = const_map_get(lens.variables,"Rc");
const LensVariable & rd = const_map_get(lens.variables,"Rd");
const LensVariable & re = const_map_get(lens.variables,"Re");
if (ra.isLinked()) {
m_resp_list->SetString(i,wxString::Format(wxT("%d (%.2f, %.2f, %.2f, %.2f, %.2f)"),i,
ra.getValue(), rb.getValue(), rc.getValue(),
rd.getValue(), re.getValue()));
} else {
m_resp_list->SetString(i,wxString::Format(wxT("%d"),i));
}
m_resp_list->Check(i,sel);
}
// update automatic checkmarks
wxCommandEvent dummy;
dummy.SetInt(m_mode_cb->GetSelection());
OnChangeMode(dummy);
}
void OptimizePhotometricPanel::setOptimizeVector(const OptimizeVector & optvec)
{
DEBUG_ASSERT((int)optvec.size() == (int)m_exp_list->GetCount());
for (int i=0; i < (int) variable_groups->getLenses().getNumberOfParts(); i++) {
m_vig_list->Check(i,false);
m_vigc_list->Check(i,false);
m_resp_list->Check(i,false);
}
unsigned int nImages = optvec.size();
for (unsigned int i=0; i < nImages; i++) {
m_exp_list->Check(i,false);
m_wb_list->Check(i,false);
unsigned int lensNr = variable_groups->getLenses().getPartNumber(i);
for(set<string>::const_iterator it = optvec[i].begin();
it != optvec[i].end(); ++it)
{
if (*it == "Eev") {
m_exp_list->Check(i);
}
if (*it == "Er") {
m_wb_list->Check(i);
}
if (*it == "Ra") {
m_resp_list->Check(lensNr);
}
if (*it == "Vb") {
m_vig_list->Check(lensNr);
}
if (*it == "Vx") {
m_vigc_list->Check(lensNr);
}
}
}
}
void OptimizePhotometricPanel::runOptimizer(const UIntSet & imgs)
{
DEBUG_TRACE("");
int mode = m_mode_cb->GetSelection();
// check if vignetting and response are linked, display a warning if they are not
// The variables to check:
const HuginBase::ImageVariableGroup::ImageVariableEnum vars[] = {
HuginBase::ImageVariableGroup::IVE_EMoRParams,
HuginBase::ImageVariableGroup::IVE_ResponseType,
HuginBase::ImageVariableGroup::IVE_VigCorrMode,
HuginBase::ImageVariableGroup::IVE_RadialVigCorrCoeff,
HuginBase::ImageVariableGroup::IVE_RadialVigCorrCenterShift
};
// keep a list of commands needed to fix it:
std::vector<PT::PanoCommand *> commands;
HuginBase::ConstImageVariableGroup & lenses = variable_groups->getLenses();
for (size_t i = 0; i < lenses.getNumberOfParts(); i++)
{
std::set<HuginBase::ImageVariableGroup::ImageVariableEnum> links_needed;
links_needed.clear();
for (int v = 0; v < 5; v++)
{
if (!lenses.getVarLinkedInPart(vars[v], i))
{
links_needed.insert(vars[v]);
}
};
if (!links_needed.empty())
{
commands.push_back(new PT::LinkLensVarsCmd(*m_pano, i, links_needed));
}
}
// if the list of commands is empty, all is good and we don't need a warning.
if (!commands.empty()) {
int ok = wxMessageBox(_("The same vignetting and response parameters should\nbe applied for all images of a lens.\nCurrently each image can have different parameters.\nLink parameters?"), _("Link parameters"), wxYES_NO | wxICON_INFORMATION);
if (ok == wxYES) {
// perform all the commands we stocked up earilier.
for (std::vector<PT::PanoCommand *>::iterator it = commands.begin();
it != commands.end(); it++)
{
GlobalCmdHist::getInstance().addCommand(*it);
}
} else {
// free all the commands, the user doesn't want them used.
for (std::vector<PT::PanoCommand *>::iterator it = commands.begin();
it != commands.end(); it++)
{
delete *it;
}
}
}
Panorama optPano = m_pano->getSubset(imgs);
PanoramaOptions opts = optPano.getOptions();
OptimizeVector optvars;
if(mode==OPT_CUSTOM)
{
optvars = getOptimizeVector();
if (optPano.getNrOfImages() != m_pano->getNrOfImages())
{
OptimizeVector o = optvars;
optvars.clear();
for (UIntSet::const_iterator it = imgs.begin(); it != imgs.end(); ++it)
{
optvars.push_back(o[*it]);
}
}
unsigned int countVar=0;
for(unsigned int i=0;i<optvars.size();i++)
{
countVar+=optvars[i].size();
};
if(countVar==0)
{
wxMessageBox(_("You selected no parameters to optimize.\nTherefore optimization will be canceled."), _("Exposure optimization"), wxOK | wxICON_INFORMATION);
return;
};
};
std::vector<vigra_ext::PointPairRGB> m_points;
// extract points only if not done previously
long nPoints = 200;
wxConfigBase::Get()->Read(wxT("/OptimizePhotometric/nRandomPointsPerImage"), & nPoints);
// get parameters for estimation.
nPoints = wxGetNumberFromUser(_("The vignetting and exposure correction is determined by analysing color values in the overlapping areas.\nTo speed up the computation, only a random subset of points is used."),
_("Number of points per image"),
_("Photometric optimization"), nPoints, 0, 32000,
this);
if (nPoints < 0) {
return;
}
wxConfigBase::Get()->Write(wxT("/OptimizePhotometric/nRandomPointsPerImage"),nPoints);
ProgressReporterDialog progress(5.0, _("Photometric alignment"), _("Loading images"));
progress.Show();
nPoints = nPoints * optPano.getNrOfImages();
// get the small images
std::vector<vigra::FRGBImage *> srcImgs;
for (size_t i=0; i < optPano.getNrOfImages(); i++) {
ImageCache::EntryPtr e = ImageCache::getInstance().getSmallImage(optPano.getImage(i).getFilename());
vigra::FRGBImage * img = new FRGBImage;
if (!e) {
wxMessageBox(_("Error: could not load all images"), _("Error"));
return;
}
if (e->image8 && e->image8->width() > 0) {
reduceToNextLevel(*(e->image8), *img);
transformImage(vigra::srcImageRange(*img), vigra::destImage(*img),
vigra::functor::Arg1()/vigra::functor::Param(255.0));
} else if (e->image16 && e->image16->width() > 0) {
reduceToNextLevel(*(e->image16), *img);
transformImage(vigra::srcImageRange(*img), vigra::destImage(*img),
vigra::functor::Arg1()/vigra::functor::Param(65535.0));
} else {
reduceToNextLevel(*(e->imageFloat), *img);
}
srcImgs.push_back(img);
}
bool randomPoints = true;
extractPoints(optPano, srcImgs, nPoints, randomPoints, progress, m_points);
if (m_points.size() == 0) {
wxMessageBox(_("Error: no overlapping points found, Photometric optimization aborted"), _("Error"));
return;
}
double error = 0;
try {
//wxBusyCursor busyc;
if (mode != OPT_CUSTOM) {
// run automatic optimisation
// ensure that we have a valid anchor.
PanoramaOptions opts = optPano.getOptions();
if (opts.colorReferenceImage >= optPano.getNrOfImages()) {
opts.colorReferenceImage = 0;
optPano.setOptions(opts);
}
smartOptimizePhotometric(optPano, PhotometricOptimizeMode(mode),
m_points, progress, error);
} else {
// optimize selected parameters
optimizePhotometric(optPano, optvars,
m_points, progress, error);
}
} catch (std::exception & error) {
wxMessageBox(_("Internal error during photometric optimization:\n") + wxString(error.what(), wxConvLocal), _("Internal error"));
}
progress.Close();
// display information about the estimation process:
int ret = wxMessageBox(wxString::Format(_("Photometric optimization results:\nAverage difference (RMSE) between overlapping pixels: %.2f gray values (0..255)\n\nApply results?"), error*255),
_("Photometric optimization finished"), wxYES_NO | wxICON_INFORMATION,this);
if (ret == wxYES) {
DEBUG_DEBUG("Applying vignetting corr");
// TODO: merge into a single update command
const VariableMapVector & vars = optPano.getVariables();
GlobalCmdHist::getInstance().addCommand(
new PT::UpdateImagesVariablesCmd(*m_pano, imgs, vars)
);
//now update panorama exposure value
PanoramaOptions opts = m_pano->getOptions();
opts.outputExposureValue = calcMeanExposure(*m_pano);
GlobalCmdHist::getInstance().addCommand(
new PT::SetPanoOptionsCmd(*m_pano, opts)
);
}
}
void OptimizePhotometricPanel::OnClose(wxCloseEvent& event)
{
DEBUG_TRACE("OnClose");
// do not close, just hide if we're not forced
if (event.CanVeto()) {
event.Veto();
Hide();
DEBUG_DEBUG("Hiding");
} else {
DEBUG_DEBUG("Closing");
Destroy();
}
}
void OptimizePhotometricPanel::OnChangeMode(wxCommandEvent & e)
{
DEBUG_TRACE("");
int mode = m_mode_cb->GetSelection();
DEBUG_ASSERT(mode >= 0 && mode < OPT_END_MARKER);
if (m_pano->getNrOfImages() == 0)
{
XRCCTRL(*this, "opt_exp_select", wxButton)->Disable();
XRCCTRL(*this, "opt_wb_select", wxButton)->Disable();
XRCCTRL(*this, "opt_vig_select", wxButton)->Disable();
XRCCTRL(*this, "opt_vigc_select", wxButton)->Disable();
XRCCTRL(*this, "opt_resp_select", wxButton)->Disable();
XRCCTRL(*this, "opt_exp_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_wb_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_vig_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_vigc_clear", wxButton)->Disable();
XRCCTRL(*this, "opt_resp_clear", wxButton)->Disable();
} else {
bool enabled = mode == OPT_CUSTOM;
unsigned int refImg = m_pano->getOptions().colorReferenceImage;
switch (mode) {
case OPT_LDR:
setOptimizeVector(createOptVars(*m_pano, OPT_VIG | OPT_EXP | OPT_RESP, refImg));
break;
case OPT_LDR_WB:
setOptimizeVector(createOptVars(*m_pano, OPT_VIG | OPT_EXP | OPT_RESP | OPT_WB, refImg));
break;
case OPT_HDR:
setOptimizeVector(createOptVars(*m_pano, OPT_VIG | OPT_RESP, refImg));
break;
case OPT_HDR_WB:
setOptimizeVector(createOptVars(*m_pano, OPT_VIG | OPT_RESP | OPT_WB, refImg));
break;
case OPT_CUSTOM:
break;
}
XRCCTRL(*this, "opt_exp_select", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_wb_select", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_vig_select", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_vigc_select", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_resp_select", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_exp_clear", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_wb_clear", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_vig_clear", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_vigc_clear", wxButton)->Enable(enabled);
XRCCTRL(*this, "opt_resp_clear", wxButton)->Enable(enabled);
m_vig_list->Enable(enabled);
m_vigc_list->Enable(enabled);
m_exp_list->Enable(enabled);
m_wb_list->Enable(enabled);
m_resp_list->Enable(enabled);
}
}
IMPLEMENT_DYNAMIC_CLASS(OptimizePhotometricPanel, wxPanel)
OptimizePhotometricPanelXmlHandler::OptimizePhotometricPanelXmlHandler()
: wxXmlResourceHandler()
{
AddWindowStyles();
}
wxObject *OptimizePhotometricPanelXmlHandler::DoCreateResource()
{
XRC_MAKE_INSTANCE(cp, OptimizePhotometricPanel)
cp->Create(m_parentAsWindow,
GetID(),
GetPosition(), GetSize(),
GetStyle(wxT("style")),
GetName());
SetupWindow( cp);
return cp;
}
bool OptimizePhotometricPanelXmlHandler::CanHandle(wxXmlNode *node)
{
return IsOfClass(node, wxT("OptimizePhotometricPanel"));
}
IMPLEMENT_DYNAMIC_CLASS(OptimizePhotometricPanelXmlHandler, wxXmlResourceHandler)