PanoDetector.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 ; tab-width: 4 -*-
00002 /*
00003 * Copyright (C) 2007-2008 Anael Orlinski
00004 *
00005 * This file is part of Panomatic.
00006 *
00007 * Panomatic is free software; you can redistribute it and/or modify
00008 * it under the terms of the GNU General Public License as published by
00009 * the Free Software Foundation; either version 2 of the License, or
00010 * (at your option) any later version.
00011 *
00012 * Panomatic is distributed in the hope that it will be useful,
00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 * GNU General Public License for more details.
00016 *
00017 * You should have received a copy of the GNU General Public License
00018 * along with Panomatic; if not, write to the Free Software
00019 * <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef __detectpano_panodetector_h
00023 #define __detectpano_panodetector_h
00024 
00025 #include <hugin_config.h>
00026 
00027 #include "PanoDetectorDefs.h"
00028 #include <memory>
00029 #include <string>
00030 #include <map>
00031 #include <localfeatures/Image.h>
00032 #include <localfeatures/PointMatch.h>
00033 #include "TestCode.h"
00034 
00035 #include <localfeatures/KeyPoint.h>
00036 #include <localfeatures/KeyPointDetector.h>
00037 
00038 #include <flann/flann.hpp>
00039 
00040 #include <vigra_ext/ROIImage.h>
00041 
00042 #include <panodata/Panorama.h>
00043 #include <algorithms/optimizer/PTOptimizer.h>
00044 #include <celeste/Celeste.h>
00045 
00046 class PanoDetector
00047 {
00048 public:
00049     typedef std::vector<std::string>                                            FileNameList_t;
00050     typedef std::vector<std::string>::iterator                          FileNameListIt_t;
00051     typedef KDTreeSpace::KDTree<KDElemKeyPoint, double>         KPKDTree;
00052     typedef std::shared_ptr<KPKDTree>        KPKDTreePtr;
00053 
00054     typedef lfeat::KeyPointDetector KeyPointDetector;
00055 
00057     enum MatchingStrategy
00058     {
00059         ALLPAIRS=0,
00060         LINEAR,
00061         MULTIROW,
00062         PREALIGNED
00063     };
00064 
00065     PanoDetector();
00066 
00067     bool checkData();
00068     void printDetails();
00069     void printFilenames();
00070     void printHelp();
00071     void run();
00072     bool match(std::vector<HuginBase::UIntSet> &checkedPairs);
00073     bool matchMultiRow();
00083     bool matchPrealigned(HuginBase::Panorama* pano, std::vector<HuginBase::UIntSet> &connectedImages, std::vector<size_t> imgMap, bool exactOverlap=true);
00084 
00085 
00086     // accessors
00087 
00088     inline HuginBase::Panorama* getPanoramaInfo() const
00089     {
00090         return _panoramaInfo;
00091     }
00092 
00093     inline void setKeyPointsIdx(std::vector<int> keyPointsIdx)
00094     {
00095         _keyPointsIdx = keyPointsIdx;
00096     }
00097     inline std::vector<int> getKeyPointsIdx() const
00098     {
00099         return _keyPointsIdx;
00100     }
00101     inline void setWriteAllKeyPoints(bool writeAllKeyPoints=true)
00102     {
00103         _writeAllKeyPoints = writeAllKeyPoints;
00104     }
00105     inline bool getWriteAllKeyPoints() const
00106     {
00107         return _writeAllKeyPoints;
00108     }
00109 
00110     inline void setVerbose(int level)
00111     {
00112         _verbose = level;
00113     }
00114     inline int  getVerbose() const
00115     {
00116         return _verbose;
00117     }
00118 
00119     inline void setSieve1Width(int iWidth)
00120     {
00121         _sieve1Width = iWidth;
00122     }
00123     inline void setSieve1Height(int iHeight)
00124     {
00125         _sieve1Height = iHeight;
00126     }
00127     inline void setSieve1Size(int iSize)
00128     {
00129         _sieve1Size = iSize;
00130     }
00131     inline int  getSieve1Width() const
00132     {
00133         return _sieve1Width;
00134     }
00135     inline int  getSieve1Height() const
00136     {
00137         return _sieve1Height;
00138     }
00139     inline int  getSieve1Size() const
00140     {
00141         return _sieve1Size;
00142     }
00143 
00144     inline void setKDTreeSearchSteps(int iSteps)
00145     {
00146         _kdTreeSearchSteps = iSteps;
00147     }
00148     inline void setKDTreeSecondDistance(double iDist)
00149     {
00150         _kdTreeSecondDistance = iDist;
00151     }
00152     inline int  getKDTreeSearchSteps() const
00153     {
00154         return _kdTreeSearchSteps;
00155     }
00156     inline double  getKDTreeSecondDistance() const
00157     {
00158         return _kdTreeSecondDistance;
00159     }
00160 
00161     inline void setMinimumMatches(int iMatches)
00162     {
00163         _minimumMatches = iMatches;
00164     }
00165     inline void setRansacIterations(int iIters)
00166     {
00167         _ransacIters = iIters;
00168     }
00169     inline void setRansacDistanceThreshold(int iDT)
00170     {
00171         _ransacDistanceThres = iDT;
00172     }
00173     inline void setRansacMode(HuginBase::RANSACOptimizer::Mode mode)
00174     {
00175         _ransacMode = mode;
00176     }
00177     inline int  getMinimumMatches() const
00178     {
00179         return _minimumMatches;
00180     }
00181     inline int  getRansacIterations() const
00182     {
00183         return _ransacIters;
00184     }
00185     inline int  getRansacDistanceThreshold() const
00186     {
00187         return _ransacDistanceThres;
00188     }
00189     inline HuginBase::RANSACOptimizer::Mode getRansacMode()
00190     {
00191         return _ransacMode;
00192     }
00193 
00194     inline void setSieve2Width(int iWidth)
00195     {
00196         _sieve2Width = iWidth;
00197     }
00198     inline void setSieve2Height(int iHeight)
00199     {
00200         _sieve2Height = iHeight;
00201     }
00202     inline void setSieve2Size(int iSize)
00203     {
00204         _sieve2Size = iSize;
00205     }
00206     inline int  getSieve2Width() const
00207     {
00208         return _sieve2Width;
00209     }
00210     inline int  getSieve2Height() const
00211     {
00212         return _sieve2Height;
00213     }
00214     inline int  getSieve2Size() const
00215     {
00216         return _sieve2Size;
00217     }
00218 
00219     inline void setLinearMatchLen(int iLen)
00220     {
00221         _linearMatchLen = iLen;
00222     }
00223     inline int  getLinearMatchLen() const
00224     {
00225         return _linearMatchLen;
00226     }
00227     inline void setMatchingStrategy(MatchingStrategy iMatchStrategy)
00228     {
00229         _matchingStrategy = iMatchStrategy;
00230     }
00231     inline MatchingStrategy getMatchingStrategy() const
00232     {
00233         return _matchingStrategy;
00234     }
00235 
00236     inline bool getDownscale() const
00237     {
00238         return _downscale;
00239     }
00240     inline void setDownscale(bool iDown)
00241     {
00242         _downscale = iDown;
00243     }
00244 
00245     //  inline void setNumberOfKeys(int iNumKeys) { _numKeys = iNumKeys; }
00246     inline void setOutputFile(const std::string& outputFile)
00247     {
00248         _outputFile = outputFile;
00249         _outputGiven=true;
00250     }
00251     inline void setInputFile(const std::string& inputFile)
00252     {
00253         _inputFile = inputFile;
00254     }
00255     inline void setKeyfilesPath(const std::string& keypath)
00256     {
00257         _keypath = keypath;
00258     }
00259     inline bool getCached() const
00260     {
00261         return _cache;
00262     }
00263     inline void setCached(bool iCached)
00264     {
00265         _cache = iCached;
00266     }
00267     inline bool getCleanup() const
00268     {
00269         return _cleanup;
00270     }
00271     inline void setCleanup(bool iCleanup)
00272     {
00273         _cleanup = iCleanup;
00274     }
00275     inline bool getCeleste() const
00276     {
00277         return _celeste;
00278     };
00279     inline void setCeleste(bool iCeleste)
00280     {
00281         _celeste = iCeleste;
00282     };
00283     inline double getCelesteThreshold() const
00284     {
00285         return _celesteThreshold;
00286     };
00287     inline void setCelesteThreshold(double iCelesteThreshold)
00288     {
00289         _celesteThreshold = iCelesteThreshold;
00290     };
00291     inline int getCelesteRadius() const
00292     {
00293         return _celesteRadius;
00294     };
00295     inline void setCelesteRadius(int iCelesteRadius)
00296     {
00297         _celesteRadius = iCelesteRadius;
00298     };
00299     inline void setTest(bool iTest)
00300     {
00301         _test = iTest;
00302     }
00303     inline bool getTest() const
00304     {
00305         return _test;
00306     }
00307     inline void setCores(int iCores)
00308     {
00309         _cores = iCores;
00310     }
00311 
00312     // predeclaration
00313     struct ImgData;
00314     struct MatchData;
00315 
00316 private:
00317     // prevent copying of class
00318     PanoDetector(const PanoDetector&);
00319     PanoDetector& operator=(const PanoDetector&);
00320 
00321     // options
00322 
00323     bool                                                _writeAllKeyPoints;
00324     std::vector<int>            _keyPointsIdx;
00325 
00326     int                     _verbose;
00327 
00328     int                                         _sieve1Width;
00329     int                                         _sieve1Height;
00330     int                                         _sieve1Size;
00331 
00332     int                                         _kdTreeSearchSteps;
00333     double                                      _kdTreeSecondDistance;
00334 
00335     int                                         _minimumMatches;
00336     HuginBase::RANSACOptimizer::Mode    _ransacMode;
00337     int                                         _ransacIters;
00338     int                                         _ransacDistanceThres;
00339 
00340     int                                         _sieve2Width;
00341     int                                         _sieve2Height;
00342     int                                         _sieve2Size;
00343 
00344     MatchingStrategy _matchingStrategy;
00345     int                                         _linearMatchLen;
00346 
00347     bool                                                _test;
00348     int                                         _cores;
00349     bool                 _downscale;
00350     bool        _cache;
00351     bool        _cleanup;
00352     bool        _celeste;
00353     double      _celesteThreshold;
00354     int         _celesteRadius;
00355     std::string _keypath;
00356     std::string _prefix;
00357 
00358     //  bool                                            _stereoRemap;
00359 
00360     // list of files
00361     std::string                         _outputFile;
00362     bool _outputGiven;
00363     std::string                         _inputFile;
00364 
00365     // Store panorama information
00366     HuginBase::Panorama*                        _panoramaInfo;
00367     HuginBase::Panorama                         _panoramaInfoCopy;
00368 
00370     void buildMultiRowImageSets();
00371 
00373     HuginBase::UIntSet _image_layer;
00375     std::vector<HuginBase::UIntVector> _image_stacks;
00376 
00377     bool                                        loadProject();
00378     bool                        checkLoadSuccess();
00379     void CleanupKeyfiles();
00380 
00381     void                                        writeOutput();
00382     void                                        writeKeyfile(ImgData& imgInfo);
00383 
00384     // internals
00385 public:
00386     struct ImgData
00387     {
00388         std::string                     _name;
00389 
00390         int                                     _number;
00391         int                                     _detectWidth;
00392         int                                     _detectHeight;
00393 
00394         lfeat::Image            _ii;
00395         vigra::BImage           _distancemap;
00396 
00397         bool                            _needsremap;
00398         HuginBase::PanoramaOptions      _projOpts;
00399 
00400         bool                                    _hasakeyfile;
00401         std::string _keyfilename;
00402 
00403         lfeat::KeyPointVect_t   _kp;
00404         int                                     _descLength;
00405         bool               _loadFail;
00406 
00407         // kdtree
00408         flann::Matrix<double> _flann_descriptors;
00409         flann::Index<flann::L2<double> > * _flann_index;
00410 
00411         ImgData()
00412         {
00413             _loadFail = false;
00414             _number = 0;
00415             _detectWidth = 0;
00416             _detectHeight = 0;
00417             _needsremap = false;
00418             _hasakeyfile = false;
00419             _descLength = 0;
00420             _flann_index = NULL;
00421         }
00422     };
00423 
00424     typedef std::map<int, ImgData>                                      ImgData_t;
00425     typedef std::map<int, ImgData>::iterator            ImgDataIt_t;
00426 
00427     struct MatchData
00428     {
00429         ImgData*                                _i1;
00430         ImgData*                                _i2;
00431         lfeat::PointMatchVector_t               _matches;
00432     };
00433 
00434     typedef std::vector<MatchData>                                                              MatchData_t;
00435     typedef std::vector<MatchData>::iterator                                    MatchDataIt_t;
00436 
00437     // actions
00438     static bool                         LoadKeypoints(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00439 
00440     static bool                         AnalyzeImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00441     static bool                         FindKeyPointsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00442     static bool                         FilterKeyPointsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00443     static bool                         MakeKeyPointDescriptorsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00444     static bool             RemapBackKeypoints(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00445     static bool                         BuildKDTreesInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00446     static bool                         FreeMemoryInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00447 
00448     static bool                         FindMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00449     static bool                         RansacMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00450     static bool                         RansacMatchesInPairCam(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00451     static bool                         RansacMatchesInPairHomography(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00452     static bool                         FilterMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00453 
00454 private:
00455     bool LoadSVMModel();
00456     ImgData_t                           _filesData;
00457     struct celeste::svm_model* svmModel;
00458 };
00459 
00461 std::string getKeyfilenameFor(std::string keyfilesPath, std::string filename);
00462 
00463 // dummy panotools progress functions
00464 static int ptProgress( int command, char* argument )
00465 {
00466     return 1;
00467 }
00468 static int ptinfoDlg( int command, char* argument )
00469 {
00470     return 1;
00471 }
00472 
00473 #endif // __detectpano_panodetector_h

Generated on 4 May 2016 for Hugintrunk by  doxygen 1.4.7