PTOptimizer.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00028 #ifndef _PTOPTIMIZER_H
00029 #define _PTOPTIMIZER_H
00030 
00031 #include <algorithms/PanoramaAlgorithm.h>
00032 
00033 #include <hugin_shared.h>
00034 #include <set>
00035 #include <boost/graph/breadth_first_search.hpp>
00036 #include <panodata/PanoramaData.h>
00037 
00038 namespace HuginBase {
00039     
00040     
00042     class IMPEX PTOptimizer : public PanoramaAlgorithm
00043     {
00044     
00045         public:
00047             PTOptimizer(PanoramaData& panorama)
00048              : PanoramaAlgorithm(panorama)
00049             {};
00050         
00052             virtual ~PTOptimizer()
00053             {}
00054             
00055             
00056         public:
00058             virtual bool modifiesPanoramaData() const
00059                 { return true; }
00060             
00062             virtual bool runAlgorithm();
00063     };
00064     
00066     class IMPEX RANSACOptimizer : public PanoramaAlgorithm
00067     {
00068         public:
00069             enum Mode {AUTO, HOMOGRAPHY, RPY, RPYV, RPYVB};
00070 
00072             RANSACOptimizer(PanoramaData& panorama, int i1, int i2, double maxError, Mode mode=RPY)
00073                 : PanoramaAlgorithm(panorama), o_i1(i1), o_i2(i2),
00074                   o_maxError(maxError), o_mode(mode)
00075             {};
00076         
00078             virtual ~RANSACOptimizer()
00079             {}
00080             
00081             
00082         public:
00084             virtual bool modifiesPanoramaData() const
00085                 { return true; }
00086 
00087             static std::vector<int> findInliers(PanoramaData & pano, int i1, int i2, double maxError,
00088                                                 Mode mode=RPY);
00089             
00091             virtual bool runAlgorithm();
00092 
00093         private:
00094             int o_i1, o_i2;
00095             double o_maxError;
00096             std::vector<int> o_inliers;
00097             Mode o_mode;
00098     };
00099     
00100     
00102     class IMPEX AutoOptimise : public PTOptimizer
00103     {
00104         
00105         public:
00107             AutoOptimise(PanoramaData& panorama, bool optRoll=true)
00108              : PTOptimizer(panorama)
00109             {};
00110         
00112             virtual ~AutoOptimise()
00113             {}
00114             
00115         
00116         public:
00118             static void autoOptimise(PanoramaData& pano, bool optRoll=true);
00119             
00120         protected:
00122             class OptimiseVisitor: public boost::default_bfs_visitor
00123             {
00124             public:
00125                 OptimiseVisitor(PanoramaData& pano, const std::set<std::string> & optvec)
00126                     : m_opt(optvec), m_pano(pano)
00127                 {};
00128                 
00130                 template <typename Vertex, typename Graph>
00131                 void discover_vertex(Vertex v, const Graph & g);
00132                 
00134                 VariableMapVector getVariables() const
00135                     { return m_pano.getVariables(); }
00136             
00137 //                ///
00138 //                const CPVector & getCtrlPoints() const
00139 //                    { return m_cps; }
00140             
00141             private:
00142                 const std::set<std::string> & m_opt;
00143                 PanoramaData & m_pano;
00144             };
00145             
00146             
00147         public:
00149             virtual bool runAlgorithm()
00150             {
00151                 autoOptimise(o_panorama);
00152                 return true; // let's hope so.
00153             }
00154 
00155     };
00156     
00158     class IMPEX SmartOptimizerStub
00159     {
00160         public:
00162             enum OptMode {
00163                 OPT_POS=1,
00164                 OPT_B=2, 
00165                 OPT_AC=4, 
00166                 OPT_DE=8, 
00167                 OPT_HFOV=16, 
00168                 OPT_GT=32, 
00169                 OPT_VIG=64, 
00170                 OPT_VIGCENTRE=128, 
00171                 OPT_EXP=256, 
00172                 OPT_WB=512, 
00173                 OPT_RESP=1024
00174             };
00175             
00177             static OptimizeVector createOptVars(const PanoramaData& optPano, int mode, unsigned anchorImg=0);   
00178     };
00179     
00180     class IMPEX SmartOptimise : public PTOptimizer, protected SmartOptimizerStub
00181     {
00182         
00183         public:
00185             SmartOptimise(PanoramaData& panorama)
00186              : PTOptimizer(panorama)
00187             {};
00188         
00190             virtual ~SmartOptimise()
00191             {}
00192         
00193         public:
00195             static void smartOptimize(PanoramaData& pano);
00196         
00197             
00198         public:
00200             virtual bool runAlgorithm()
00201             {
00202                 smartOptimize(o_panorama);
00203                 return true; // let's hope so.
00204             }
00205 
00206     };
00207     
00208 }//namesapce
00209 
00210 
00211 
00212 //==============================================================================
00213 // template implementation
00214 
00215 
00216 #include <algorithms/optimizer/ImageGraph.h>
00217 #include <panotools/PanoToolsOptimizerWrapper.h>
00218 
00219 namespace HuginBase {
00220 
00221 template <typename Vertex, typename Graph>
00222 void AutoOptimise::OptimiseVisitor::discover_vertex(Vertex v, const Graph & g)
00223 {
00224     UIntSet imgs;
00225     imgs.insert(v);
00226     //        VariableMapVector vars(1);
00227 #ifdef DEBUG
00228     std::cerr << "before optim "<< v << " : ";
00229     printVariableMap(std::cerr, m_pano.getImageVariables(v));
00230     std::cerr << std::endl;
00231 #endif
00232     
00233     // collect all optimized neighbours
00234     typename boost::graph_traits<CPGraph>::adjacency_iterator ai;
00235     typename boost::graph_traits<CPGraph>::adjacency_iterator ai_end;
00236     for (boost::tuples::tie(ai, ai_end) = adjacent_vertices(v, g);
00237          ai != ai_end; ++ai)
00238     {
00239         if (*ai != v) {
00240             if ( (get(boost::vertex_color, g))[*ai] != boost::color_traits<boost::default_color_type>::white()) {
00241                 // image has been already optimized, use as anchor
00242                 imgs.insert(unsigned(*ai));
00243                 DEBUG_DEBUG("non white neighbour " << (*ai));
00244             } else {
00245                 DEBUG_DEBUG("white neighbour " << (*ai));
00246             }
00247         }
00248     }
00249     
00250     // get pano with neighbouring images.
00251     PanoramaData& localPano = *(m_pano.getNewSubset(imgs)); // don't forget to delete
00252     
00253     // find number of current image in subset
00254     unsigned currImg = 0;
00255     unsigned cnt=0;
00256     for (UIntSet::const_iterator it= imgs.begin(); it != imgs.end(); ++it) {
00257         if (v == *it) {
00258             currImg = cnt;
00259         }
00260         cnt++;
00261     }
00262     
00263     OptimizeVector optvec(imgs.size());
00264     optvec[currImg] = m_opt;
00265     localPano.setOptimizeVector(optvec);
00266     
00267     if ( imgs.size() > 1) {
00268         DEBUG_DEBUG("optimising image " << v << ", with " << imgs.size() -1 << " already optimised neighbour imgs.");
00269         
00270         PTools::optimize(localPano);
00271         m_pano.updateVariables(unsigned(v), localPano.getImageVariables(currImg));
00272 #ifdef DEBUG
00273         std::cerr << "after optim " << v << " : ";
00274         printVariableMap(std::cerr, m_pano.getImageVariables(v));
00275         std::cerr << std::endl;
00276 #endif
00277     }
00278 
00279     delete &localPano;
00280 }
00281 
00282 } //namespace
00283 #endif //_h

Generated on Tue Jul 29 01:25:42 2014 for Hugintrunk by  doxygen 1.3.9.1