MRPT  2.0.1
TKeyPoint.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/round.h>
12 #include <mrpt/img/TPixelCoord.h>
14 #include <mrpt/vision/types.h>
15 #include <functional>
16 
17 namespace mrpt::vision
18 {
19 /** \addtogroup mrptvision_features
20  @{ */
21 
22 /** Simple structure for image key points. Descriptors are stored separately in
23  * CFeatureList. This is a template to allow using both integers
24  * (TKeyPoint) or floats (TKeyPointf) as pixel coordinates. \sa
25  * TKeyPoint, TKeyPointf
26  */
27 template <typename PIXEL_COORD_TYPE>
29 {
30  /** The type of \a pt */
31  using pixel_coords_t = PIXEL_COORD_TYPE;
32  /** The type of pt.x and pt.y */
33  using pixel_coord_t = typename PIXEL_COORD_TYPE::pixel_coord_t;
34 
35  /** Coordinates in the image */
37 
38  /** ID of the feature */
39  TFeatureID ID{static_cast<TFeatureID>(-1)};
40 
41  /** Status of the feature tracking process */
43 
44  /** A measure of the "goodness" of the feature (typically, the KLT_response
45  * value) */
46  float response{0};
47  /** The image octave the image was found in: 0=original image, 1=1/2 image,
48  * 2=1/4 image, etc. */
49  uint8_t octave{0};
50  /** A field for any other flags needed by the user (this has not a
51  * predefined meaning) */
52  uint8_t user_flags{0};
53 
54  /** Constructor that only sets the pt.{x,y} values, leaving all other values
55  * to *undefined values*. */
56  template <typename COORD_TYPE>
57  inline TKeyPoint_templ(const COORD_TYPE x, const COORD_TYPE y) : pt(x, y)
58  {
59  }
60 
61  /** Default constructor, leaves all fields uninitialized */
62  inline TKeyPoint_templ() = default;
63  template <typename OTHER_TKeyPoint>
64  explicit TKeyPoint_templ(const OTHER_TKeyPoint& o)
65  : pt(o.pt.x, o.pt.y),
66  ID(o.ID),
68  response(o.response),
69  octave(o.octave),
71  {
72  }
73 };
74 
75 /** Simple structure for image key points.
76  * \sa TKeyPointf, CFeature, TKeyPointList
77  */
79 
80 /** A version of TKeyPoint with subpixel precision */
82 
83 template <typename FEATURE>
85 
86 template <>
88 {
89  using coord_t = int;
90 
91  static inline coord_t f2coord(float f) { return mrpt::round(f); }
92 };
93 
94 template <>
96 {
97  using coord_t = float;
98 
99  static inline coord_t f2coord(float f) { return f; }
100 };
101 
102 /** A list of image features using the structure TKeyPoint for each feature
103  * Users normally will use directly: TKeyPointList, TKeyPointfList
104  */
105 template <typename FEATURE>
107 {
108  public:
109  using TFeatureVector = std::vector<FEATURE>;
110  using feature_t = FEATURE;
111 
112  /** @name Utilities
113  @{ */
114 
115  /** Returns a const ref to the actual std::vector<> container */
116  const TFeatureVector& getVector() const { return m_feats; }
117  /** Returns the maximum ID of all features in the list, or 0 if it's empty
118  */
120  {
121  if (this->empty()) return 0;
122  TFeatureID maxID = m_feats[0].ID;
123  size_t N = m_feats.size() - 1;
124  for (; N; --N) mrpt::keep_max(maxID, m_feats[N].ID);
125  return maxID;
126  }
127 
128  /** Returns a vector with a LUT of the first feature index per row, to
129  * efficiently look for neighbors, etc.
130  * By default this vector is empty, so if a feature detector is used that
131  * doesn't fill this out, it will remain empty and useless.
132  * \note FASTER detectors do fill this out. In general, a feature list
133  * that dynamically changes will not use this LUT.
134  */
135  const std::vector<size_t>& getFirstIndexPerRowLUT() const
136  {
137  return m_first_index_per_row;
138  }
139  /// \overload
140  std::vector<size_t>& getFirstIndexPerRowLUT()
141  {
142  return m_first_index_per_row;
143  }
144 
145  /** @} */
146 
147  /** @name Method and datatypes to emulate a STL container
148  @{ */
149  using iterator = typename TFeatureVector::iterator;
150  using const_iterator = typename TFeatureVector::const_iterator;
151 
152  using reverse_iterator = typename TFeatureVector::reverse_iterator;
153  using const_reverse_iterator =
154  typename TFeatureVector::const_reverse_iterator;
155 
156  inline iterator begin() { return m_feats.begin(); }
157  inline iterator end() { return m_feats.end(); }
158  inline const_iterator begin() const { return m_feats.begin(); }
159  inline const_iterator end() const { return m_feats.end(); }
160  inline reverse_iterator rbegin() { return m_feats.rbegin(); }
161  inline reverse_iterator rend() { return m_feats.rend(); }
162  inline const_reverse_iterator rbegin() const { return m_feats.rbegin(); }
163  inline const_reverse_iterator rend() const { return m_feats.rend(); }
164  inline iterator erase(const iterator& it) { return m_feats.erase(it); }
165  inline bool empty() const { return m_feats.empty(); }
166  inline size_t size() const { return m_feats.size(); }
167  inline void clear()
168  {
169  m_feats.clear();
170  m_first_index_per_row.clear();
171  }
172  inline void resize(size_t N) { m_feats.resize(N); }
173  inline void reserve(size_t N) { m_feats.reserve(N); }
174  inline void push_back(const FEATURE& f) { m_feats.push_back(f); }
175  inline void emplace_back(const int x, const int y)
176  {
177  m_feats.emplace_back(x, y);
178  }
179 
180  inline FEATURE& operator[](const std::size_t index)
181  {
182  return m_feats[index];
183  }
184  inline const FEATURE& operator[](const std::size_t index) const
185  {
186  return m_feats[index];
187  }
188 
189  inline FEATURE& back() { return m_feats.back(); }
190  inline const FEATURE& back() const { return m_feats.back(); }
191  inline FEATURE& front() { return m_feats.front(); }
192  inline const FEATURE& front() const { return m_feats.front(); }
193  /** @} */
194 
195  /** @name getFeature*() methods for template-based access to feature list
196  @{ */
198  size_t i) const
199  {
200  return m_feats[i].pt.x;
201  }
203  size_t i) const
204  {
205  return m_feats[i].pt.y;
206  }
207  inline TFeatureID getFeatureID(size_t i) const { return m_feats[i].ID; }
208  inline float getFeatureResponse(size_t i) const
209  {
210  return m_feats[i].response;
211  }
212  inline bool isPointFeature([[maybe_unused]] size_t i) const { return true; }
213  inline float getScale(size_t i) const
214  {
215  return d2f(1 << m_feats[i].octave);
216  }
218  {
219  return m_feats[i].track_status;
220  }
221 
222  inline void setFeatureX(
223  size_t i, typename TKeyPointTraits<FEATURE>::coord_t x)
224  {
225  m_feats[i].pt.x = x;
226  }
227  inline void setFeatureY(
228  size_t i, typename TKeyPointTraits<FEATURE>::coord_t y)
229  {
230  m_feats[i].pt.y = y;
231  }
232 
233  inline void setFeatureXf(size_t i, float x)
234  {
236  }
237  inline void setFeatureYf(size_t i, float y)
238  {
240  }
241 
242  inline void setFeatureID(size_t i, TFeatureID id) { m_feats[i]->ID = id; }
243  inline void setFeatureResponse(size_t i, float r)
244  {
245  m_feats[i]->response = r;
246  }
247  inline void setScale(size_t i, float s)
248  {
249  m_feats[i]->octave = mrpt::round(std::log(s) / std::log(2));
250  }
251  inline void setTrackStatus(size_t i, TFeatureTrackStatus s)
252  {
253  m_feats[i].track_status = s;
254  }
255 
256  inline void mark_as_outdated() const {}
257  /** @} */
258 
259  private:
260  /** The actual container with the list of features */
262  /** A LUT of the first feature index per row, to efficiently look for
263  * neighbors, etc. */
264  std::vector<size_t> m_first_index_per_row;
266 
267 }; // end of class
268 
269 /** A list of image features using the structure TKeyPoint for each feature
270  * - capable of KD-tree computations */
272 
273 /** A list of image features using the structure TKeyPointf for each
274  * feature - capable of KD-tree computations */
276 
277 /** A helper struct to sort keypoints by their response: It can be used with
278  *these types:
279  * - std::vector<cv::KeyPoint>
280  * - mrpt::vision::TKeyPointList
281  */
282 template <typename FEATURE_LIST>
283 struct KeypointResponseSorter : public std::function<bool(size_t, size_t)>
284 {
285  const FEATURE_LIST& m_data;
286  KeypointResponseSorter(const FEATURE_LIST& data) : m_data(data) {}
287  bool operator()(size_t k1, size_t k2) const
288  {
289  return (m_data[k1].response > m_data[k2].response);
290  }
291 };
292 
293 /** Helper class: KD-tree search class for vector<KeyPoint>:
294  * Call mark_as_outdated() to force rebuilding the kd-tree after modifying the
295  * linked feature list.
296  * \tparam FEAT Can be cv::KeyPoint or mrpt::vision::TKeyPoint
297  */
298 template <typename FEAT>
300  : public mrpt::math::KDTreeCapable<CFeatureListKDTree<FEAT>>
301 {
302  public:
303  inline void mark_as_outdated()
304  {
307  }
308 
309  const std::vector<FEAT>& m_data;
310  CFeatureListKDTree(const std::vector<FEAT>& data) : m_data(data) {}
311  /** @name Methods that MUST be implemented by children classes of
312  KDTreeCapable
313  @{ */
314 
315  /// Must return the number of data points
316  inline size_t kdtree_get_point_count() const { return m_data.size(); }
317  /// Returns the dim'th component of the idx'th point in the class:
318  inline float kdtree_get_pt(const size_t idx, int dim) const
319  {
320  ASSERTDEB_(dim == 0 || dim == 1);
321  if (dim == 0)
322  return m_data[idx].pt.x;
323  else
324  return m_data[idx].pt.y;
325  }
326 
327  /// Returns the distance between the vector "p1[0:size-1]" and the data
328  /// point with index "idx_p2" stored in the class:
329  inline float kdtree_distance(
330  const float* p1, const size_t idx_p2,
331  [[maybe_unused]] size_t size) const
332  {
333  ASSERTDEB_(size == 2);
334 
335  const float d0 = p1[0] - m_data[idx_p2].pt.x;
336  const float d1 = p1[1] - m_data[idx_p2].pt.y;
337  return d0 * d0 + d1 * d1;
338  }
339 
340  // Optional bounding-box computation: return false to default to a standard
341  // bbox computation loop.
342  // Return true if the BBOX was already computed by the class and returned
343  // in "bb" so it can be avoided to redo it again.
344  // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3
345  // for point clouds)
346  template <typename BBOX>
347  bool kdtree_get_bbox([[maybe_unused]] BBOX& bb) const
348  {
349  return false;
350  }
351 
352  /** @} */
353 
354 }; // end CFeatureListKDTree
355 
356 /** @} */ // End of add to module: mrptvision_features
357 
358 } // namespace mrpt::vision
const_reverse_iterator rend() const
Definition: TKeyPoint.h:163
const FEATURE & operator[](const std::size_t index) const
Definition: TKeyPoint.h:184
TKeyPoint_templ()=default
Default constructor, leaves all fields uninitialized.
Helper class: KD-tree search class for vector<KeyPoint>: Call mark_as_outdated() to force rebuilding ...
Definition: TKeyPoint.h:299
uint64_t TFeatureID
Definition of a feature ID.
CFeatureListKDTree(const std::vector< FEAT > &data)
Definition: TKeyPoint.h:310
iterator erase(const iterator &it)
Definition: TKeyPoint.h:164
size_t size(const MATRIXLIKE &m, const int dim)
typename TFeatureVector::reverse_iterator reverse_iterator
Definition: TKeyPoint.h:152
typename TFeatureVector::iterator iterator
Definition: TKeyPoint.h:149
typename TFeatureVector::const_iterator const_iterator
Definition: TKeyPoint.h:150
void setFeatureXf(size_t i, float x)
Definition: TKeyPoint.h:233
TKeyPointTraits< FEATURE >::coord_t getFeatureY(size_t i) const
Definition: TKeyPoint.h:202
float kdtree_distance(const float *p1, const size_t idx_p2, [[maybe_unused]] size_t size) const
Returns the distance between the vector "p1[0:size-1]" and the data point with index "idx_p2" stored ...
Definition: TKeyPoint.h:329
A pair (x,y) of pixel coordinates (subpixel resolution).
Definition: TPixelCoord.h:18
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:
Definition: TKeyPoint.h:318
TFeatureID ID
ID of the feature.
Definition: TKeyPoint.h:39
TKeyPointTraits< FEATURE >::coord_t getFeatureX(size_t i) const
Definition: TKeyPoint.h:197
mrpt::math::CMatrixBool m_occupied_sections
Definition: TKeyPoint.h:265
TFeatureTrackStatus track_status
Status of the feature tracking process.
Definition: TKeyPoint.h:42
Simple structure for image key points.
Definition: TKeyPoint.h:28
bool kdtree_get_bbox([[maybe_unused]] BBOX &bb) const
Definition: TKeyPoint.h:347
void emplace_back(const int x, const int y)
Definition: TKeyPoint.h:175
FEATURE & operator[](const std::size_t index)
Definition: TKeyPoint.h:180
A helper struct to sort keypoints by their response: It can be used with these types: ...
Definition: TKeyPoint.h:283
A generic adaptor class for providing Nearest Neighbor (NN) lookup via the nanoflann library...
Definition: KDTreeCapable.h:83
void setScale(size_t i, float s)
Definition: TKeyPoint.h:247
float d2f(const double d)
shortcut for static_cast<float>(double)
void push_back(const FEATURE &f)
Definition: TKeyPoint.h:174
const FEATURE & front() const
Definition: TKeyPoint.h:192
Inactive (right after detection, and before being tried to track)
typename TFeatureVector::const_reverse_iterator const_reverse_iterator
Definition: TKeyPoint.h:154
TFeatureVector m_feats
The actual container with the list of features.
Definition: TKeyPoint.h:261
std::vector< size_t > m_first_index_per_row
A LUT of the first feature index per row, to efficiently look for neighbors, etc. ...
Definition: TKeyPoint.h:264
bool isPointFeature([[maybe_unused]] size_t i) const
Definition: TKeyPoint.h:212
void setTrackStatus(size_t i, TFeatureTrackStatus s)
Definition: TKeyPoint.h:251
const TFeatureVector & getVector() const
Returns a const ref to the actual std::vector<> container.
Definition: TKeyPoint.h:116
Classes for computer vision, detectors, features, etc.
Definition: CDifodo.h:17
void setFeatureY(size_t i, typename TKeyPointTraits< FEATURE >::coord_t y)
Definition: TKeyPoint.h:227
const_reverse_iterator rbegin() const
Definition: TKeyPoint.h:162
const FEATURE & back() const
Definition: TKeyPoint.h:190
typename mrpt::img::TPixelCoordf ::pixel_coord_t pixel_coord_t
The type of pt.x and pt.y.
Definition: TKeyPoint.h:33
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...
void setFeatureID(size_t i, TFeatureID id)
Definition: TKeyPoint.h:242
float getFeatureResponse(size_t i) const
Definition: TKeyPoint.h:208
std::vector< size_t > & getFirstIndexPerRowLUT()
Definition: TKeyPoint.h:140
TFeatureID getMaxID() const
Returns the maximum ID of all features in the list, or 0 if it&#39;s empty.
Definition: TKeyPoint.h:119
void setFeatureX(size_t i, typename TKeyPointTraits< FEATURE >::coord_t x)
Definition: TKeyPoint.h:222
A list of image features using the structure TKeyPoint for each feature Users normally will use direc...
Definition: TKeyPoint.h:106
const_iterator begin() const
Definition: TKeyPoint.h:158
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
Definition: exceptions.h:190
void kdtree_mark_as_outdated() const
To be called by child classes when KD tree data changes.
TKeyPoint_templ(const COORD_TYPE x, const COORD_TYPE y)
Constructor that only sets the pt.
Definition: TKeyPoint.h:57
const std::vector< FEAT > & m_data
Definition: TKeyPoint.h:309
uint8_t octave
The image octave the image was found in: 0=original image, 1=1/2 image, 2=1/4 image, etc.
Definition: TKeyPoint.h:49
void setFeatureYf(size_t i, float y)
Definition: TKeyPoint.h:237
TKeyPoint_templ(const OTHER_TKeyPoint &o)
Definition: TKeyPoint.h:64
TFeatureID getFeatureID(size_t i) const
Definition: TKeyPoint.h:207
TFeatureTrackStatus getTrackStatus(size_t i)
Definition: TKeyPoint.h:217
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...
Definition: TKeyPoint.h:135
float getScale(size_t i) const
Definition: TKeyPoint.h:213
This template class provides the basic functionality for a general 2D any-size, resizable container o...
float response
A measure of the "goodness" of the feature (typically, the KLT_response value)
Definition: TKeyPoint.h:46
size_t kdtree_get_point_count() const
Must return the number of data points.
Definition: TKeyPoint.h:316
uint8_t user_flags
A field for any other flags needed by the user (this has not a predefined meaning) ...
Definition: TKeyPoint.h:52
pixel_coords_t pt
Coordinates in the image.
Definition: TKeyPoint.h:36
bool operator()(size_t k1, size_t k2) const
Definition: TKeyPoint.h:287
KeypointResponseSorter(const FEATURE_LIST &data)
Definition: TKeyPoint.h:286
void setFeatureResponse(size_t i, float r)
Definition: TKeyPoint.h:243
static struct FontData data
Definition: gltext.cpp:144
const_iterator end() const
Definition: TKeyPoint.h:159
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:24



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020