Main MRPT website > C++ reference for MRPT 1.5.7
TSimpleFeature.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef _mrpt_vision_TSimpleFeature_H
10 #define _mrpt_vision_TSimpleFeature_H
11 
12 #include <mrpt/utils/TPixelCoord.h>
13 #include <mrpt/utils/round.h>
15 #include <mrpt/math/CMatrixTemplate.h> // mrpt::math::CMatrixBool
17 #include <mrpt/vision/types.h>
18 #include <mrpt/utils/round.h>
19 
21 
22 namespace mrpt
23 {
24  namespace vision
25  {
26  /** \addtogroup mrptvision_features
27  @{ */
28 
29  /** A simple structure for representing one image feature (without descriptor nor patch) - This is
30  * the template which allows you to select if pixels are represented as integers or floats.
31  * \sa TSimpleFeature, TSimpleFeaturef
32  */
33  template <typename PIXEL_COORD_TYPE>
35  {
36  typedef PIXEL_COORD_TYPE pixel_coords_t; //!< The type of \a pt
37  typedef typename PIXEL_COORD_TYPE::pixel_coord_t pixel_coord_t; //!< The type of pt.x and pt.y
38 
39  pixel_coords_t pt; //!< Coordinates in the image
40  TFeatureID ID; //!< ID of the feature
41  TFeatureTrackStatus track_status; //!< Status of the feature tracking process
42  float response; //!< A measure of the "goodness" of the feature (typically, the KLT_response value)
43  uint8_t octave; //!< The image octave the image was found in: 0=original image, 1=1/2 image, 2=1/4 image, etc.
44  uint8_t user_flags; //!< A field for any other flags needed by the user (this has not a predefined meaning)
45 
46  /** Constructor that only sets the pt.{x,y} values, leaving all other values to *undefined values*. */
47  template <typename COORD_TYPE>
48  inline TSimpleFeature_templ(const COORD_TYPE x, const COORD_TYPE y) : pt(x,y) { }
49 
50  /** Default constructor, leaves all fields uninitialized */
52 
53  template <typename OTHER_TSIMPLEFEATURE>
54  explicit TSimpleFeature_templ(const OTHER_TSIMPLEFEATURE &o) :
55  pt(o.pt.x,o.pt.y),
56  ID(o.ID),
58  response(o.response),
59  octave(o.octave),
61  {
62  }
63  };
64 
65  /** A simple structure for representing one image feature (without descriptor nor patch).
66  * \sa TSimpleFeaturef, CFeature, TSimpleFeatureList
67  */
69 
70  /** A version of TSimpleFeature with subpixel precision */
72 
73 
74  template <typename FEATURE> struct TSimpleFeatureTraits;
75 
76  template <> struct TSimpleFeatureTraits<TSimpleFeature> {
77  typedef int coord_t;
78 
79  static inline coord_t f2coord(float f) { return mrpt::utils::round(f); }
80  };
81 
82  template <> struct TSimpleFeatureTraits<TSimpleFeaturef> {
83  typedef float coord_t;
84 
85  static inline coord_t f2coord(float f) { return f; }
86  };
87 
88 
89 
90  /** A list of image features using the structure TSimpleFeature for each feature - capable of KD-tree computations
91  * Users normally use directly the typedef's: TSimpleFeatureList & TSimpleFeaturefList
92  */
93  template <typename FEATURE>
95  {
96  public:
97  typedef std::vector<FEATURE> TFeatureVector;
98  typedef FEATURE feature_t;
99 
100  /** @name Utilities
101  @{ */
102 
103  /** Returns a const ref to the actual std::vector<> container */
104  const TFeatureVector& getVector() const { return m_feats; }
105 
106  /** Returns the maximum ID of all features in the list, or 0 if it's empty */
108  if (this->empty()) return 0;
109  TFeatureID maxID = m_feats[0].ID;
110  size_t N = m_feats.size()-1;
111  for ( ; N ; --N) mrpt::utils::keep_max(maxID, m_feats[N].ID);
112  return maxID;
113  }
114 
115  /** Returns a vector with a LUT of the first feature index per row, to efficiently look for neighbors, etc.
116  * By default this vector is empty, so if a feature detector is used that doesn't fill this out, it will remain empty and useless.
117  * \note FASTER detectors do fill this out. In general, a feature list that dynamically changes will not use this LUT.
118  */
119  const std::vector<size_t> & getFirstIndexPerRowLUT() const { return m_first_index_per_row; }
120  /// \overload
121  std::vector<size_t> & getFirstIndexPerRowLUT() { return m_first_index_per_row; }
122 
123  /** Get a ref to the occupation matrix: this is a user-defined matrix, which is not updated automatically by this class. */
126 
127  /** @} */
128 
129  /** @name Method and datatypes to emulate a STL container
130  @{ */
133 
134  typedef typename TFeatureVector::reverse_iterator reverse_iterator;
135  typedef typename TFeatureVector::const_reverse_iterator const_reverse_iterator;
136 
137  inline iterator begin() { return m_feats.begin(); }
138  inline iterator end() { return m_feats.end(); }
139  inline const_iterator begin() const { return m_feats.begin(); }
140  inline const_iterator end() const { return m_feats.end(); }
141 
142  inline reverse_iterator rbegin() { return m_feats.rbegin(); }
143  inline reverse_iterator rend() { return m_feats.rend(); }
144  inline const_reverse_iterator rbegin() const { return m_feats.rbegin(); }
145  inline const_reverse_iterator rend() const { return m_feats.rend(); }
146 
147  inline iterator erase(const iterator &it) { return m_feats.erase(it); }
148 
149  inline bool empty() const { return m_feats.empty(); }
150  inline size_t size() const { return m_feats.size(); }
151 
152  inline void clear() { m_feats.clear(); m_first_index_per_row.clear(); }
153  inline void resize(size_t N) { m_feats.resize(N); }
154  inline void reserve(size_t N) { m_feats.reserve(N); }
155 
156  inline void push_back(const FEATURE &f) { m_feats.push_back(f); }
157  inline void push_back_fast (const FEATURE &f) { m_feats.push_back(f); }
158  inline void push_back_fast (const int x, const int y) { m_feats.push_back (FEATURE(x,y)); }
159 
160  inline FEATURE & operator [](const unsigned int index) { return m_feats[index]; }
161  inline const FEATURE & operator [](const unsigned int index) const { return m_feats[index]; }
162 
163  inline FEATURE & back() { return m_feats.back(); }
164  inline const FEATURE & back() const { return m_feats.back(); }
165 
166  inline FEATURE & front() { return m_feats.front(); }
167  inline const FEATURE & front() const { return m_feats.front(); }
168 
169  /** @} */
170 
171  /** @name getFeature*() methods for template-based access to feature list
172  @{ */
173  inline typename TSimpleFeatureTraits<FEATURE>::coord_t getFeatureX(size_t i) const { return m_feats[i].pt.x; }
174  inline typename TSimpleFeatureTraits<FEATURE>::coord_t getFeatureY(size_t i) const { return m_feats[i].pt.y; }
175  inline TFeatureID getFeatureID(size_t i) const { return m_feats[i].ID; }
176  inline float getFeatureResponse(size_t i) const { return m_feats[i].response; }
177  inline bool isPointFeature(size_t i) const { MRPT_UNUSED_PARAM(i); return true; }
178  inline float getScale(size_t i) const { return static_cast<float>(1<<m_feats[i].octave); }
179  inline TFeatureTrackStatus getTrackStatus(size_t i) { return m_feats[i].track_status; }
180 
181  inline void setFeatureX(size_t i,typename TSimpleFeatureTraits<FEATURE>::coord_t x) { m_feats[i].pt.x=x; }
182  inline void setFeatureY(size_t i,typename TSimpleFeatureTraits<FEATURE>::coord_t y) { m_feats[i].pt.y=y; }
183 
184  inline void setFeatureXf(size_t i,float x) { m_feats[i].pt.x=TSimpleFeatureTraits<FEATURE>::f2coord(x); }
185  inline void setFeatureYf(size_t i,float y) { m_feats[i].pt.y=TSimpleFeatureTraits<FEATURE>::f2coord(y); }
186 
187  inline void setFeatureID(size_t i,TFeatureID id) { m_feats[i]->ID=id; }
188  inline void setFeatureResponse(size_t i,float r) { m_feats[i]->response=r; }
189  inline void setScale(size_t i,float s) { m_feats[i]->octave=mrpt::utils::round(std::log(s)/std::log(2)); }
190  inline void setTrackStatus(size_t i,TFeatureTrackStatus s) { m_feats[i].track_status=s; }
191 
192  inline void mark_as_outdated() const { }
193  /** @} */
194 
195  private:
196  TFeatureVector m_feats; //!< The actual container with the list of features
197  std::vector<size_t> m_first_index_per_row; //!< A LUT of the first feature index per row, to efficiently look for neighbors, etc.
199 
200  }; // end of class
201 
202  /** A list of image features using the structure TSimpleFeature for each feature - capable of KD-tree computations */
204 
205  /** A list of image features using the structure TSimpleFeaturef for each feature - capable of KD-tree computations */
207 
208 
209  /** A helper struct to sort keypoints by their response: It can be used with these types:
210  * - std::vector<cv::KeyPoint>
211  * - mrpt::vision::TSimpleFeatureList
212  */
213  template <typename FEATURE_LIST>
214  struct KeypointResponseSorter// : public std::binary_function<size_t,size_t,bool>
215  {
216  const FEATURE_LIST &m_data;
217  KeypointResponseSorter( const FEATURE_LIST &data ) : m_data(data) { }
218  bool operator() (size_t k1, size_t k2 ) const {
219  return (m_data[k1].response > m_data[k2].response);
220  }
221  };
222 
223 
224  /** Helper class: KD-tree search class for vector<KeyPoint>:
225  * Call mark_as_outdated() to force rebuilding the kd-tree after modifying the linked feature list.
226  * \tparam FEAT Can be cv::KeyPoint or mrpt::vision::TSimpleFeature
227  */
228  template <typename FEAT>
229  class CFeatureListKDTree : public mrpt::math::KDTreeCapable<CFeatureListKDTree<FEAT> >
230  {
231  public:
233 
234  const std::vector<FEAT> & m_data;
235  CFeatureListKDTree(const std::vector<FEAT> & data) : m_data(data) { }
236 
237 
238  /** @name Methods that MUST be implemented by children classes of KDTreeCapable
239  @{ */
240 
241  /// Must return the number of data points
242  inline size_t kdtree_get_point_count() const { return m_data.size(); }
243 
244  /// Returns the dim'th component of the idx'th point in the class:
245  inline float kdtree_get_pt(const size_t idx, int dim) const {
246  ASSERTDEB_(dim==0 || dim==1)
247  if (dim==0) return m_data[idx].pt.x;
248  else return m_data[idx].pt.y;
249  }
250 
251  /// Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored in the class:
252  inline float kdtree_distance(const float *p1, const size_t idx_p2,size_t size) const
253  {
254  MRPT_UNUSED_PARAM(size); // in release mode
255  ASSERTDEB_(size==2)
256 
257  const float d0 = p1[0] - m_data[idx_p2].pt.x;
258  const float d1 = p1[1] - m_data[idx_p2].pt.y;
259  return d0*d0+d1*d1;
260  }
261 
262  // Optional bounding-box computation: return false to default to a standard bbox computation loop.
263  // Return true if the BBOX was already computed by the class and returned in "bb" so it can be avoided to redo it again.
264  // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)
265  template <typename BBOX>
266  bool kdtree_get_bbox(BBOX &bb) const { MRPT_UNUSED_PARAM(bb); return false; }
267 
268  /** @} */
269 
270  }; // end CFeatureListKDTree
271 
272 
273  /** @} */ // End of add to module: mrptvision_features
274 
275  } // end of namespace
276 
277 } // end of namespace
278 
279 #endif
280 
mrpt::math::CMatrixBool m_occupied_sections
Helper class: KD-tree search class for vector<KeyPoint>: Call mark_as_outdated() to force rebuilding ...
void setFeatureResponse(size_t i, float r)
void setFeatureYf(size_t i, float y)
Declares a matrix of booleans (non serializable).
TFeatureVector::const_reverse_iterator const_reverse_iterator
TSimpleFeatureList_templ< TSimpleFeaturef > TSimpleFeaturefList
A list of image features using the structure TSimpleFeaturef for each feature - capable of KD-tree co...
CFeatureListKDTree(const std::vector< FEAT > &data)
std::vector< FEATURE > TFeatureVector
const mrpt::math::CMatrixBool & getOccupiedSectionsMatrix() const
Scalar * iterator
Definition: eigen_plugins.h:23
TFeatureVector::const_iterator const_iterator
float kdtree_get_pt(const size_t idx, int dim) const
Returns the dim&#39;th component of the idx&#39;th point in the class:
void setFeatureID(size_t i, TFeatureID id)
const Scalar * const_iterator
Definition: eigen_plugins.h:24
TFeatureVector m_feats
The actual container with the list of features.
GLdouble s
Definition: glext.h:3602
std::vector< size_t > m_first_index_per_row
A LUT of the first feature index per row, to efficiently look for neighbors, etc. ...
TFeatureTrackStatus track_status
Status of the feature tracking process.
unsigned char uint8_t
Definition: rptypes.h:43
A helper struct to sort keypoints by their response: It can be used with these types: ...
mrpt::math::CMatrixBool & getOccupiedSectionsMatrix()
Get a ref to the occupation matrix: this is a user-defined matrix, which is not updated automatically...
A generic adaptor class for providing Nearest Neighbor (NN) lookup via the nanoflann library...
Definition: KDTreeCapable.h:67
void setFeatureX(size_t i, typename TSimpleFeatureTraits< FEATURE >::coord_t x)
PIXEL_COORD_TYPE pixel_coords_t
The type of pt.
TSimpleFeatureTraits< FEATURE >::coord_t getFeatureX(size_t i) const
TSimpleFeature_templ()
Default constructor, leaves all fields uninitialized.
const TFeatureVector & getVector() const
Returns a const ref to the actual std::vector<> container.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
GLuint index
Definition: glext.h:3891
PIXEL_COORD_TYPE::pixel_coord_t pixel_coord_t
The type of pt.x and pt.y.
uint8_t octave
The image octave the image was found in: 0=original image, 1=1/2 image, 2=1/4 image, etc.
uint64_t TFeatureID
Definition of a feature ID.
FEATURE & operator[](const unsigned int index)
uint8_t user_flags
A field for any other flags needed by the user (this has not a predefined meaning) ...
TSimpleFeatureList_templ< TSimpleFeature > TSimpleFeatureList
A list of image features using the structure TSimpleFeature for each feature - capable of KD-tree com...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
TFeatureID ID
ID of the feature.
std::vector< size_t > & getFirstIndexPerRowLUT()
TSimpleFeature_templ< mrpt::utils::TPixelCoordf > TSimpleFeaturef
A version of TSimpleFeature with subpixel precision.
TSimpleFeature_templ< mrpt::utils::TPixelCoord > TSimpleFeature
A simple structure for representing one image feature (without descriptor nor patch).
pixel_coords_t pt
Coordinates in the image.
float response
A measure of the "goodness" of the feature (typically, the KLT_response value)
TFeatureVector::reverse_iterator reverse_iterator
void kdtree_mark_as_outdated() const
To be called by child classes when KD tree data changes.
GLuint id
Definition: glext.h:3770
float kdtree_distance(const float *p1, const size_t idx_p2, size_t size) const
Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored ...
const std::vector< FEAT > & m_data
void setTrackStatus(size_t i, TFeatureTrackStatus s)
A simple structure for representing one image feature (without descriptor nor patch) - This is the te...
const_reverse_iterator rend() const
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:26
void setFeatureXf(size_t i, float x)
GLenum GLint GLint y
Definition: glext.h:3516
TFeatureID getMaxID() const
Returns the maximum ID of all features in the list, or 0 if it&#39;s empty.
A list of image features using the structure TSimpleFeature for each feature - capable of KD-tree com...
GLsizeiptr size
Definition: glext.h:3779
TSimpleFeatureTraits< FEATURE >::coord_t getFeatureY(size_t i) const
TFeatureTrackStatus getTrackStatus(size_t i)
TSimpleFeature_templ(const OTHER_TSIMPLEFEATURE &o)
GLenum GLint x
Definition: glext.h:3516
void push_back_fast(const int x, const int y)
const std::vector< size_t > & getFirstIndexPerRowLUT() const
Returns a vector with a LUT of the first feature index per row, to efficiently look for neighbors...
const_reverse_iterator rbegin() const
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
iterator erase(const iterator &it)
TFeatureID getFeatureID(size_t i) const
TSimpleFeature_templ(const COORD_TYPE x, const COORD_TYPE y)
Constructor that only sets the pt.
size_t kdtree_get_point_count() const
Must return the number of data points.
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
float getFeatureResponse(size_t i) const
bool operator()(size_t k1, size_t k2) const
bool kdtree_get_bbox(BBOX &bb) const
KeypointResponseSorter(const FEATURE_LIST &data)
void setFeatureY(size_t i, typename TSimpleFeatureTraits< FEATURE >::coord_t y)



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019