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

Generated on 28 Sep 2016 for Hugintrunk by  doxygen 1.4.7