MRPT  1.9.9
CFeatureExtraction_FASTER.cpp
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 
10 #include "vision-precomp.h" // Precompiled headers
11 
13 
14 // Universal include for all versions of OpenCV
15 #include <mrpt/otherlibs/do_opencv_includes.h>
16 
17 #if MRPT_HAS_OPENCV
19 #endif
20 
21 using namespace mrpt;
22 using namespace mrpt::vision;
23 using namespace mrpt::img;
24 using namespace mrpt::img;
25 using namespace mrpt::system;
26 using namespace std;
27 
28 // ------------ SSE2-optimized implementations of FASTER -------------
30  const CImage& img, TSimpleFeatureList& corners, const int threshold,
31  bool append_to_list, uint8_t octave,
32  std::vector<size_t>* out_feats_index_by_row)
33 {
34 #if MRPT_HAS_OPENCV
35  const IplImage* IPL = img.getAs<IplImage>();
36  ASSERTDEB_(IPL && IPL->nChannels == 1);
37  if (!append_to_list) corners.clear();
38 
40  IPL, corners, threshold, octave, out_feats_index_by_row);
41 #else
42  THROW_EXCEPTION("MRPT built without OpenCV support!");
43 #endif
44 }
46  const CImage& img, TSimpleFeatureList& corners, const int threshold,
47  bool append_to_list, uint8_t octave,
48  std::vector<size_t>* out_feats_index_by_row)
49 {
50 #if MRPT_HAS_OPENCV
51  const IplImage* IPL = img.getAs<IplImage>();
52  ASSERTDEB_(IPL && IPL->nChannels == 1);
53  if (!append_to_list) corners.clear();
54 
56  IPL, corners, threshold, octave, out_feats_index_by_row);
57 #else
58  THROW_EXCEPTION("MRPT built without OpenCV support!");
59 #endif
60 }
62  const CImage& img, TSimpleFeatureList& corners, const int threshold,
63  bool append_to_list, uint8_t octave,
64  std::vector<size_t>* out_feats_index_by_row)
65 {
66 #if MRPT_HAS_OPENCV
67  const IplImage* IPL = img.getAs<IplImage>();
68  ASSERTDEB_(IPL && IPL->nChannels == 1);
69  if (!append_to_list) corners.clear();
70 
72  IPL, corners, threshold, octave, out_feats_index_by_row);
73 #else
74  THROW_EXCEPTION("MRPT built without OpenCV support!");
75 #endif
76 }
77 
78 /************************************************************************************************
79  * extractFeaturesFASTER
80  **
81  ************************************************************************************************/
82 // N_fast = 9, 10, 12
84  const int N_fast, const mrpt::img::CImage& inImg, CFeatureList& feats,
85  unsigned int init_ID, unsigned int nDesiredFeatures,
86  const TImageROI& ROI) const
87 {
88  MRPT_UNUSED_PARAM(ROI);
90 
91 #if MRPT_HAS_OPENCV
92  // Make sure we operate on a gray-scale version of the image:
93  const CImage inImg_gray(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
94 
95  const IplImage* IPL = inImg_gray.getAs<IplImage>();
96 
97  TSimpleFeatureList corners;
98  TFeatureType type_of_this_feature;
99 
100  switch (N_fast)
101  {
102  case 9:
104  IPL, corners, options.FASTOptions.threshold, 0, nullptr);
105  type_of_this_feature = featFASTER9;
106  break;
107  case 10:
109  IPL, corners, options.FASTOptions.threshold, 0, nullptr);
110  type_of_this_feature = featFASTER10;
111  break;
112  case 12:
114  IPL, corners, options.FASTOptions.threshold, 0, nullptr);
115  type_of_this_feature = featFASTER12;
116  break;
117  default:
119  "Only the 9,10,12 FASTER detectors are implemented.")
120  break;
121  };
122 
123  // *All* the features have been extracted.
124  const size_t N = corners.size();
125 
126  // Now:
127  // 1) Sort them by "response": It's ~100 times faster to sort a list of
128  // indices "sorted_indices" than sorting directly the actual list of
129  // features "corners"
130  std::vector<size_t> sorted_indices(N);
131  for (size_t i = 0; i < N; i++) sorted_indices[i] = i;
132 
133  // Use KLT response
134  if (options.FASTOptions.use_KLT_response ||
135  nDesiredFeatures != 0 // If the user wants us to limit the number of
136  // features, we need to do it according to some
137  // quality measure
138  )
139  {
140  const int KLT_half_win = 4;
141  const int max_x = inImg_gray.getWidth() - 1 - KLT_half_win;
142  const int max_y = inImg_gray.getHeight() - 1 - KLT_half_win;
143 
144  for (size_t i = 0; i < N; i++)
145  {
146  const int x = corners[i].pt.x;
147  const int y = corners[i].pt.y;
148  if (x > KLT_half_win && y > KLT_half_win && x <= max_x &&
149  y <= max_y)
150  corners[i].response =
151  inImg_gray.KLT_response(x, y, KLT_half_win);
152  else
153  corners[i].response = -100;
154  }
155 
156  std::sort(
157  sorted_indices.begin(), sorted_indices.end(),
159  }
160  else
161  {
162  for (size_t i = 0; i < N; i++) corners[i].response = 0;
163  }
164 
165  // 2) Filter by "min-distance" (in options.FASTOptions.min_distance)
166  // 3) Convert to MRPT CFeatureList format.
167  // Steps 2 & 3 are done together in the while() below.
168  // The "min-distance" filter is done by means of a 2D binary matrix where
169  // each cell is marked when one
170  // feature falls within it. This is not exactly the same than a pure
171  // "min-distance" but is pretty close
172  // and for large numbers of features is much faster than brute force search
173  // of kd-trees.
174  // (An intermediate approach would be the creation of a mask image updated
175  // for each accepted feature, etc.)
176 
177  const bool do_filter_min_dist = options.FASTOptions.min_distance > 1;
178 
179  // Used half the min-distance since we'll later mark as occupied the ranges
180  // [i-1,i+1] for a feature at "i"
181  const unsigned int occupied_grid_cell_size =
182  options.FASTOptions.min_distance / 2.0;
183  const float occupied_grid_cell_size_inv = 1.0f / occupied_grid_cell_size;
184 
185  unsigned int grid_lx =
186  !do_filter_min_dist
187  ? 1
188  : (unsigned int)(1 + inImg.getWidth() * occupied_grid_cell_size_inv);
189  unsigned int grid_ly =
190  !do_filter_min_dist
191  ? 1
192  : (unsigned int)(1 + inImg.getHeight() * occupied_grid_cell_size_inv);
193 
194  mrpt::math::CMatrixBool occupied_sections(
195  grid_lx, grid_ly); // See the comments above for an explanation.
196  occupied_sections.fillAll(false);
197 
198  unsigned int nMax =
199  (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
200  const int offset = (int)this->options.patchSize / 2 + 1;
201  const int size_2 = options.patchSize / 2;
202  const size_t imgH = inImg.getHeight();
203  const size_t imgW = inImg.getWidth();
204  unsigned int i = 0;
205  unsigned int cont = 0;
206  TFeatureID nextID = init_ID;
207 
208  if (!options.addNewFeatures) feats.clear();
209 
210  while (cont != nMax && i != N)
211  {
212  // Take the next feature fromt the ordered list of good features:
213  const TSimpleFeature& feat = corners[sorted_indices[i]];
214  i++;
215 
216  // Patch out of the image??
217  const int xBorderInf = feat.pt.x - size_2;
218  const int xBorderSup = feat.pt.x + size_2;
219  const int yBorderInf = feat.pt.y - size_2;
220  const int yBorderSup = feat.pt.y + size_2;
221 
222  if (!(xBorderSup < (int)imgW && xBorderInf > 0 &&
223  yBorderSup < (int)imgH && yBorderInf > 0))
224  continue; // nope, skip.
225 
226  if (do_filter_min_dist)
227  {
228  // Check the min-distance:
229  const size_t section_idx_x =
230  size_t(feat.pt.x * occupied_grid_cell_size_inv);
231  const size_t section_idx_y =
232  size_t(feat.pt.y * occupied_grid_cell_size_inv);
233 
234  if (occupied_sections(section_idx_x, section_idx_y))
235  continue; // Already occupied! skip.
236 
237  // Mark section as occupied
238  occupied_sections.set_unsafe(section_idx_x, section_idx_y, true);
239  if (section_idx_x > 0)
240  occupied_sections.set_unsafe(
241  section_idx_x - 1, section_idx_y, true);
242  if (section_idx_y > 0)
243  occupied_sections.set_unsafe(
244  section_idx_x, section_idx_y - 1, true);
245  if (section_idx_x < grid_lx - 1)
246  occupied_sections.set_unsafe(
247  section_idx_x + 1, section_idx_y, true);
248  if (section_idx_y < grid_ly - 1)
249  occupied_sections.set_unsafe(
250  section_idx_x, section_idx_y + 1, true);
251  }
252 
253  // All tests passed: add new feature:
254  CFeature::Ptr ft = mrpt::make_aligned_shared<CFeature>();
255  ft->type = type_of_this_feature;
256  ft->ID = nextID++;
257  ft->x = feat.pt.x;
258  ft->y = feat.pt.y;
259  ft->response = feat.response;
260  ft->orientation = 0;
261  ft->scale = 1;
262  ft->patchSize = options.patchSize; // The size of the feature patch
263 
264  if (options.patchSize > 0)
265  {
266  inImg.extract_patch(
267  ft->patch, round(ft->x) - offset, round(ft->y) - offset,
268  options.patchSize,
269  options.patchSize); // Image patch surronding the feature
270  }
271  feats.push_back(ft);
272  ++cont;
273  }
274 
275 #endif
276  MRPT_END
277 }
#define MRPT_START
Definition: exceptions.h:262
uint64_t TFeatureID
Definition of a feature ID.
Declares a matrix of booleans (non serializable).
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
FASTER-9 detector, Edward Rosten&#39;s libcvd implementation optimized for SSE2.
static void detectFeatures_SSE2_FASTER10(const mrpt::img::CImage &img, TSimpleFeatureList &corners, const int threshold=20, bool append_to_list=false, uint8_t octave=0, std::vector< size_t > *out_feats_index_by_row=nullptr)
Just like detectFeatures_SSE2_FASTER9() for another version of the detector.
GLintptr offset
Definition: glext.h:3925
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:892
STL namespace.
void extractFeaturesFASTER_N(const int N, const mrpt::img::CImage &img, CFeatureList &feats, unsigned int init_ID=0, unsigned int nDesiredFeatures=0, const TImageROI &ROI=TImageROI()) const
Edward&#39;s "FASTER & Better" detector, N=9,10,12.
void fast_corner_detect_12(const IplImage *I, TSimpleFeatureList &corners, int barrier, uint8_t octave, std::vector< size_t > *out_feats_index_by_row)
unsigned char uint8_t
Definition: rptypes.h:41
A helper struct to sort keypoints by their response: It can be used with these types: ...
A structure for defining a ROI within an image.
size_t getWidth() const override
Returns the width of the image in pixels.
Definition: CImage.cpp:864
GLint GLvoid * img
Definition: glext.h:3763
void fast_corner_detect_9(const IplImage *I, TSimpleFeatureList &corners, int barrier, uint8_t octave, std::vector< size_t > *out_feats_index_by_row)
FASTER-9 detector, Edward Rosten&#39;s libcvd implementation optimized for SSE2.
static void detectFeatures_SSE2_FASTER9(const mrpt::img::CImage &img, TSimpleFeatureList &corners, const int threshold=20, bool append_to_list=false, uint8_t octave=0, std::vector< size_t > *out_feats_index_by_row=nullptr)
A SSE2-optimized implementation of FASTER-9 (requires img to be grayscale).
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:18
FASTER-9 detector, Edward Rosten&#39;s libcvd implementation optimized for SSE2.
static void detectFeatures_SSE2_FASTER12(const mrpt::img::CImage &img, TSimpleFeatureList &corners, const int threshold=20, bool append_to_list=false, uint8_t octave=0, std::vector< size_t > *out_feats_index_by_row=nullptr)
Just like detectFeatures_SSE2_FASTER9() for another version of the detector.
void set_unsafe(size_t row, size_t col, const T &v)
Fast but unsafe method to write a value in the matrix.
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:304
TFeatureType
Types of features - This means that the point has been detected with this algorithm, which is independent of additional descriptors a feature may also have.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
pixel_coords_t pt
Coordinates in the image.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
Definition: exceptions.h:205
float response
A measure of the "goodness" of the feature (typically, the KLT_response value)
#define MRPT_END
Definition: exceptions.h:266
A simple structure for representing one image feature (without descriptor nor patch) - This is the te...
void extract_patch(CImage &patch, const unsigned int col=0, const unsigned int row=0, const unsigned int width=1, const unsigned int height=1) const
Extract a patch from this image, saveing it into "patch" (its previous contents will be overwritten)...
Definition: CImage.cpp:1359
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
void fast_corner_detect_10(const IplImage *I, TSimpleFeatureList &corners, int barrier, uint8_t octave, std::vector< size_t > *out_feats_index_by_row)
void push_back(const CFeature::Ptr &f)
Definition: CFeature.h:398
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
void fillAll(const T &val)
#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