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 "hugin_utils/shared_ptr.h"
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 using namespace HuginBase;
00047 
00048 class PanoDetector
00049 {
00050 public:
00051     typedef std::vector<std::string>                                            FileNameList_t;
00052     typedef std::vector<std::string>::iterator                          FileNameListIt_t;
00053     typedef KDTreeSpace::KDTree<KDElemKeyPoint, double>         KPKDTree;
00054     typedef sharedPtrNamespace::shared_ptr<KPKDTree>        KPKDTreePtr;
00055 
00056     typedef lfeat::KeyPointDetector KeyPointDetector;
00057 
00059     enum MatchingStrategy
00060     {
00061         ALLPAIRS=0,
00062         LINEAR,
00063         MULTIROW,
00064         PREALIGNED
00065     };
00066 
00067     PanoDetector();
00068 
00069     bool checkData();
00070     void printDetails();
00071     void printFilenames();
00072     void printHelp();
00073     void run();
00074     bool match(std::vector<HuginBase::UIntSet> &checkedPairs);
00075     bool matchMultiRow();
00085     bool matchPrealigned(Panorama* pano, std::vector<HuginBase::UIntSet> &connectedImages, std::vector<size_t> imgMap, bool exactOverlap=true);
00086 
00087 
00088     // accessors
00089 
00090     inline Panorama* getPanoramaInfo() const
00091     {
00092         return _panoramaInfo;
00093     }
00094 
00095     inline void setKeyPointsIdx(std::vector<int> keyPointsIdx)
00096     {
00097         _keyPointsIdx = keyPointsIdx;
00098     }
00099     inline std::vector<int> getKeyPointsIdx() const
00100     {
00101         return _keyPointsIdx;
00102     }
00103     inline void setWriteAllKeyPoints(bool writeAllKeyPoints=true)
00104     {
00105         _writeAllKeyPoints = writeAllKeyPoints;
00106     }
00107     inline bool getWriteAllKeyPoints() const
00108     {
00109         return _writeAllKeyPoints;
00110     }
00111 
00112     inline void setVerbose(int level)
00113     {
00114         _verbose = level;
00115     }
00116     inline int  getVerbose() const
00117     {
00118         return _verbose;
00119     }
00120 
00121     inline void setSieve1Width(int iWidth)
00122     {
00123         _sieve1Width = iWidth;
00124     }
00125     inline void setSieve1Height(int iHeight)
00126     {
00127         _sieve1Height = iHeight;
00128     }
00129     inline void setSieve1Size(int iSize)
00130     {
00131         _sieve1Size = iSize;
00132     }
00133     inline int  getSieve1Width() const
00134     {
00135         return _sieve1Width;
00136     }
00137     inline int  getSieve1Height() const
00138     {
00139         return _sieve1Height;
00140     }
00141     inline int  getSieve1Size() const
00142     {
00143         return _sieve1Size;
00144     }
00145 
00146     inline void setKDTreeSearchSteps(int iSteps)
00147     {
00148         _kdTreeSearchSteps = iSteps;
00149     }
00150     inline void setKDTreeSecondDistance(double iDist)
00151     {
00152         _kdTreeSecondDistance = iDist;
00153     }
00154     inline int  getKDTreeSearchSteps() const
00155     {
00156         return _kdTreeSearchSteps;
00157     }
00158     inline double  getKDTreeSecondDistance() const
00159     {
00160         return _kdTreeSecondDistance;
00161     }
00162 
00163     inline void setMinimumMatches(int iMatches)
00164     {
00165         _minimumMatches = iMatches;
00166     }
00167     inline void setRansacIterations(int iIters)
00168     {
00169         _ransacIters = iIters;
00170     }
00171     inline void setRansacDistanceThreshold(int iDT)
00172     {
00173         _ransacDistanceThres = iDT;
00174     }
00175     inline void setRansacMode(RANSACOptimizer::Mode mode)
00176     {
00177         _ransacMode = mode;
00178     }
00179     inline int  getMinimumMatches() const
00180     {
00181         return _minimumMatches;
00182     }
00183     inline int  getRansacIterations() const
00184     {
00185         return _ransacIters;
00186     }
00187     inline int  getRansacDistanceThreshold() const
00188     {
00189         return _ransacDistanceThres;
00190     }
00191     inline RANSACOptimizer::Mode getRansacMode()
00192     {
00193         return _ransacMode;
00194     }
00195 
00196     inline void setSieve2Width(int iWidth)
00197     {
00198         _sieve2Width = iWidth;
00199     }
00200     inline void setSieve2Height(int iHeight)
00201     {
00202         _sieve2Height = iHeight;
00203     }
00204     inline void setSieve2Size(int iSize)
00205     {
00206         _sieve2Size = iSize;
00207     }
00208     inline int  getSieve2Width() const
00209     {
00210         return _sieve2Width;
00211     }
00212     inline int  getSieve2Height() const
00213     {
00214         return _sieve2Height;
00215     }
00216     inline int  getSieve2Size() const
00217     {
00218         return _sieve2Size;
00219     }
00220 
00221     inline void setLinearMatchLen(int iLen)
00222     {
00223         _linearMatchLen = iLen;
00224     }
00225     inline int  getLinearMatchLen() const
00226     {
00227         return _linearMatchLen;
00228     }
00229     inline void setMatchingStrategy(MatchingStrategy iMatchStrategy)
00230     {
00231         _matchingStrategy = iMatchStrategy;
00232     }
00233     inline MatchingStrategy getMatchingStrategy() const
00234     {
00235         return _matchingStrategy;
00236     }
00237 
00238     inline bool getDownscale() const
00239     {
00240         return _downscale;
00241     }
00242     inline void setDownscale(bool iDown)
00243     {
00244         _downscale = iDown;
00245     }
00246 
00247     //  inline void setNumberOfKeys(int iNumKeys) { _numKeys = iNumKeys; }
00248     inline void setOutputFile(const std::string& outputFile)
00249     {
00250         _outputFile = outputFile;
00251         _outputGiven=true;
00252     }
00253     inline void setInputFile(const std::string& inputFile)
00254     {
00255         _inputFile = inputFile;
00256     }
00257     inline void setKeyfilesPath(const std::string& keypath)
00258     {
00259         _keypath = keypath;
00260     }
00261     inline bool getCached() const
00262     {
00263         return _cache;
00264     }
00265     inline void setCached(bool iCached)
00266     {
00267         _cache = iCached;
00268     }
00269     inline bool getCleanup() const
00270     {
00271         return _cleanup;
00272     }
00273     inline void setCleanup(bool iCleanup)
00274     {
00275         _cleanup = iCleanup;
00276     }
00277     inline bool getCeleste() const
00278     {
00279         return _celeste;
00280     };
00281     inline void setCeleste(bool iCeleste)
00282     {
00283         _celeste = iCeleste;
00284     };
00285     inline double getCelesteThreshold() const
00286     {
00287         return _celesteThreshold;
00288     };
00289     inline void setCelesteThreshold(double iCelesteThreshold)
00290     {
00291         _celesteThreshold = iCelesteThreshold;
00292     };
00293     inline int getCelesteRadius() const
00294     {
00295         return _celesteRadius;
00296     };
00297     inline void setCelesteRadius(int iCelesteRadius)
00298     {
00299         _celesteRadius = iCelesteRadius;
00300     };
00301     inline void setTest(bool iTest)
00302     {
00303         _test = iTest;
00304     }
00305     inline bool getTest() const
00306     {
00307         return _test;
00308     }
00309     inline void setCores(int iCores)
00310     {
00311         _cores = iCores;
00312     }
00313 
00314     // predeclaration
00315     struct ImgData;
00316     struct MatchData;
00317 
00318 private:
00319     // prevent copying of class
00320     PanoDetector(const PanoDetector&);
00321     PanoDetector& operator=(const PanoDetector&);
00322 
00323     // options
00324 
00325     bool                                                _writeAllKeyPoints;
00326     std::vector<int>            _keyPointsIdx;
00327 
00328     int                     _verbose;
00329 
00330     int                                         _sieve1Width;
00331     int                                         _sieve1Height;
00332     int                                         _sieve1Size;
00333 
00334     int                                         _kdTreeSearchSteps;
00335     double                                      _kdTreeSecondDistance;
00336 
00337     int                                         _minimumMatches;
00338     RANSACOptimizer::Mode       _ransacMode;
00339     int                                         _ransacIters;
00340     int                                         _ransacDistanceThres;
00341 
00342     int                                         _sieve2Width;
00343     int                                         _sieve2Height;
00344     int                                         _sieve2Size;
00345 
00346     MatchingStrategy _matchingStrategy;
00347     int                                         _linearMatchLen;
00348 
00349     bool                                                _test;
00350     int                                         _cores;
00351     bool                 _downscale;
00352     bool        _cache;
00353     bool        _cleanup;
00354     bool        _celeste;
00355     double      _celesteThreshold;
00356     int         _celesteRadius;
00357     std::string _keypath;
00358     std::string _prefix;
00359 
00360     //  bool                                            _stereoRemap;
00361 
00362     // list of files
00363     std::string                         _outputFile;
00364     bool _outputGiven;
00365     std::string                         _inputFile;
00366 
00367     // Store panorama information
00368     Panorama*                   _panoramaInfo;
00369     Panorama                            _panoramaInfoCopy;
00370 
00372     void buildMultiRowImageSets();
00373 
00375     HuginBase::UIntSet _image_layer;
00377     std::vector<HuginBase::UIntVector> _image_stacks;
00378 
00379     bool                                        loadProject();
00380     bool                        checkLoadSuccess();
00381     void CleanupKeyfiles();
00382 
00383     void                                        writeOutput();
00384     void                                        writeKeyfile(ImgData& imgInfo);
00385 
00386     // internals
00387 public:
00388     struct ImgData
00389     {
00390         std::string                     _name;
00391 
00392         int                                     _number;
00393         int                                     _detectWidth;
00394         int                                     _detectHeight;
00395 
00396         lfeat::Image            _ii;
00397         vigra::BImage           _distancemap;
00398 
00399         bool                            _needsremap;
00400         PanoramaOptions         _projOpts;
00401 
00402         bool                                    _hasakeyfile;
00403         std::string _keyfilename;
00404 
00405         lfeat::KeyPointVect_t   _kp;
00406         int                                     _descLength;
00407         bool               _loadFail;
00408 
00409         // kdtree
00410         flann::Matrix<double> _flann_descriptors;
00411         flann::Index<flann::L2<double> > * _flann_index;
00412 
00413         ImgData()
00414         {
00415             _loadFail = false;
00416             _number = 0;
00417             _detectWidth = 0;
00418             _detectHeight = 0;
00419             _needsremap = false;
00420             _hasakeyfile = false;
00421             _descLength = 0;
00422             _flann_index = NULL;
00423         }
00424     };
00425 
00426     typedef std::map<int, ImgData>                                      ImgData_t;
00427     typedef std::map<int, ImgData>::iterator            ImgDataIt_t;
00428 
00429     struct MatchData
00430     {
00431         ImgData*                                _i1;
00432         ImgData*                                _i2;
00433         lfeat::PointMatchVector_t               _matches;
00434     };
00435 
00436     typedef std::vector<MatchData>                                                              MatchData_t;
00437     typedef std::vector<MatchData>::iterator                                    MatchDataIt_t;
00438 
00439     // actions
00440     static bool                         LoadKeypoints(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00441 
00442     static bool                         AnalyzeImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00443     static bool                         FindKeyPointsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00444     static bool                         FilterKeyPointsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00445     static bool                         MakeKeyPointDescriptorsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00446     static bool             RemapBackKeypoints(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00447     static bool                         BuildKDTreesInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00448     static bool                         FreeMemoryInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
00449 
00450     static bool                         FindMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00451     static bool                         RansacMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00452     static bool                         RansacMatchesInPairCam(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00453     static bool                         RansacMatchesInPairHomography(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00454     static bool                         FilterMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
00455 
00456 private:
00457     bool LoadSVMModel();
00458     ImgData_t                           _filesData;
00459     struct celeste::svm_model* svmModel;
00460 };
00461 
00463 std::string getKeyfilenameFor(std::string keyfilesPath, std::string filename);
00464 
00465 // dummy panotools progress functions
00466 static int ptProgress( int command, char* argument )
00467 {
00468     return 1;
00469 }
00470 static int ptinfoDlg( int command, char* argument )
00471 {
00472     return 1;
00473 }
00474 
00475 #endif // __detectpano_panodetector_h

Generated on 28 Jul 2015 for Hugintrunk by  doxygen 1.4.7