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-2019, 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. */
50  /** A field for any other flags needed by the user (this has not a
51  * predefined meaning) */
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 push_back_fast(const FEATURE& f) { m_feats.push_back(f); }
176  inline void emplace_back(const int x, const int y)
177  {
178  m_feats.emplace_back(x, y);
179  }
180 
181  inline FEATURE& operator[](const std::size_t index)
182  {
183  return m_feats[index];
184  }
185  inline const FEATURE& operator[](const std::size_t index) const
186  {
187  return m_feats[index];
188  }
189 
190  inline FEATURE& back() { return m_feats.back(); }
191  inline const FEATURE& back() const { return m_feats.back(); }
192  inline FEATURE& front() { return m_feats.front(); }
193  inline const FEATURE& front() const { return m_feats.front(); }
194  /** @} */
195 
196  /** @name getFeature*() methods for template-based access to feature list
197  @{ */
199  size_t i) const
200  {
201  return m_feats[i].pt.x;
202  }
204  size_t i) const
205  {
206  return m_feats[i].pt.y;
207  }
208  inline TFeatureID getFeatureID(size_t i) const { return m_feats[i].ID; }
209  inline float getFeatureResponse(size_t i) const
210  {
211  return m_feats[i].response;
212  }
213  inline bool isPointFeature(size_t i) const
214  {
216  return true;
217  }
218  inline float getScale(size_t i) const
219  {
220  return static_cast<float>(1 << m_feats[i].octave);
221  }
223  {
224  return m_feats[i].track_status;
225  }
226 
227  inline void setFeatureX(
228  size_t i, typename TKeyPointTraits<FEATURE>::coord_t x)
229  {
230  m_feats[i].pt.x = x;
231  }
232  inline void setFeatureY(
233  size_t i, typename TKeyPointTraits<FEATURE>::coord_t y)
234  {
235  m_feats[i].pt.y = y;
236  }
237 
238  inline void setFeatureXf(size_t i, float x)
239  {
241  }
242  inline void setFeatureYf(size_t i, float y)
243  {
245  }
246 
247  inline void setFeatureID(size_t i, TFeatureID id) { m_feats[i]->ID = id; }
248  inline void setFeatureResponse(size_t i, float r)
249  {
250  m_feats[i]->response = r;
251  }
252  inline void setScale(size_t i, float s)
253  {
254  m_feats[i]->octave = mrpt::round(std::log(s) / std::log(2));
255  }
256  inline void setTrackStatus(size_t i, TFeatureTrackStatus s)
257  {
258  m_feats[i].track_status = s;
259  }
260 
261  inline void mark_as_outdated() const {}
262  /** @} */
263 
264  private:
265  /** The actual container with the list of features */
267  /** A LUT of the first feature index per row, to efficiently look for
268  * neighbors, etc. */
269  std::vector<size_t> m_first_index_per_row;
271 
272 }; // end of class
273 
274 /** A list of image features using the structure TKeyPoint for each feature
275  * - capable of KD-tree computations */
277 
278 /** A list of image features using the structure TKeyPointf for each
279  * feature - capable of KD-tree computations */
281 
282 /** A helper struct to sort keypoints by their response: It can be used with
283  *these types:
284  * - std::vector<cv::KeyPoint>
285  * - mrpt::vision::TKeyPointList
286  */
287 template <typename FEATURE_LIST>
288 struct KeypointResponseSorter : public std::function<bool(size_t, size_t)>
289 {
290  const FEATURE_LIST& m_data;
291  KeypointResponseSorter(const FEATURE_LIST& data) : m_data(data) {}
292  bool operator()(size_t k1, size_t k2) const
293  {
294  return (m_data[k1].response > m_data[k2].response);
295  }
296 };
297 
298 /** Helper class: KD-tree search class for vector<KeyPoint>:
299  * Call mark_as_outdated() to force rebuilding the kd-tree after modifying the
300  * linked feature list.
301  * \tparam FEAT Can be cv::KeyPoint or mrpt::vision::TKeyPoint
302  */
303 template <typename FEAT>
305  : public mrpt::math::KDTreeCapable<CFeatureListKDTree<FEAT>>
306 {
307  public:
308  inline void mark_as_outdated()
309  {
312  }
313 
314  const std::vector<FEAT>& m_data;
315  CFeatureListKDTree(const std::vector<FEAT>& data) : m_data(data) {}
316  /** @name Methods that MUST be implemented by children classes of
317  KDTreeCapable
318  @{ */
319 
320  /// Must return the number of data points
321  inline size_t kdtree_get_point_count() const { return m_data.size(); }
322  /// Returns the dim'th component of the idx'th point in the class:
323  inline float kdtree_get_pt(const size_t idx, int dim) const
324  {
325  ASSERTDEB_(dim == 0 || dim == 1);
326  if (dim == 0)
327  return m_data[idx].pt.x;
328  else
329  return m_data[idx].pt.y;
330  }
331 
332  /// Returns the distance between the vector "p1[0:size-1]" and the data
333  /// point with index "idx_p2" stored in the class:
334  inline float kdtree_distance(
335  const float* p1, const size_t idx_p2, size_t size) const
336  {
337  MRPT_UNUSED_PARAM(size); // in release mode
338  ASSERTDEB_(size == 2);
339 
340  const float d0 = p1[0] - m_data[idx_p2].pt.x;
341  const float d1 = p1[1] - m_data[idx_p2].pt.y;
342  return d0 * d0 + d1 * d1;
343  }
344 
345  // Optional bounding-box computation: return false to default to a standard
346  // bbox computation loop.
347  // Return true if the BBOX was already computed by the class and returned
348  // in "bb" so it can be avoided to redo it again.
349  // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3
350  // for point clouds)
351  template <typename BBOX>
352  bool kdtree_get_bbox(BBOX& bb) const
353  {
354  MRPT_UNUSED_PARAM(bb);
355  return false;
356  }
357 
358  /** @} */
359 
360 }; // end CFeatureListKDTree
361 
362 /** @} */ // End of add to module: mrptvision_features
363 
364 } // namespace mrpt::vision
const_reverse_iterator rend() const
Definition: TKeyPoint.h:163
const FEATURE & operator[](const std::size_t index) const
Definition: TKeyPoint.h:185
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:304
uint64_t TFeatureID
Definition of a feature ID.
CFeatureListKDTree(const std::vector< FEAT > &data)
Definition: TKeyPoint.h:315
iterator erase(const iterator &it)
Definition: TKeyPoint.h:164
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:238
TKeyPointTraits< FEATURE >::coord_t getFeatureY(size_t i) const
Definition: TKeyPoint.h:203
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:323
TFeatureID ID
ID of the feature.
Definition: TKeyPoint.h:39
TKeyPointTraits< FEATURE >::coord_t getFeatureX(size_t i) const
Definition: TKeyPoint.h:198
mrpt::math::CMatrixBool m_occupied_sections
Definition: TKeyPoint.h:270
TFeatureTrackStatus track_status
Status of the feature tracking process.
Definition: TKeyPoint.h:42
Simple structure for image key points.
Definition: TKeyPoint.h:28
GLdouble s
Definition: glext.h:3682
void emplace_back(const int x, const int y)
Definition: TKeyPoint.h:176
unsigned char uint8_t
Definition: rptypes.h:44
FEATURE & operator[](const std::size_t index)
Definition: TKeyPoint.h:181
A helper struct to sort keypoints by their response: It can be used with these types: ...
Definition: TKeyPoint.h:288
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:252
void push_back(const FEATURE &f)
Definition: TKeyPoint.h:174
const FEATURE & front() const
Definition: TKeyPoint.h:193
Inactive (right after detection, and before being tried to track)
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...
typename TFeatureVector::const_reverse_iterator const_reverse_iterator
Definition: TKeyPoint.h:154
GLuint index
Definition: glext.h:4068
TFeatureVector m_feats
The actual container with the list of features.
Definition: TKeyPoint.h:266
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:269
void push_back_fast(const FEATURE &f)
Definition: TKeyPoint.h:175
void setTrackStatus(size_t i, TFeatureTrackStatus s)
Definition: TKeyPoint.h:256
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:232
const_reverse_iterator rbegin() const
Definition: TKeyPoint.h:162
bool isPointFeature(size_t i) const
Definition: TKeyPoint.h:213
const FEATURE & back() const
Definition: TKeyPoint.h:191
typename mrpt::img::TPixelCoordf ::pixel_coord_t pixel_coord_t
The type of pt.x and pt.y.
Definition: TKeyPoint.h:33
void setFeatureID(size_t i, TFeatureID id)
Definition: TKeyPoint.h:247
float getFeatureResponse(size_t i) const
Definition: TKeyPoint.h:209
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
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
void setFeatureX(size_t i, typename TKeyPointTraits< FEATURE >::coord_t x)
Definition: TKeyPoint.h:227
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.
GLuint id
Definition: glext.h:3920
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:334
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:314
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:242
TKeyPoint_templ(const OTHER_TKeyPoint &o)
Definition: TKeyPoint.h:64
GLenum GLint GLint y
Definition: glext.h:3542
TFeatureID getFeatureID(size_t i) const
Definition: TKeyPoint.h:208
GLsizeiptr size
Definition: glext.h:3934
GLenum GLint x
Definition: glext.h:3542
TFeatureTrackStatus getTrackStatus(size_t i)
Definition: TKeyPoint.h:222
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:218
This template class provides the basic functionality for a general 2D any-size, resizable container o...
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3550
float response
A measure of the "goodness" of the feature (typically, the KLT_response value)
Definition: TKeyPoint.h:46
mrpt::img::TPixelCoordf pixel_coords_t
The type of pt.
Definition: TKeyPoint.h:31
size_t kdtree_get_point_count() const
Must return the number of data points.
Definition: TKeyPoint.h:321
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:292
bool kdtree_get_bbox(BBOX &bb) const
Definition: TKeyPoint.h:352
KeypointResponseSorter(const FEATURE_LIST &data)
Definition: TKeyPoint.h:291
void setFeatureResponse(size_t i, float r)
Definition: TKeyPoint.h:248
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
const_iterator end() const
Definition: TKeyPoint.h:159
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: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019