MRPT  1.9.9
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(size_t i) const
213  {
215  return true;
216  }
217  inline float getScale(size_t i) const
218  {
219  return d2f(1 << m_feats[i].octave);
220  }
222  {
223  return m_feats[i].track_status;
224  }
225 
226  inline void setFeatureX(
227  size_t i, typename TKeyPointTraits<FEATURE>::coord_t x)
228  {
229  m_feats[i].pt.x = x;
230  }
231  inline void setFeatureY(
232  size_t i, typename TKeyPointTraits<FEATURE>::coord_t y)
233  {
234  m_feats[i].pt.y = y;
235  }
236 
237  inline void setFeatureXf(size_t i, float x)
238  {
240  }
241  inline void setFeatureYf(size_t i, float y)
242  {
244  }
245 
246  inline void setFeatureID(size_t i, TFeatureID id) { m_feats[i]->ID = id; }
247  inline void setFeatureResponse(size_t i, float r)
248  {
249  m_feats[i]->response = r;
250  }
251  inline void setScale(size_t i, float s)
252  {
253  m_feats[i]->octave = mrpt::round(std::log(s) / std::log(2));
254  }
255  inline void setTrackStatus(size_t i, TFeatureTrackStatus s)
256  {
257  m_feats[i].track_status = s;
258  }
259 
260  inline void mark_as_outdated() const {}
261  /** @} */
262 
263  private:
264  /** The actual container with the list of features */
266  /** A LUT of the first feature index per row, to efficiently look for
267  * neighbors, etc. */
268  std::vector<size_t> m_first_index_per_row;
270 
271 }; // end of class
272 
273 /** A list of image features using the structure TKeyPoint for each feature
274  * - capable of KD-tree computations */
276 
277 /** A list of image features using the structure TKeyPointf for each
278  * feature - capable of KD-tree computations */
280 
281 /** A helper struct to sort keypoints by their response: It can be used with
282  *these types:
283  * - std::vector<cv::KeyPoint>
284  * - mrpt::vision::TKeyPointList
285  */
286 template <typename FEATURE_LIST>
287 struct KeypointResponseSorter : public std::function<bool(size_t, size_t)>
288 {
289  const FEATURE_LIST& m_data;
290  KeypointResponseSorter(const FEATURE_LIST& data) : m_data(data) {}
291  bool operator()(size_t k1, size_t k2) const
292  {
293  return (m_data[k1].response > m_data[k2].response);
294  }
295 };
296 
297 /** Helper class: KD-tree search class for vector<KeyPoint>:
298  * Call mark_as_outdated() to force rebuilding the kd-tree after modifying the
299  * linked feature list.
300  * \tparam FEAT Can be cv::KeyPoint or mrpt::vision::TKeyPoint
301  */
302 template <typename FEAT>
304  : public mrpt::math::KDTreeCapable<CFeatureListKDTree<FEAT>>
305 {
306  public:
307  inline void mark_as_outdated()
308  {
311  }
312 
313  const std::vector<FEAT>& m_data;
314  CFeatureListKDTree(const std::vector<FEAT>& data) : m_data(data) {}
315  /** @name Methods that MUST be implemented by children classes of
316  KDTreeCapable
317  @{ */
318 
319  /// Must return the number of data points
320  inline size_t kdtree_get_point_count() const { return m_data.size(); }
321  /// Returns the dim'th component of the idx'th point in the class:
322  inline float kdtree_get_pt(const size_t idx, int dim) const
323  {
324  ASSERTDEB_(dim == 0 || dim == 1);
325  if (dim == 0)
326  return m_data[idx].pt.x;
327  else
328  return m_data[idx].pt.y;
329  }
330 
331  /// Returns the distance between the vector "p1[0:size-1]" and the data
332  /// point with index "idx_p2" stored in the class:
333  inline float kdtree_distance(
334  const float* p1, const size_t idx_p2, size_t size) const
335  {
336  MRPT_UNUSED_PARAM(size); // in release mode
337  ASSERTDEB_(size == 2);
338 
339  const float d0 = p1[0] - m_data[idx_p2].pt.x;
340  const float d1 = p1[1] - m_data[idx_p2].pt.y;
341  return d0 * d0 + d1 * d1;
342  }
343 
344  // Optional bounding-box computation: return false to default to a standard
345  // bbox computation loop.
346  // Return true if the BBOX was already computed by the class and returned
347  // in "bb" so it can be avoided to redo it again.
348  // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3
349  // for point clouds)
350  template <typename BBOX>
351  bool kdtree_get_bbox(BBOX& bb) const
352  {
353  MRPT_UNUSED_PARAM(bb);
354  return false;
355  }
356 
357  /** @} */
358 
359 }; // end CFeatureListKDTree
360 
361 /** @} */ // End of add to module: mrptvision_features
362 
363 } // 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:303
uint64_t TFeatureID
Definition of a feature ID.
CFeatureListKDTree(const std::vector< FEAT > &data)
Definition: TKeyPoint.h:314
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:237
TKeyPointTraits< FEATURE >::coord_t getFeatureY(size_t i) const
Definition: TKeyPoint.h:202
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:322
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:269
TFeatureTrackStatus track_status
Status of the feature tracking process.
Definition: TKeyPoint.h:42
Simple structure for image key points.
Definition: TKeyPoint.h:28
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:287
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:251
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:265
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:268
void setTrackStatus(size_t i, TFeatureTrackStatus s)
Definition: TKeyPoint.h:255
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:231
const_reverse_iterator rbegin() const
Definition: TKeyPoint.h:162
bool isPointFeature(size_t i) const
Definition: TKeyPoint.h:212
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:246
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:226
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.
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 ...
Definition: TKeyPoint.h:333
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:313
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:241
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:221
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:217
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:320
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:291
bool kdtree_get_bbox(BBOX &bb) const
Definition: TKeyPoint.h:351
KeypointResponseSorter(const FEATURE_LIST &data)
Definition: TKeyPoint.h:290
void setFeatureResponse(size_t i, float r)
Definition: TKeyPoint.h:247
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
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 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020