1 /*
2 * Copyright (C) 2007-2008 Anael Orlinski
3 *
4 * This file is part of Panomatic.
5 *
6 * Panomatic is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Panomatic is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Panomatic; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20 
21 #ifndef __detectpano_panodetector_h
22 #define __detectpano_panodetector_h
23 
24 #include "PanoDetectorDefs.h"
25 #include <string>
26 #include <map>
27 #include "../libsurf/Image.h"
28 #include "PointMatch.h"
29 #include "TestCode.h"
30 #include "zthread/Runnable.h"
31 #include "zthread/PoolExecutor.h"
32 
33 class PanoDetector
34 {
35 public:
36 	typedef std::vector<std::string>						FileNameList_t;
37 	typedef std::vector<std::string>::iterator				FileNameListIt_t;
38 	typedef KDTreeSpace::KDTree<KDElemKeyPoint, double>		KPKDTree;
39 	typedef boost::shared_ptr<KPKDTree >					KPKDTreePtr;
40 
41 	PanoDetector();
42 
43 	bool		checkData();
44 	void		printDetails();
45 	void		printHelp();
46 	void		run();
47 
48 
49 	// accessors
setSurfExtended(bool iExtended)50 	inline void setSurfExtended(bool iExtended) { _extendedSurf = iExtended; }
setSurfScoreThreshold(double iScore)51 	inline void setSurfScoreThreshold(double iScore) { _surfScoreThreshold = iScore; }
52 
getSurfExtended()53 	inline bool getSurfExtended() const { return _extendedSurf; }
getSurfScoreThreshold()54 	inline double getSurfScoreThreshold() const { return _surfScoreThreshold; }
55 
setSieve1Width(int iWidth)56 	inline void setSieve1Width(int iWidth) { _sieve1Width = iWidth; }
setSieve1Height(int iHeight)57 	inline void setSieve1Height(int iHeight) { _sieve1Height = iHeight; }
setSieve1Size(int iSize)58 	inline void setSieve1Size(int iSize) { _sieve1Size = iSize; }
getSieve1Width()59 	inline int  getSieve1Width() const { return _sieve1Width; }
getSieve1Height()60 	inline int  getSieve1Height() const { return _sieve1Height; }
getSieve1Size()61 	inline int  getSieve1Size() const { return _sieve1Size; }
62 
setKDTreeSearchSteps(int iSteps)63 	inline void setKDTreeSearchSteps(int iSteps) { _kdTreeSearchSteps = iSteps; }
setKDTreeSecondDistance(double iDist)64 	inline void setKDTreeSecondDistance(double iDist) { _kdTreeSecondDistance = iDist; }
getKDTreeSearchSteps()65 	inline int  getKDTreeSearchSteps() const { return _kdTreeSearchSteps; }
getKDTreeSecondDistance()66 	inline double  getKDTreeSecondDistance() const { return _kdTreeSecondDistance; }
67 
setMinimumMatches(int iMatches)68 	inline void setMinimumMatches(int iMatches) { _minimumMatches = iMatches; }
setRansacIterations(int iIters)69 	inline void setRansacIterations(int iIters) { _ransacIters = iIters; }
setRansacDistanceThreshold(int iDT)70 	inline void setRansacDistanceThreshold(int iDT) { _ransacDistanceThres = iDT; }
getMinimumMatches()71 	inline int  getMinimumMatches() const { return _minimumMatches; }
getRansacIterations()72 	inline int  getRansacIterations() const { return _ransacIters; }
getRansacDistanceThreshold()73 	inline int  getRansacDistanceThreshold() const { return _ransacDistanceThres; }
74 
setSieve2Width(int iWidth)75 	inline void setSieve2Width(int iWidth) { _sieve2Width = iWidth; }
setSieve2Height(int iHeight)76 	inline void setSieve2Height(int iHeight) { _sieve2Height = iHeight; }
setSieve2Size(int iSize)77 	inline void setSieve2Size(int iSize) { _sieve2Size = iSize; }
getSieve2Width()78 	inline int  getSieve2Width() const { return _sieve2Width; }
getSieve2Height()79 	inline int  getSieve2Height() const { return _sieve2Height; }
getSieve2Size()80 	inline int  getSieve2Size() const { return _sieve2Size; }
81 
setLinearMatch(bool iLin)82 	inline void setLinearMatch(bool iLin) { _linearMatch = iLin; }
setLinearMatchLen(int iLen)83 	inline void setLinearMatchLen(int iLen) { _linearMatchLen = iLen; }
getLinearMatch()84 	inline bool getLinearMatch() const { return _linearMatch; }
getLinearMatchLen()85 	inline int  getLinearMatchLen() const { return _linearMatchLen; }
86 
getScale()87 	inline float	getScale() const { return _scale; }
setScale(float iScale)88     inline void setScale(float iScale) { _scale = iScale; }
89 
90 
addFile(const std::string & iFile)91 	inline void addFile(const std::string& iFile) { _files.push_back(iFile); }
92 
93 	//	inline void setNumberOfKeys(int iNumKeys) { _numKeys = iNumKeys; }
setOutputFile(const std::string & iOutputFile)94 	inline void setOutputFile(const std::string& iOutputFile) { _outputFile = iOutputFile; }
setTest(bool iTest)95 	inline void setTest(bool iTest) { _test = iTest; }
getTest()96 	inline bool getTest() const { return _test; }
setCores(int iCores)97 	inline void setCores(int iCores) { _cores = iCores; }
98 
99 	// predeclaration
100 	struct ImgData;
101 	struct MatchData;
102 
103 private:
104 	// options
105 	// setup values
106 	double					_surfScoreThreshold;
107 	bool					_extendedSurf;
108 
109 	int						_sieve1Width;
110 	int						_sieve1Height;
111 	int						_sieve1Size;
112 
113 	int						_kdTreeSearchSteps;
114 	double					_kdTreeSecondDistance;
115 
116 	int						_minimumMatches;
117 	int						_ransacIters;
118 	int						_ransacDistanceThres;
119 
120 	int						_sieve2Width;
121 	int						_sieve2Height;
122 	int						_sieve2Size;
123 
124 	bool					_linearMatch;
125 	int						_linearMatchLen;
126 
127 	bool					_test;
128 	int						_cores;
129     float                    _scale;
130 
131 	// list of files
132 	FileNameList_t			_files;
133 	std::string				_outputFile;
134 
135 	// size of images
136 
137 	void					prepareImages();
138     bool                    checkLoadSuccess();
139 	void					prepareMatches();
140 
141 	void					writeOutput();
142 
143 
144 	// internals
145 public:
146 	struct ImgData
147 	{
148 		std::string		_name;
149 		int				_number;
150 		int				_detectWidth;
151 		int				_detectHeight;
152 		int				_origWidth;
153 		int				_origHeight;
154 		libsurf::Image	_ii;
155 		KeyPointVect_t	_kp;
156 		int				_descLength;
157         bool            _loadFail;
158 
159 		// kdtree
160 		KDElemKeyPointVect_t	_kdv;
161 		KPKDTreePtr				_kd;
162 	};
163 
164 	typedef std::map<std::string, ImgData>					ImgData_t;
165 	typedef std::map<std::string, ImgData>::iterator		ImgDataIt_t;
166 
167 	struct MatchData
168 	{
169 		std::string				_i1_name;
170 		std::string				_i2_name;
171 		ImgData*				_i1;
172 		ImgData*				_i2;
173 		PointMatchVector_t		_matches;
174 	};
175 
176 	typedef std::vector<MatchData>								MatchData_t;
177 	typedef std::vector<MatchData>::iterator					MatchDataIt_t;
178 
179 	// actions
180 	static bool				AnalyzeImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
181 	static bool				FindKeyPointsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
182 	static bool				FilterKeyPointsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
183 	static bool				MakeKeyPointDescriptorsInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
184 	static bool				BuildKDTreesInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
185 	static bool				FreeMemoryInImage(ImgData& ioImgInfo, const PanoDetector& iPanoDetector);
186 
187 	static bool				FindMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
188 	static bool				RansacMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
189 	static bool				FilterMatchesInPair(MatchData& ioMatchData, const PanoDetector& iPanoDetector);
190 
191 private:
192 	ImgData_t				_filesData;
193 	MatchData_t				_matchesData;
194 
195 };
196 
197 
198 
199 #endif // __detectpano_panodetector_h
200