MRPT  1.9.9
CFeatureExtraction_ORB.cpp
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 
10 #include "vision-precomp.h" // Precompiled headers
11 
13 
14 // Universal include for all versions of OpenCV
15 #include <mrpt/3rdparty/do_opencv_includes.h>
16 
17 using namespace mrpt;
18 using namespace mrpt::vision;
19 using namespace mrpt::img;
20 using namespace mrpt::system;
21 using namespace mrpt::img;
22 using namespace std;
23 
25  const mrpt::img::CImage& inImg, CFeatureList& feats,
26  const unsigned int init_ID, const unsigned int nDesiredFeatures,
27  [[maybe_unused]] const TImageROI& ROI)
28 {
30 
31  mrpt::system::CTimeLoggerEntry tle(profiler, "extractFeaturesORB");
32 
33 #if MRPT_HAS_OPENCV
34  using namespace cv;
35 
36  vector<KeyPoint> cv_feats; // OpenCV keypoint output vector
37  Mat cv_descs; // OpenCV descriptor output
38 
39  const bool use_precomputed_feats = feats.size() > 0;
40 
41  if (use_precomputed_feats)
42  {
43  cv_feats.resize(feats.size());
44  for (size_t k = 0; k < cv_feats.size(); ++k)
45  {
46  cv_feats[k].pt.x = feats[k].keypoint.pt.x;
47  cv_feats[k].pt.y = feats[k].keypoint.pt.y;
48  }
49  }
50 
51  // Make sure we operate on a gray-scale version of the image:
52  const CImage inImg_gray(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
53  const Mat& cvImg = inImg_gray.asCvMatRef();
54 
55  // The detector and descriptor
56  profiler.enter("extractFeaturesORB.openCV_detectAndCompute");
57 
58 #if MRPT_OPENCV_VERSION_NUM < 0x300
59  Ptr<Feature2D> orb = Algorithm::create<Feature2D>("Feature2D.ORB");
60  orb->operator()(cvImg, Mat(), cv_feats, cv_descs, use_precomputed_feats);
61 #else
62  const size_t n_feats_2_extract =
63  nDesiredFeatures == 0 ? 1000 : 3 * nDesiredFeatures;
64  Ptr<cv::ORB> orb = cv::ORB::create(
65  n_feats_2_extract, options.ORBOptions.scale_factor,
66  options.ORBOptions.n_levels);
67  orb->detectAndCompute(
68  cvImg, Mat(), cv_feats, cv_descs, use_precomputed_feats);
69 #endif
70  profiler.leave("extractFeaturesORB.openCV_detectAndCompute");
71 
72  CTimeLoggerEntry tle2(profiler, "extractFeaturesORB.fillFeatsStruct");
73 
74  const size_t n_feats = cv_feats.size();
75 
76  // if we had input features, just convert cv_feats to CFeatures and return
77  const unsigned int patch_size_2 = options.patchSize / 2;
78  unsigned int f_id = init_ID;
79  if (use_precomputed_feats)
80  {
81  for (size_t k = 0; k < n_feats; ++k)
82  {
83  feats[k].descriptors.ORB->resize(cv_descs.cols);
84  for (int m = 0; m < cv_descs.cols; ++m)
85  (*feats[k].descriptors.ORB)[m] = cv_descs.at<uchar>(k, m);
86 
87  /*
88  feats[k].response = cv_feats[k].response;
89  feats[k].scale = cv_feats[k].size;
90  feats[k].angle = cv_feats[k].orientation;
91  feats[k].ID = f_id++;
92  */
93  feats[k].type = featORB;
94 
95  if (options.ORBOptions.extract_patch && options.patchSize > 0)
96  {
97  inImg.extract_patch(
98  *feats[k].patch,
99  round(feats[k].keypoint.pt.x) - patch_size_2,
100  round(feats[k].keypoint.pt.y) - patch_size_2,
101  options.patchSize, options.patchSize);
102  }
103  }
104  return;
105  }
106 
107  // 1) Sort the fearues by "response": It's ~100 times faster to sort a list
108  // of
109  // indices "sorted_indices" than sorting directly the actual list of
110  // features "cv_feats"
111  std::vector<size_t> sorted_indices(n_feats);
112  for (size_t i = 0; i < n_feats; i++) sorted_indices[i] = i;
113  std::sort(
114  sorted_indices.begin(), sorted_indices.end(),
116 
117  // 2) Filter by "min-distance" (in options.ORBOptions.min_distance)
118  // 3) Convert to MRPT CFeatureList format.
119  // Steps 2 & 3 are done together in the while() below.
120  // The "min-distance" filter is done by means of a 2D binary matrix where
121  // each cell is marked when one
122  // feature falls within it. This is not exactly the same than a pure
123  // "min-distance" but is pretty close
124  // and for large numbers of features is much faster than brute force search
125  // of kd-trees.
126  // (An intermediate approach would be the creation of a mask image updated
127  // for each accepted feature, etc.)
128 
129  const bool do_filter_min_dist = options.ORBOptions.min_distance > 1;
130  const unsigned int occupied_grid_cell_size =
131  options.ORBOptions.min_distance / 2;
132  const float occupied_grid_cell_size_inv = 1.0f / occupied_grid_cell_size;
133 
134  unsigned int grid_lx =
135  !do_filter_min_dist
136  ? 1
137  : (unsigned int)(1 + inImg.getWidth() * occupied_grid_cell_size_inv);
138  unsigned int grid_ly =
139  !do_filter_min_dist
140  ? 1
141  : (unsigned int)(1 + inImg.getHeight() * occupied_grid_cell_size_inv);
142 
143  mrpt::math::CMatrixBool occupied_sections(
144  grid_lx, grid_ly); // See the comments above for an explanation.
145  occupied_sections.fill(false);
146 
147  const size_t n_max_feats = nDesiredFeatures > 0
148  ? std::min(size_t(nDesiredFeatures), n_feats)
149  : n_feats;
150 
151  if (!options.addNewFeatures) feats.clear();
152  // feats.reserve( feats.size() + n_max_feats );
153 
154  const size_t imgH = inImg.getHeight();
155  const size_t imgW = inImg.getWidth();
156  size_t k = 0;
157  size_t c_feats = 0;
158  while (c_feats < n_max_feats && k < n_feats)
159  {
160  const size_t idx = sorted_indices[k++];
161  const KeyPoint& kp = cv_feats[idx];
162  if (options.ORBOptions.extract_patch && options.patchSize > 0)
163  {
164  // check image boundaries for extracting the patch
165  const int xBorderInf = (int)floor(kp.pt.x - patch_size_2);
166  const int xBorderSup = (int)floor(kp.pt.x + patch_size_2);
167  const int yBorderInf = (int)floor(kp.pt.y - patch_size_2);
168  const int yBorderSup = (int)floor(kp.pt.y + patch_size_2);
169 
170  if (!(xBorderSup < (int)imgW && xBorderInf > 0 &&
171  yBorderSup < (int)imgH && yBorderInf > 0))
172  continue; // nope, skip.
173  }
174 
175  if (do_filter_min_dist)
176  {
177  // Check the min-distance:
178  const auto sect_ix = size_t(kp.pt.x * occupied_grid_cell_size_inv);
179  const auto sect_iy = size_t(kp.pt.y * occupied_grid_cell_size_inv);
180 
181  if (occupied_sections(sect_ix, sect_iy))
182  continue; // Already occupied! skip.
183 
184  // Mark section as occupied
185  occupied_sections(sect_ix, sect_iy) = true;
186  if (sect_ix > 0) occupied_sections(sect_ix - 1, sect_iy) = true;
187  if (sect_iy > 0) occupied_sections(sect_ix, sect_iy - 1) = true;
188  if (sect_ix < grid_lx - 1)
189  occupied_sections(sect_ix + 1, sect_iy) = true;
190  if (sect_iy < grid_ly - 1)
191  occupied_sections(sect_ix, sect_iy + 1) = true;
192  }
193 
194  // All tests passed: add new feature:
195  CFeature ft;
196  ft.type = featORB;
197  ft.keypoint.ID = f_id++;
198  ft.keypoint.pt.x = kp.pt.x;
199  ft.keypoint.pt.y = kp.pt.y;
200  ft.response = kp.response;
201  ft.orientation = kp.angle;
202  ft.keypoint.octave = kp.octave;
203  ft.patchSize = 0;
204 
205  // descriptor
206  ft.descriptors.ORB.emplace();
207  ft.descriptors.ORB->resize(cv_descs.cols);
208  for (int m = 0; m < cv_descs.cols; ++m)
209  (*ft.descriptors.ORB)[m] = cv_descs.at<uchar>(idx, m);
210 
211  if (options.ORBOptions.extract_patch && options.patchSize > 0)
212  {
213  ft.patchSize = options.patchSize; // The size of the feature patch
214 
215  ft.patch.emplace();
216  inImg.extract_patch(
217  *ft.patch, round(kp.pt.x) - patch_size_2,
218  round(kp.pt.y) - patch_size_2, options.patchSize,
219  options.patchSize); // Image patch surronding the feature
220  }
221 
222  feats.emplace_back(std::move(ft));
223  c_feats++;
224  }
225 #endif
226  MRPT_END
227 }
228 
230  const CImage& in_img, CFeatureList& in_features)
231 {
232 #if MRPT_HAS_OPENCV
233  using namespace cv;
235  profiler, "internal_computeORBDescriptors");
236 
237  const size_t n_feats = in_features.size();
238  const CImage inImg_gray(in_img, FAST_REF_OR_CONVERT_TO_GRAY);
239 
240  // convert from CFeatureList to vector<KeyPoint>
241  vector<KeyPoint> cv_feats(n_feats);
242  for (size_t k = 0; k < n_feats; ++k)
243  {
244  KeyPoint& kp = cv_feats[k];
245  kp.pt.x = in_features[k].keypoint.pt.x;
246  kp.pt.y = in_features[k].keypoint.pt.y;
247  kp.angle = in_features[k].orientation;
248  kp.size = in_features[k].keypoint.octave;
249  } // end-for
250 
251  const Mat& cvImg = inImg_gray.asCvMatRef();
252  Mat cv_descs;
253 
254 #if MRPT_OPENCV_VERSION_NUM < 0x300
255  Ptr<Feature2D> orb = Algorithm::create<Feature2D>("Feature2D.ORB");
256  orb->operator()(
257  cvImg, cv::noArray(), cv_feats, cv_descs,
258  true /* use_precomputed_feats */);
259 #else
260  Ptr<cv::ORB> orb = cv::ORB::create(
261  n_feats, options.ORBOptions.scale_factor, options.ORBOptions.n_levels);
262  orb->detectAndCompute(
263  cvImg, cv::noArray(), cv_feats, cv_descs,
264  true /* use_precomputed_feats */);
265 #endif
266 
267  // add descriptor to CFeatureList
268  for (size_t k = 0; k < n_feats; ++k)
269  {
270  in_features[k].descriptors.ORB.emplace();
271  auto& orb_desc = *in_features[k].descriptors.ORB;
272  orb_desc.resize(cv_descs.cols);
273  for (int i = 0; i < cv_descs.cols; ++i)
274  orb_desc[i] = cv_descs.at<uchar>(k, i);
275 
276  } // end-for
277 #endif
278 
279 } // end-internal_computeORBImageDescriptors
#define MRPT_START
Definition: exceptions.h:241
std::optional< std::vector< uint8_t > > ORB
ORB feature descriptor.
Definition: CFeature.h:135
A safe way to call enter() and leave() of a mrpt::system::CTimeLogger upon construction and destructi...
TKeyPointMethod type
Keypoint method used to detect this feature.
Definition: CFeature.h:73
void fill(const Scalar &val)
size_t size() const
Definition: CFeature.h:352
cv::Mat & asCvMatRef()
Get a reference to the internal cv::Mat, which can be resized, etc.
Definition: CImage.cpp:227
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:849
STL namespace.
TFeatureID ID
ID of the feature.
Definition: TKeyPoint.h:39
Definition: img/CImage.h:23
A helper struct to sort keypoints by their response: It can be used with these types: ...
Definition: TKeyPoint.h:283
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:818
void resize(size_t N)
Definition: CFeature.h:358
TKeyPointf keypoint
Definition: CFeature.h:64
std::optional< mrpt::img::CImage > patch
A patch of the image surrounding the feature.
Definition: CFeature.h:67
void internal_computeORBDescriptors(const mrpt::img::CImage &in_img, CFeatureList &in_features)
Compute the ORB descriptor of the provided features into the input image.
Classes for computer vision, detectors, features, etc.
Definition: CDifodo.h:17
A generic 2D feature from an image, extracted with CFeatureExtraction Each feature may have one or mo...
Definition: CFeature.h:53
void extractFeaturesORB(const mrpt::img::CImage &img, CFeatureList &feats, const unsigned int init_ID=0, const unsigned int nDesiredFeatures=0, const TImageROI &ROI=TImageROI())
Extract features from the image based on the ORB method.
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:275
float response
A measure of the "goodness" of the feature.
Definition: CFeature.h:79
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
TDescriptors descriptors
Definition: CFeature.h:157
#define MRPT_END
Definition: exceptions.h:245
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 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:1166
float orientation
Main orientation of the feature.
Definition: CFeature.h:81
This template class provides the basic functionality for a general 2D any-size, resizable container o...
uint16_t patchSize
Size of the patch (patchSize x patchSize) (it must be an odd number)
Definition: CFeature.h:70
void emplace_back(CFeature &&f)
Definition: CFeature.h:364
ORB detector and descriptor, OpenCV&#39;s implementation.
pixel_coords_t pt
Coordinates in the image.
Definition: TKeyPoint.h:36
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148
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: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020