Main MRPT website > C++ reference for MRPT 1.9.9
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-2018, 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/img/TPixelCoord.h>
13 #include <mrpt/core/round.h>
15 #include <mrpt/math/CMatrixTemplate.h> // mrpt::math::CMatrixBool
17 #include <mrpt/vision/types.h>
18 #include <mrpt/core/round.h>
19 #include <functional>
20 
21 namespace mrpt
22 {
23 namespace vision
24 {
25 /** \addtogroup mrptvision_features
26  @{ */
27 
28 /** A simple structure for representing one image feature (without descriptor
29  * nor patch) - This is
30  * the template which allows you to select if pixels are represented as
31  * integers or floats.
32  * \sa TSimpleFeature, TSimpleFeaturef
33  */
34 template <typename PIXEL_COORD_TYPE>
36 {
37  /** The type of \a pt */
38  using pixel_coords_t = PIXEL_COORD_TYPE;
39  /** The type of pt.x and pt.y */
40  using pixel_coord_t = typename PIXEL_COORD_TYPE::pixel_coord_t;
41 
42  /** Coordinates in the image */
44  /** ID of the feature */
46  /** Status of the feature tracking process */
48  /** A measure of the "goodness" of the feature (typically, the KLT_response
49  * value) */
50  float response;
51  /** The image octave the image was found in: 0=original image, 1=1/2 image,
52  * 2=1/4 image, etc. */
54  /** A field for any other flags needed by the user (this has not a
55  * predefined meaning) */
57 
58  /** Constructor that only sets the pt.{x,y} values, leaving all other values
59  * to *undefined values*. */
60  template <typename COORD_TYPE>
61  inline TSimpleFeature_templ(const COORD_TYPE x, const COORD_TYPE y)
62  : pt(x, y)
63  {
64  }
65 
66  /** Default constructor, leaves all fields uninitialized */
68  template <typename OTHER_TSIMPLEFEATURE>
69  explicit TSimpleFeature_templ(const OTHER_TSIMPLEFEATURE& o)
70  : pt(o.pt.x, o.pt.y),
71  ID(o.ID),
73  response(o.response),
74  octave(o.octave),
76  {
77  }
78 };
79 
80 /** A simple structure for representing one image feature (without descriptor
81  * nor patch).
82  * \sa TSimpleFeaturef, CFeature, TSimpleFeatureList
83  */
85 
86 /** A version of TSimpleFeature with subpixel precision */
88 
89 template <typename FEATURE>
91 
92 template <>
94 {
95  using coord_t = int;
96 
97  static inline coord_t f2coord(float f) { return mrpt::round(f); }
98 };
99 
100 template <>
102 {
103  using coord_t = float;
104 
105  static inline coord_t f2coord(float f) { return f; }
106 };
107 
108 /** A list of image features using the structure TSimpleFeature for each feature
109  * - capable of KD-tree computations
110  * Users normally use directly the type TSimpleFeatureList &
111  * TSimpleFeaturefList
112  */
113 template <typename FEATURE>
115 {
116  public:
117  using TFeatureVector = std::vector<FEATURE>;
118  using feature_t = FEATURE;
119 
120  /** @name Utilities
121  @{ */
122 
123  /** Returns a const ref to the actual std::vector<> container */
124  const TFeatureVector& getVector() const { return m_feats; }
125  /** Returns the maximum ID of all features in the list, or 0 if it's empty
126  */
128  {
129  if (this->empty()) return 0;
130  TFeatureID maxID = m_feats[0].ID;
131  size_t N = m_feats.size() - 1;
132  for (; N; --N) mrpt::keep_max(maxID, m_feats[N].ID);
133  return maxID;
134  }
135 
136  /** Returns a vector with a LUT of the first feature index per row, to
137  * efficiently look for neighbors, etc.
138  * By default this vector is empty, so if a feature detector is used that
139  * doesn't fill this out, it will remain empty and useless.
140  * \note FASTER detectors do fill this out. In general, a feature list
141  * that dynamically changes will not use this LUT.
142  */
143  const std::vector<size_t>& getFirstIndexPerRowLUT() const
144  {
145  return m_first_index_per_row;
146  }
147  /// \overload
148  std::vector<size_t>& getFirstIndexPerRowLUT()
149  {
150  return m_first_index_per_row;
151  }
152 
153  /** Get a ref to the occupation matrix: this is a user-defined matrix, which
154  * is not updated automatically by this class. */
156  {
157  return m_occupied_sections;
158  }
160  {
161  return m_occupied_sections;
162  }
163 
164  /** @} */
165 
166  /** @name Method and datatypes to emulate a STL container
167  @{ */
170 
171  using reverse_iterator = typename TFeatureVector::reverse_iterator;
172  using const_reverse_iterator =
173  typename TFeatureVector::const_reverse_iterator;
174 
175  inline iterator begin() { return m_feats.begin(); }
176  inline iterator end() { return m_feats.end(); }
177  inline const_iterator begin() const { return m_feats.begin(); }
178  inline const_iterator end() const { return m_feats.end(); }
179  inline reverse_iterator rbegin() { return m_feats.rbegin(); }
180  inline reverse_iterator rend() { return m_feats.rend(); }
181  inline const_reverse_iterator rbegin() const { return m_feats.rbegin(); }
182  inline const_reverse_iterator rend() const { return m_feats.rend(); }
183  inline iterator erase(const iterator& it) { return m_feats.erase(it); }
184  inline bool empty() const { return m_feats.empty(); }
185  inline size_t size() const { return m_feats.size(); }
186  inline void clear()
187  {
188  m_feats.clear();
189  m_first_index_per_row.clear();
190  }
191  inline void resize(size_t N) { m_feats.resize(N); }
192  inline void reserve(size_t N) { m_feats.reserve(N); }
193  inline void push_back(const FEATURE& f) { m_feats.push_back(f); }
194  inline void push_back_fast(const FEATURE& f) { m_feats.push_back(f); }
195  inline void push_back_fast(const int x, const int y)
196  {
197  m_feats.push_back(FEATURE(x, y));
198  }
199 
200  inline FEATURE& operator[](const unsigned int index)
201  {
202  return m_feats[index];
203  }
204  inline const FEATURE& operator[](const unsigned int index) const
205  {
206  return m_feats[index];
207  }
208 
209  inline FEATURE& back() { return m_feats.back(); }
210  inline const FEATURE& back() const { return m_feats.back(); }
211  inline FEATURE& front() { return m_feats.front(); }
212  inline const FEATURE& front() const { return m_feats.front(); }
213  /** @} */
214 
215  /** @name getFeature*() methods for template-based access to feature list
216  @{ */
218  size_t i) const
219  {
220  return m_feats[i].pt.x;
221  }
223  size_t i) const
224  {
225  return m_feats[i].pt.y;
226  }
227  inline TFeatureID getFeatureID(size_t i) const { return m_feats[i].ID; }
228  inline float getFeatureResponse(size_t i) const
229  {
230  return m_feats[i].response;
231  }
232  inline bool isPointFeature(size_t i) const
233  {
235  return true;
236  }
237  inline float getScale(size_t i) const
238  {
239  return static_cast<float>(1 << m_feats[i].octave);
240  }
242  {
243  return m_feats[i].track_status;
244  }
245 
246  inline void setFeatureX(
247  size_t i, typename TSimpleFeatureTraits<FEATURE>::coord_t x)
248  {
249  m_feats[i].pt.x = x;
250  }
251  inline void setFeatureY(
252  size_t i, typename TSimpleFeatureTraits<FEATURE>::coord_t y)
253  {
254  m_feats[i].pt.y = y;
255  }
256 
257  inline void setFeatureXf(size_t i, float x)
258  {
260  }
261  inline void setFeatureYf(size_t i, float y)
262  {
264  }
265 
266  inline void setFeatureID(size_t i, TFeatureID id) { m_feats[i]->ID = id; }
267  inline void setFeatureResponse(size_t i, float r)
268  {
269  m_feats[i]->response = r;
270  }
271  inline void setScale(size_t i, float s)
272  {
273  m_feats[i]->octave = mrpt::round(std::log(s) / std::log(2));
274  }
275  inline void setTrackStatus(size_t i, TFeatureTrackStatus s)
276  {
277  m_feats[i].track_status = s;
278  }
279 
280  inline void mark_as_outdated() const {}
281  /** @} */
282 
283  private:
284  /** The actual container with the list of features */
286  /** A LUT of the first feature index per row, to efficiently look for
287  * neighbors, etc. */
288  std::vector<size_t> m_first_index_per_row;
290 
291 }; // end of class
292 
293 /** A list of image features using the structure TSimpleFeature for each feature
294  * - capable of KD-tree computations */
296 
297 /** A list of image features using the structure TSimpleFeaturef for each
298  * feature - capable of KD-tree computations */
300 
301 /** A helper struct to sort keypoints by their response: It can be used with
302  *these types:
303  * - std::vector<cv::KeyPoint>
304  * - mrpt::vision::TSimpleFeatureList
305  */
306 template <typename FEATURE_LIST>
308  : public std::function<bool(size_t, size_t)>
309 {
310  const FEATURE_LIST& m_data;
311  KeypointResponseSorter(const FEATURE_LIST& data) : m_data(data) {}
312  bool operator()(size_t k1, size_t k2) const
313  {
314  return (m_data[k1].response > m_data[k2].response);
315  }
316 };
317 
318 /** Helper class: KD-tree search class for vector<KeyPoint>:
319  * Call mark_as_outdated() to force rebuilding the kd-tree after modifying the
320  * linked feature list.
321  * \tparam FEAT Can be cv::KeyPoint or mrpt::vision::TSimpleFeature
322  */
323 template <typename FEAT>
325  : public mrpt::math::KDTreeCapable<CFeatureListKDTree<FEAT>>
326 {
327  public:
328  inline void mark_as_outdated()
329  {
332  }
333 
334  const std::vector<FEAT>& m_data;
335  CFeatureListKDTree(const std::vector<FEAT>& data) : m_data(data) {}
336  /** @name Methods that MUST be implemented by children classes of
337  KDTreeCapable
338  @{ */
339 
340  /// Must return the number of data points
341  inline size_t kdtree_get_point_count() const { return m_data.size(); }
342  /// Returns the dim'th component of the idx'th point in the class:
343  inline float kdtree_get_pt(const size_t idx, int dim) const
344  {
345  ASSERTDEB_(dim == 0 || dim == 1);
346  if (dim == 0)
347  return m_data[idx].pt.x;
348  else
349  return m_data[idx].pt.y;
350  }
351 
352  /// Returns the distance between the vector "p1[0:size-1]" and the data
353  /// point with index "idx_p2" stored in the class:
354  inline float kdtree_distance(
355  const float* p1, const size_t idx_p2, size_t size) const
356  {
357  MRPT_UNUSED_PARAM(size); // in release mode
358  ASSERTDEB_(size == 2);
359 
360  const float d0 = p1[0] - m_data[idx_p2].pt.x;
361  const float d1 = p1[1] - m_data[idx_p2].pt.y;
362  return d0 * d0 + d1 * d1;
363  }
364 
365  // Optional bounding-box computation: return false to default to a standard
366  // bbox computation loop.
367  // Return true if the BBOX was already computed by the class and returned
368  // in "bb" so it can be avoided to redo it again.
369  // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3
370  // for point clouds)
371  template <typename BBOX>
372  bool kdtree_get_bbox(BBOX& bb) const
373  {
374  MRPT_UNUSED_PARAM(bb);
375  return false;
376  }
377 
378  /** @} */
379 
380 }; // end CFeatureListKDTree
381 
382 /** @} */ // End of add to module: mrptvision_features
383 
384 } // namespace vision
385 
386 } // namespace mrpt
387 
388 #endif
mrpt::math::CMatrixBool m_occupied_sections
Scalar * iterator
Definition: eigen_plugins.h:26
Helper class: KD-tree search class for vector<KeyPoint>: Call mark_as_outdated() to force rebuilding ...
void setFeatureResponse(size_t i, float r)
uint64_t TFeatureID
Definition of a feature ID.
void setFeatureYf(size_t i, float y)
Declares a matrix of booleans (non serializable).
CFeatureListKDTree(const std::vector< FEAT > &data)
const mrpt::math::CMatrixBool & getOccupiedSectionsMatrix() const
typename TFeatureVector::const_iterator const_iterator
typename PIXEL_COORD_TYPE::pixel_coord_t pixel_coord_t
The type of pt.x and pt.y.
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)
TFeatureVector m_feats
The actual container with the list of features.
GLdouble s
Definition: glext.h:3676
std::vector< size_t > m_first_index_per_row
A LUT of the first feature index per row, to efficiently look for neighbors, etc. ...
typename TFeatureVector::const_reverse_iterator const_reverse_iterator
TFeatureTrackStatus track_status
Status of the feature tracking process.
unsigned char uint8_t
Definition: rptypes.h:41
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:79
void setFeatureX(size_t i, typename TSimpleFeatureTraits< FEATURE >::coord_t x)
TSimpleFeatureTraits< FEATURE >::coord_t getFeatureX(size_t i) const
TSimpleFeature_templ()
Default constructor, leaves all fields uninitialized.
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...
const TFeatureVector & getVector() const
Returns a const ref to the actual std::vector<> container.
PIXEL_COORD_TYPE pixel_coords_t
The type of pt.
GLuint index
Definition: glext.h:4054
const FEATURE & operator[](const unsigned int index) const
uint8_t octave
The image octave the image was found in: 0=original image, 1=1/2 image, 2=1/4 image, etc.
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) ...
typename TFeatureVector::reverse_iterator reverse_iterator
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
TFeatureID ID
ID of the feature.
std::vector< size_t > & getFirstIndexPerRowLUT()
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
Definition: exceptions.h:205
pixel_coords_t pt
Coordinates in the image.
float response
A measure of the "goodness" of the feature (typically, the KLT_response value)
void kdtree_mark_as_outdated() const
To be called by child classes when KD tree data changes.
GLuint id
Definition: glext.h:3909
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
void setFeatureXf(size_t i, float x)
GLenum GLint GLint y
Definition: glext.h:3538
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.
GLsizeiptr size
Definition: glext.h:3923
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:3538
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:3546
iterator erase(const iterator &it)
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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.
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)
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019