00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CFeature_H 00029 #define CFeature_H 00030 00031 #include <mrpt/utils/CImage.h> 00032 #include <mrpt/utils/stl_extensions.h> 00033 #include <mrpt/math/CMatrix.h> 00034 #include <mrpt/math/ops_matrices.h> 00035 #include <mrpt/math/KDTreeCapable.h> 00036 00037 #include <mrpt/vision/link_pragmas.h> 00038 00039 namespace mrpt 00040 { 00041 namespace vision 00042 { 00043 using namespace mrpt::utils; 00044 using namespace mrpt::math; 00045 00046 class CFeatureList; 00047 class CMatchedFeatureList; 00048 00049 /** Definition of a feature ID 00050 */ 00051 typedef uint64_t TFeatureID; 00052 00053 /** Types of features - This means that the point has been detected with this algorithm, which is independent of additional descriptors a feature may also have 00054 */ 00055 enum TFeatureType 00056 { 00057 featNotDefined = -1, //!< Non-defined feature (also used for Occupancy features) 00058 featKLT = 0, //!< Kanade-Lucas-Tomasi feature [SHI'94] 00059 featHarris, //!< Harris border and corner detector [HARRIS] 00060 featBCD, //!< Binary corder detector 00061 featSIFT, //!< Scale Invariant Feature Transform [LOWE'04] 00062 featSURF, //!< Speeded Up Robust Feature [BAY'06] 00063 featBeacon, //!< A especial case: this is not an image feature, but a 2D/3D beacon (used for range-only SLAM from mrpt::slam::CLandmark) 00064 featFAST //!< FAST feature detector ("Faster and better: A machine learning approach to corner detection", E. Rosten, R. Porter and T. Drummond, PAMI, 2009). 00065 }; 00066 00067 /** The bitwise OR combination of values of TDescriptorType are used in CFeatureExtraction::computeDescriptors to indicate which descriptors are to be computed for features. 00068 */ 00069 enum TDescriptorType 00070 { 00071 descAny = 0, //!< Used in some methods to mean "any of the present descriptors" 00072 descSIFT = 1, //!< SIFT descriptors 00073 descSURF = 2, //!< SURF descriptors 00074 descSpinImages = 4, //!< Intensity-domain spin image descriptors 00075 descPolarImages = 8, //!< Polar image descriptor 00076 descLogPolarImages = 16 //!< Log-Polar image descriptor 00077 }; 00078 00079 enum TFeatureTrackStatus 00080 { 00081 // Init value 00082 status_IDLE = 0, //!< Inactive (right after detection, and before being tried to track) 00083 00084 // Ok: 00085 status_TRACKED = 5, //!< Feature correctly tracked 00086 00087 // Bad: 00088 status_OOB = 1, //!< Feature felt Out Of Bounds 00089 status_LOST = 10, //!< Unable to track this feature 00090 00091 // KLT specific: 00092 statusKLT_IDLE = 0, //!< Inactive 00093 statusKLT_OOB = 1, //!< Out Of Bounds (Value identical to status_OOB) 00094 statusKLT_SMALL_DET = 2, //!< Determinant of the matrix too small 00095 statusKLT_LARGE_RESIDUE = 3, //!< Error too big 00096 statusKLT_MAX_RESIDUE = 4, 00097 statusKLT_TRACKED = 5, //!< Feature correctly tracked (Value identical to status_TRACKED) 00098 statusKLT_MAX_ITERATIONS = 6 //!< Iteration maximum reached 00099 }; 00100 00101 typedef TFeatureTrackStatus TKLTFeatureStatus; //!< For backward compatibility 00102 00103 /**************************************************** 00104 Class CFEATURE 00105 *****************************************************/ 00106 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CFeature, mrpt::utils::CSerializable, VISION_IMPEXP ) 00107 00108 /** A generic 2D feature from an image, extracted with \a CFeatureExtraction 00109 * Each feature may have one or more descriptors (see \a descriptors), in addition to an image patch. 00110 * The (Euclidean) distance between descriptors in a pair of features can be computed with descriptorDistanceTo, 00111 * while the similarity of the patches is given by patchCorrelationTo. 00112 */ 00113 class VISION_IMPEXP CFeature : public mrpt::utils::CSerializable 00114 { 00115 friend class CFeatureList; 00116 friend class CMatchedFeatureList; 00117 00118 DEFINE_SERIALIZABLE( CFeature ) 00119 00120 public: 00121 float x,y; //!< Coordinates in the image 00122 TFeatureID ID; //!< ID of the feature 00123 CImage patch; //!< A patch of the image surrounding the feature 00124 uint16_t patchSize; //!< Size of the patch (patchSize x patchSize) (it must be an odd number) 00125 TFeatureType type; //!< Type of the feature: featNotDefined, featSIFT, featKLT, featHarris, featSURF, featBeacon 00126 TFeatureTrackStatus track_status; //!< Status of the feature tracking process (old name: KLT_status) 00127 float response; //!< A measure of the "goodness" of the feature (old name: KLT_val) 00128 float orientation; //!< Main orientation of the feature 00129 float scale; //!< Feature scale into the scale space 00130 uint8_t IDSourceImage; //!< ID of the image from which the feature was extracted. 00131 00132 bool isPointFeature() const; //!< Return false only for Blob detectors (SIFT, SURF) 00133 00134 /** All the possible descriptors this feature may have */ 00135 struct VISION_IMPEXP TDescriptors 00136 { 00137 TDescriptors(); // Initialization 00138 00139 std::vector<unsigned char> SIFT; //!< Feature descriptor 00140 std::vector<float> SURF; //!< Feature descriptor 00141 std::vector<float> SpinImg; //!< The 2D histogram as a single row 00142 uint16_t SpinImg_range_rows; //!< The number of rows (corresponding to range bins in the 2D histogram) of the original matrix from which SpinImg was extracted as a vector. 00143 mrpt::math::CMatrix PolarImg; //!< A polar image centered at the interest point 00144 mrpt::math::CMatrix LogPolarImg; //!< A log-polar image centered at the interest point 00145 bool polarImgsNoRotation; //!< If set to true (manually, default=false) the call to "descriptorDistanceTo" will not consider all the rotations between polar image descriptors (PolarImg, LogPolarImg) 00146 00147 bool hasDescriptorSIFT() const { return !SIFT.empty(); }; //!< Whether this feature has this kind of descriptor 00148 bool hasDescriptorSURF() const { return !SURF.empty(); } //!< Whether this feature has this kind of descriptor 00149 bool hasDescriptorSpinImg() const { return !SpinImg.empty(); }; //!< Whether this feature has this kind of descriptor 00150 bool hasDescriptorPolarImg() const { return size(PolarImg,1)>0; } ; //!< Whether this feature has this kind of descriptor 00151 bool hasDescriptorLogPolarImg() const { return size(LogPolarImg,1)>0; } ; //!< Whether this feature has this kind of descriptor 00152 } 00153 descriptors; 00154 00155 /** Return the first found descriptor, as a matrix. 00156 * \return false on error, i.e. there is no valid descriptor. 00157 */ 00158 bool getFirstDescriptorAsMatrix(mrpt::math::CMatrixFloat &desc) const; 00159 00160 /** Computes the normalized cross-correlation between the patches of this and another feature (normalized in the range [0,1], such as 0=best, 1=worst). 00161 * \note If this or the other features does not have patches or they are of different sizes, an exception will be raised. 00162 * \sa descriptorDistanceTo 00163 */ 00164 float patchCorrelationTo( const CFeature &oFeature) const; 00165 00166 /** Computes the Euclidean Distance between this feature's and other feature's descriptors, using the given descriptor or the first present one. 00167 * \note If descriptorToUse is not descAny and that descriptor is not present in one of the features, an exception will be raised. 00168 * \sa patchCorrelationTo 00169 */ 00170 float descriptorDistanceTo( const CFeature &oFeature, TDescriptorType descriptorToUse = descAny, bool normalize_distances = true ) const; 00171 00172 /** Computes the Euclidean Distance between "this" and the "other" descriptors */ 00173 float descriptorSIFTDistanceTo( const CFeature &oFeature, bool normalize_distances = true ) const; 00174 00175 /** Computes the Euclidean Distance between "this" and the "other" descriptors */ 00176 float descriptorSURFDistanceTo( const CFeature &oFeature, bool normalize_distances = true ) const; 00177 00178 /** Computes the Euclidean Distance between "this" and the "other" descriptors */ 00179 float descriptorSpinImgDistanceTo( const CFeature &oFeature, bool normalize_distances = true ) const; 00180 00181 /** Returns the minimum Euclidean Distance between "this" and the "other" polar image descriptor, for the best shift in orientation. 00182 * \param oFeature The other feature to compare with. 00183 * \param minDistAngle The placeholder for the angle at which the smallest distance is found. 00184 * \return The distance for the best orientation (minimum distance). 00185 */ 00186 float descriptorPolarImgDistanceTo( 00187 const CFeature &oFeature, 00188 float &minDistAngle, 00189 bool normalize_distances = true ) const; 00190 00191 /** Returns the minimum Euclidean Distance between "this" and the "other" log-polar image descriptor, for the best shift in orientation. 00192 * \param oFeature The other feature to compare with. 00193 * \param minDistAngle The placeholder for the angle at which the smallest distance is found. 00194 * \return The distance for the best orientation (minimum distance). 00195 */ 00196 float descriptorLogPolarImgDistanceTo( 00197 const CFeature &oFeature, 00198 float &minDistAngle, 00199 bool normalize_distances = true ) const; 00200 00201 /** Get the type of the feature 00202 */ 00203 TFeatureType get_type() const { return type; } 00204 00205 /** Constructor 00206 */ 00207 CFeature(); 00208 00209 /** Virtual destructor */ 00210 virtual ~CFeature() {} 00211 00212 00213 protected: 00214 00215 /** Internal function used by "descriptorLogPolarImgDistanceTo" and "descriptorPolarImgDistanceTo" 00216 */ 00217 static float internal_distanceBetweenPolarImages( 00218 const mrpt::math::CMatrix &desc1, 00219 const mrpt::math::CMatrix &desc2, 00220 float &minDistAngle, 00221 bool normalize_distances, 00222 bool dont_shift_angle ); 00223 00224 }; // end of class 00225 00226 00227 /**************************************************** 00228 Class CFEATURELIST 00229 *****************************************************/ 00230 /** A list of visual features, to be used as output by detectors, as input/output by trackers, etc. 00231 */ 00232 class VISION_IMPEXP CFeatureList : public mrpt::math::KDTreeCapable //public std::deque<CFeaturePtr> 00233 { 00234 protected: 00235 typedef std::deque<CFeaturePtr> TInternalFeatList; 00236 00237 TInternalFeatList m_feats; //!< The actual container with the list of features 00238 00239 public: 00240 /** The type of the first feature in the list */ 00241 inline TFeatureType get_type() const { return empty() ? featNotDefined : (*begin())->get_type(); } 00242 00243 /** Save feature list to a text file */ 00244 void saveToTextFile( const std::string &fileName, bool APPEND = false ); 00245 00246 /** Save feature list to a text file */ 00247 void loadFromTextFile( const std::string &fileName ); 00248 00249 /** Get the maximum ID into the list */ 00250 TFeatureID getMaxID() const; 00251 00252 /** Get a reference to a Feature from its ID */ 00253 CFeaturePtr getByID( TFeatureID ID ) const; 00254 00255 /** Get a reference to the nearest feature to the a given 2D point (version returning distance to closest feature in "max_dist") 00256 * \param x [IN] The query point x-coordinate 00257 * \param y [IN] The query point y-coordinate 00258 * \param max_dist [IN/OUT] At input: The maximum distance to search for. At output: The actual distance to the feature. 00259 * \return A reference to the found feature, or a NULL smart pointer if none found. 00260 * \note See also all the available KD-tree search methods, listed in mrpt::math::KDTreeCapable 00261 */ 00262 CFeaturePtr nearest( const float x, const float y, double &max_dist ) const; 00263 00264 /** Constructor */ 00265 CFeatureList(); 00266 00267 /** Virtual destructor */ 00268 virtual ~CFeatureList(); 00269 00270 /** Call this when the list of features has been modified so the KD-tree is marked as outdated. */ 00271 inline void mark_kdtree_as_outdated() const { kdtree_mark_as_outdated(); } 00272 00273 /** @name Method and datatypes to emulate a STL container 00274 @{ */ 00275 typedef TInternalFeatList::iterator iterator; 00276 typedef TInternalFeatList::const_iterator const_iterator; 00277 00278 typedef TInternalFeatList::reverse_iterator reverse_iterator; 00279 typedef TInternalFeatList::const_reverse_iterator const_reverse_iterator; 00280 00281 inline iterator begin() { return m_feats.begin(); } 00282 inline iterator end() { return m_feats.end(); } 00283 inline const_iterator begin() const { return m_feats.begin(); } 00284 inline const_iterator end() const { return m_feats.end(); } 00285 00286 inline iterator erase(const iterator it) { mark_kdtree_as_outdated(); return m_feats.erase(it); } 00287 00288 inline bool empty() const { return m_feats.empty(); } 00289 inline size_t size() const { return m_feats.size(); } 00290 00291 inline void clear() { m_feats.clear(); mark_kdtree_as_outdated(); } 00292 inline void resize(size_t N) { m_feats.resize(N); } 00293 00294 inline void push_front(const CFeaturePtr &f) { mark_kdtree_as_outdated(); m_feats.push_front(f); } 00295 inline void push_back(const CFeaturePtr &f) { mark_kdtree_as_outdated(); m_feats.push_back(f); } 00296 00297 inline CFeaturePtr & operator [](const unsigned int index) { return m_feats[index]; } 00298 inline const CFeaturePtr & operator [](const unsigned int index) const { return m_feats[index]; } 00299 00300 /** @} */ 00301 00302 /** @name Virtual methods that MUST be implemented by children classes of KDTreeCapable 00303 @{ */ 00304 00305 /** Must return the number of data points */ 00306 virtual size_t kdtree_get_point_count() const { return size(); } 00307 00308 /** Must fill out the data points in "data", such as the i'th point will be stored in (data[i][0],...,data[i][nDims-1]). */ 00309 virtual void kdtree_fill_point_data(ANNpointArray &data, const int nDims) const; 00310 /** @} */ 00311 00312 00313 }; // end of class 00314 00315 /**************************************************** 00316 Class CMATCHEDFEATURELIST 00317 *****************************************************/ 00318 /** A list of features 00319 */ 00320 class VISION_IMPEXP CMatchedFeatureList : public std::deque< std::pair<CFeaturePtr,CFeaturePtr> > 00321 { 00322 public: 00323 /** The type of the first feature in the list */ 00324 inline TFeatureType get_type() const { return empty() ? featNotDefined : (begin()->first)->get_type(); } 00325 00326 /** Save list of matched features to a text file */ 00327 void saveToTextFile(const std::string &fileName); 00328 00329 /** Constructor */ 00330 CMatchedFeatureList(); 00331 00332 /** Virtual destructor */ 00333 virtual ~CMatchedFeatureList(); 00334 }; // end of class 00335 00336 } // end of namespace 00337 00338 namespace utils 00339 { 00340 using namespace ::mrpt::vision; 00341 // Specialization must occur in the same namespace 00342 MRPT_DECLARE_TTYPENAME_PTR(CFeature) 00343 } 00344 00345 00346 } // end of namespace 00347 00348 #endif 00349
| Page generated by Doxygen 1.7.1 for MRPT 0.9.1 SVN:exported at Fri Aug 6 10:31:35 UTC 2010 |
