Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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/types.h>
00038 #include <mrpt/vision/link_pragmas.h>
00039
00040 namespace mrpt
00041 {
00042 namespace vision
00043 {
00044 using namespace mrpt::utils;
00045 using namespace mrpt::math;
00046 using namespace std;
00047
00048 class CFeatureList;
00049 class CMatchedFeatureList;
00050
00051 enum TListIdx
00052 {
00053 firstList = 0,
00054 secondList,
00055 bothLists
00056 };
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CFeature, mrpt::utils::CSerializable, VISION_IMPEXP )
00070
00071
00072
00073
00074
00075
00076
00077
00078 class VISION_IMPEXP CFeature : public mrpt::utils::CSerializable
00079 {
00080 friend class CFeatureList;
00081 friend class CMatchedFeatureList;
00082
00083 DEFINE_SERIALIZABLE( CFeature )
00084
00085 public:
00086 float x,y;
00087 TFeatureID ID;
00088 CImage patch;
00089 uint16_t patchSize;
00090 TFeatureType type;
00091 TFeatureTrackStatus track_status;
00092 float response;
00093 float orientation;
00094 float scale;
00095 uint8_t user_flags;
00096 uint16_t nTimesSeen;
00097 uint16_t nTimesNotSeen;
00098 uint16_t nTimesLastSeen;
00099
00100 double depth;
00101 double initialDepth;
00102 TPoint3D p3D;
00103 deque<double> multiScales;
00104 deque<vector<double> > multiOrientations;
00105 deque<vector<vector<int32_t> > > multiHashCoeffs;
00106 bool isPointFeature() const;
00107
00108
00109 struct VISION_IMPEXP TDescriptors
00110 {
00111 TDescriptors();
00112
00113 std::vector<uint8_t> SIFT;
00114 std::vector<float> SURF;
00115 std::vector<float> SpinImg;
00116 uint16_t SpinImg_range_rows;
00117 mrpt::math::CMatrix PolarImg;
00118 mrpt::math::CMatrix LogPolarImg;
00119 bool polarImgsNoRotation;
00120 deque<vector<vector<int32_t> > > multiSIFTDescriptors;
00121
00122 bool hasDescriptorSIFT() const { return !SIFT.empty(); };
00123 bool hasDescriptorSURF() const { return !SURF.empty(); }
00124 bool hasDescriptorSpinImg() const { return !SpinImg.empty(); };
00125 bool hasDescriptorPolarImg() const { return PolarImg.rows()!=0; } ;
00126 bool hasDescriptorLogPolarImg() const { return LogPolarImg.rows()!=0; } ;
00127 bool hasDescriptorMultiSIFT() const {
00128 return (multiSIFTDescriptors.size() > 0 && multiSIFTDescriptors[0].size() > 0);
00129 }
00130 }
00131 descriptors;
00132
00133
00134
00135
00136 bool getFirstDescriptorAsMatrix(mrpt::math::CMatrixFloat &desc) const;
00137
00138
00139
00140
00141
00142 float patchCorrelationTo( const CFeature &oFeature) const;
00143
00144
00145
00146
00147
00148 float descriptorDistanceTo( const CFeature &oFeature, TDescriptorType descriptorToUse = descAny, bool normalize_distances = true ) const;
00149
00150
00151 float descriptorSIFTDistanceTo( const CFeature &oFeature, bool normalize_distances = true ) const;
00152
00153
00154 float descriptorSURFDistanceTo( const CFeature &oFeature, bool normalize_distances = true ) const;
00155
00156
00157 float descriptorSpinImgDistanceTo( const CFeature &oFeature, bool normalize_distances = true ) const;
00158
00159
00160
00161
00162
00163
00164 float descriptorPolarImgDistanceTo(
00165 const CFeature &oFeature,
00166 float &minDistAngle,
00167 bool normalize_distances = true ) const;
00168
00169
00170
00171
00172
00173
00174 float descriptorLogPolarImgDistanceTo(
00175 const CFeature &oFeature,
00176 float &minDistAngle,
00177 bool normalize_distances = true ) const;
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 void saveToTextFile( const std::string &filename, bool APPEND = false );
00192
00193
00194
00195 TFeatureType get_type() const { return type; }
00196
00197
00198 void dumpToTextStream( mrpt::utils::CStream &out) const;
00199
00200 void dumpToConsole() const;
00201
00202
00203
00204 CFeature();
00205
00206
00207 virtual ~CFeature() {}
00208
00209
00210 protected:
00211
00212
00213
00214 static float internal_distanceBetweenPolarImages(
00215 const mrpt::math::CMatrix &desc1,
00216 const mrpt::math::CMatrix &desc2,
00217 float &minDistAngle,
00218 bool normalize_distances,
00219 bool dont_shift_angle );
00220
00221 };
00222
00223
00224
00225
00226
00227
00228
00229 class VISION_IMPEXP CFeatureList : public mrpt::math::KDTreeCapable<CFeatureList>
00230 {
00231 protected:
00232 typedef std::vector<CFeaturePtr> TInternalFeatList;
00233
00234 TInternalFeatList m_feats;
00235
00236 public:
00237
00238 inline TFeatureType get_type() const { return empty() ? featNotDefined : (*begin())->get_type(); }
00239
00240
00241 void saveToTextFile( const std::string &fileName, bool APPEND = false );
00242
00243
00244 void loadFromTextFile( const std::string &fileName );
00245
00246
00247 void copyListFrom( const CFeatureList &otherList );
00248
00249
00250 TFeatureID getMaxID() const;
00251
00252
00253 CFeaturePtr getByID( const TFeatureID &ID ) const;
00254 CFeaturePtr getByID( const TFeatureID &ID, int &out_idx ) const;
00255
00256
00257 void getByMultiIDs( const vector<TFeatureID> &IDs, vector<CFeaturePtr> &out, vector<int> &outIndex ) const;
00258
00259
00260
00261
00262
00263
00264
00265
00266 CFeaturePtr nearest( const float x, const float y, double &max_dist ) const;
00267
00268
00269 CFeatureList();
00270
00271
00272 virtual ~CFeatureList();
00273
00274
00275 inline void mark_kdtree_as_outdated() const { kdtree_mark_as_outdated(); }
00276
00277
00278
00279 typedef TInternalFeatList::iterator iterator;
00280 typedef TInternalFeatList::const_iterator const_iterator;
00281
00282 typedef TInternalFeatList::reverse_iterator reverse_iterator;
00283 typedef TInternalFeatList::const_reverse_iterator const_reverse_iterator;
00284
00285 inline iterator begin() { return m_feats.begin(); }
00286 inline iterator end() { return m_feats.end(); }
00287 inline const_iterator begin() const { return m_feats.begin(); }
00288 inline const_iterator end() const { return m_feats.end(); }
00289
00290 inline reverse_iterator rbegin() { return m_feats.rbegin(); }
00291 inline reverse_iterator rend() { return m_feats.rend(); }
00292 inline const_reverse_iterator rbegin() const { return m_feats.rbegin(); }
00293 inline const_reverse_iterator rend() const { return m_feats.rend(); }
00294
00295 inline iterator erase(const iterator it) { mark_kdtree_as_outdated(); return m_feats.erase(it); }
00296
00297 inline bool empty() const { return m_feats.empty(); }
00298 inline size_t size() const { return m_feats.size(); }
00299
00300 inline void clear() { m_feats.clear(); mark_kdtree_as_outdated(); }
00301 inline void resize(size_t N) { m_feats.resize(N); mark_kdtree_as_outdated(); }
00302
00303 inline void push_back(const CFeaturePtr &f) { mark_kdtree_as_outdated(); m_feats.push_back(f); }
00304
00305 inline CFeaturePtr & operator [](const unsigned int index) { return m_feats[index]; }
00306 inline const CFeaturePtr & operator [](const unsigned int index) const { return m_feats[index]; }
00307
00308
00309
00310
00311
00312
00313
00314
00315 inline size_t kdtree_get_point_count() const { return this->size(); }
00316
00317
00318 inline float kdtree_get_pt(const size_t idx, int dim) const {
00319 ASSERTDEB_(dim==0 || dim==1)
00320 if (dim==0) return m_feats[idx]->x;
00321 else return m_feats[idx]->y;
00322 }
00323
00324
00325 inline float kdtree_distance(const float *p1, const size_t idx_p2,size_t size) const
00326 {
00327 ASSERTDEB_(size==2)
00328
00329 const float d0 = p1[0] - m_feats[idx_p2]->x;
00330 const float d1 = p1[1] - m_feats[idx_p2]->y;
00331 return d0*d0+d1*d1;
00332 }
00333
00334
00335
00336
00337 template <typename BBOX>
00338 bool kdtree_get_bbox(BBOX &bb) const { return false; }
00339
00340
00341
00342
00343
00344
00345 inline float getFeatureX(size_t i) const { return m_feats[i]->x; }
00346 inline float getFeatureY(size_t i) const { return m_feats[i]->y; }
00347 inline TFeatureID getFeatureID(size_t i) const { return m_feats[i]->ID; }
00348 inline float getFeatureResponse(size_t i) const { return m_feats[i]->response; }
00349 inline bool isPointFeature(size_t i) const { return m_feats[i]->isPointFeature(); }
00350 inline float getScale(size_t i) const { return m_feats[i]->scale; }
00351 inline TFeatureTrackStatus getTrackStatus(size_t i) { return m_feats[i]->track_status; }
00352
00353 inline void setFeatureX(size_t i,float x) { m_feats[i]->x=x; }
00354 inline void setFeatureXf(size_t i,float x) { m_feats[i]->x=x; }
00355 inline void setFeatureY(size_t i,float y) { m_feats[i]->y=y; }
00356 inline void setFeatureYf(size_t i,float y) { m_feats[i]->y=y; }
00357
00358 inline void setFeatureID(size_t i,TFeatureID id) { m_feats[i]->ID=id; }
00359 inline void setFeatureResponse(size_t i,float r) { m_feats[i]->response=r; }
00360 inline void setScale(size_t i,float s) { m_feats[i]->scale=s; }
00361 inline void setTrackStatus(size_t i,TFeatureTrackStatus s) { m_feats[i]->track_status=s; }
00362
00363 inline void mark_as_outdated() const { kdtree_mark_as_outdated(); }
00364
00365
00366
00367
00368 };
00369
00370
00371
00372
00373
00374
00375 class VISION_IMPEXP CMatchedFeatureList : public std::deque< std::pair<CFeaturePtr,CFeaturePtr> >
00376 {
00377 public:
00378
00379 inline TFeatureType get_type() const { return empty() ? featNotDefined : (begin()->first)->get_type(); }
00380
00381
00382 void saveToTextFile(const std::string &fileName);
00383
00384
00385 void getBothFeatureLists( CFeatureList &list1, CFeatureList &list2 );
00386
00387
00388 CFeaturePtr getByID( const TFeatureID & ID, const TListIdx &idx );
00389
00390
00391
00392 void getMaxID( const TListIdx &idx, TFeatureID & firstListID, TFeatureID & secondListID );
00393
00394
00395 void updateMaxID( const TListIdx &idx );
00396
00397
00398 inline void setLeftMaxID( const TFeatureID &leftID ){ m_leftMaxID = leftID; }
00399 inline void setRightMaxID( const TFeatureID &rightID ){ m_rightMaxID = rightID; }
00400 inline void setMaxIDs( const TFeatureID &leftID, const TFeatureID &rightID )
00401 {
00402 setLeftMaxID(leftID);
00403 setRightMaxID(rightID);
00404 };
00405
00406
00407 CMatchedFeatureList();
00408
00409
00410 virtual ~CMatchedFeatureList();
00411 protected:
00412 TFeatureID m_leftMaxID, m_rightMaxID;
00413 };
00414
00415
00416
00417
00418 }
00419
00420
00421 namespace utils
00422 {
00423 using namespace ::mrpt::vision;
00424
00425 MRPT_DECLARE_TTYPENAME_PTR(CFeature)
00426 }
00427 }
00428
00429 #endif
00430