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