1 // -*- c-basic-offset: 4 -*-
2 /** @file hugin_base/algorithms/optimizer/PhotometricOptimizer.h
3  *
4  *  @author Pablo d'Angelo <pablo.dangelo@web.de>
5  *
6  *  $Id: PhotometricOptimizer.h 1931 2007-04-15 20:10:38Z dangelo $
7  *
8  *  This is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2 of the License, or (at your option) any later version.
12  *
13  *  This software is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public
19  *  License along with this software. If not, see
20  *  <http://www.gnu.org/licenses/>.
21  *
22  */
23 
24 #ifndef _PHOTOMETRIC_OPTIMIZER_H_
25 #define _PHOTOMETRIC_OPTIMIZER_H_
26 
27 #include <hugin_shared.h>
28 #include <algorithms/PanoramaAlgorithm.h>
29 #include <algorithms/optimizer/PTOptimizer.h>
30 
31 #include <vector>
32 #include <panodata/PanoramaData.h>
33 #include <appbase/ProgressDisplay.h>
34 #include <vigra_ext/VignettingCorrection.h>
35 
36 namespace HuginBase
37 {
38 
39     class IMPEX PhotometricOptimizer : public TimeConsumingPanoramaAlgorithm
40     {
41 
42         public:
43             ///
44             typedef std::vector<vigra_ext::PointPairRGB> PointPairs;
45 
46             ///
PhotometricOptimizer(PanoramaData & panorama,AppBase::ProgressDisplay * progressDisplay,const OptimizeVector & vars,const PointPairs & correspondences,const float imageStepSize)47             PhotometricOptimizer(PanoramaData& panorama, AppBase::ProgressDisplay* progressDisplay,
48                                  const OptimizeVector& vars,
49                                  const PointPairs& correspondences, const float imageStepSize)
50                 : TimeConsumingPanoramaAlgorithm(panorama, progressDisplay),
51                   o_vars(vars), o_correspondences(correspondences), o_resultError(0.0), o_imageStepSize(imageStepSize)
52             {};
53 
54             ///
~PhotometricOptimizer()55             virtual ~PhotometricOptimizer() {};
56 
57 
58         public:
59             ///
60             static void optimizePhotometric(PanoramaData& pano, const OptimizeVector& vars,
61                                             const PointPairs& correspondences,
62                                             const float imageStepSize,
63                                             AppBase::ProgressDisplay* progress,
64                                             double& error);
65 
66         protected:
67             ///
68             struct VarMapping
69             {
70                 std::string type;
71                 std::set<unsigned> imgs;
72             };
73 
74             ///
75             struct OptimData
76             {
77 
78                 const PanoramaData& m_pano;
79                 std::vector<SrcPanoImage> m_imgs;
80                 std::vector<VarMapping> m_vars;
81                 std::vector<vigra_ext::PointPairRGB> m_data;
82                 double huberSigma;
83                 bool symmetricError;
84 
85                 int m_maxIter;
86                 AppBase::ProgressDisplay* m_progress;
87 
88 
89                 ///
90                 OptimData(const PanoramaData& pano, const OptimizeVector& optvars,
91                           const std::vector<vigra_ext::PointPairRGB>& data,
92                           double mEstimatorSigma, bool symmetric,
93                           int maxIter, AppBase::ProgressDisplay* progress);
94 
95                 /// copy optimisation variables into x
96                 void ToX(double * x);
97 
98                 /// copy new values from x to into this->m_imgs
99                 void FromX(double * x);
100 
101             };
102 
103             static int photometricVis(double *p, double *x, int m, int n, int iter, double sqerror, void * data);
104 
105             ///
106             static void photometricError(double* p, double* x, int m, int n, void* data);
107 
108 
109         public:
110             ///
modifiesPanoramaData()111             virtual bool modifiesPanoramaData() const
112                 { return true; }
113 
114             ///
115             virtual bool runAlgorithm();
116 
117 
118         public:
getResultError()119             double getResultError() const
120             {
121                 // [TODO] if(!hasRunSuccessfully()) DEBUG;
122                 return o_resultError;
123             }
124 
125 
126         protected:
127             const OptimizeVector& o_vars;
128             const PointPairs& o_correspondences;
129             const float o_imageStepSize;
130             double o_resultError;
131     };
132 
133 
134 
135 
136     class IMPEX SmartPhotometricOptimizer : public PhotometricOptimizer, protected SmartOptimizerStub
137     {
138         public:
139             /// local optimize definition.
140             enum PhotometricOptimizeMode {
141                 OPT_PHOTOMETRIC_LDR=0,
142                 OPT_PHOTOMETRIC_LDR_WB,
143                 OPT_PHOTOMETRIC_HDR,
144                 OPT_PHOTOMETRIC_HDR_WB
145             };
146 
147             ///
SmartPhotometricOptimizer(PanoramaData & panorama,AppBase::ProgressDisplay * progressDisplay,const OptimizeVector & vars,const PointPairs & correspondences,const float imageStepSize,PhotometricOptimizeMode optMode)148             SmartPhotometricOptimizer(PanoramaData& panorama, AppBase::ProgressDisplay* progressDisplay,
149                                        const OptimizeVector& vars,
150                                        const PointPairs& correspondences,
151                                        const float imageStepSize,
152                                        PhotometricOptimizeMode optMode)
153                 : PhotometricOptimizer(panorama, progressDisplay, vars, correspondences, imageStepSize), o_optMode(optMode)
154             {};
155 
156             ///
~SmartPhotometricOptimizer()157             virtual ~SmartPhotometricOptimizer() {};
158 
159 
160         public:
161             /** use various heuristics to decide what to optimize.
162              */
163             static void smartOptimizePhotometric(PanoramaData & pano, PhotometricOptimizeMode mode,
164                                                  const std::vector<vigra_ext::PointPairRGB> & correspondences,
165                                                  const float imageStepSize,
166                                                  AppBase::ProgressDisplay* progress,
167                                                  double & error);
168 
169             ///
170             virtual bool runAlgorithm();
171 
172 
173         protected:
174             PhotometricOptimizeMode o_optMode;
175     };
176 
177 
178 
179 } // namespace
180 
181 
182 #endif
183