Main MRPT website > C++ reference for MRPT 1.9.9
CFeatureExtraction_SURF.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-2017, 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 #ifdef HAVE_OPENCV_NONFREE // MRPT_HAS_OPENCV_NONFREE
18 #include <opencv2/nonfree/nonfree.hpp>
19 #endif
20 #ifdef HAVE_OPENCV_FEATURES2D
21 #include <opencv2/features2d/features2d.hpp>
22 #endif
23 #ifdef HAVE_OPENCV_XFEATURES2D
24 #include <opencv2/xfeatures2d.hpp>
25 #endif
26 
27 using namespace mrpt;
28 using namespace mrpt::vision;
29 using namespace mrpt::system;
30 using namespace mrpt::utils;
31 using namespace std;
32 
33 // Was: MRPT_HAS_OPENCV_NONFREE
34 #if defined(HAVE_OPENCV_XFEATURES2D) || \
35  (MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240)
36 #define HAVE_OPENCV_WITH_SURF 1
37 #else
38 #define HAVE_OPENCV_WITH_SURF 0
39 #endif
40 
41 /************************************************************************************************
42 * extractFeaturesSURF *
43 ************************************************************************************************/
45  const mrpt::utils::CImage& inImg, CFeatureList& feats, unsigned int init_ID,
46  unsigned int nDesiredFeatures, const TImageROI& ROI) const
47 {
48 #if HAVE_OPENCV_WITH_SURF
49  using namespace cv;
50 
51  const CImage img_grayscale(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
52  const Mat img = cvarrToMat(img_grayscale.getAs<IplImage>());
53 
54  vector<KeyPoint> cv_feats; // OpenCV keypoint output vector
55  Mat cv_descs; // OpenCV descriptor output
56 
57 // gb redesign start -make sure that the Algorithm::create is used only for
58 // older openCV versions
59 #if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240
60 
61  Ptr<Feature2D> surf = Algorithm::create<Feature2D>("Feature2D.SURF");
62  if (surf.empty())
63  CV_Error(
64  CV_StsNotImplemented,
65  "OpenCV <v3.0 was built without SURF support");
66 
67  surf->set("hessianThreshold", options.SURFOptions.hessianThreshold);
68  surf->set("nOctaves", options.SURFOptions.nOctaves);
69  surf->set("nOctaveLayers", options.SURFOptions.nLayersPerOctave);
70  // surf->set("upright", params.upright != 0);
71  surf->set("extended", options.SURFOptions.rotation_invariant);
72 
73  surf->operator()(img, Mat(), cv_feats, cv_descs);
74 
75 #else
76  Ptr<xfeatures2d::SURF> surf =
77  xfeatures2d::SURF::create(100.0, 4, 3, 0, 0); // gb
78 
79  if (surf.empty())
80  CV_Error(
81  CV_StsNotImplemented, "OpenCV 3.0 was built without SURF support");
82  surf->setHessianThreshold(options.SURFOptions.hessianThreshold);
83  surf->setNOctaves(options.SURFOptions.nOctaves);
84  surf->setNOctaveLayers(options.SURFOptions.nLayersPerOctave);
85  // surf->setUpright(params.upright != 0);
86  surf->setExtended(options.SURFOptions.rotation_invariant);
87  // gb redesign end
88  surf->detectAndCompute(img, Mat(), cv_feats, cv_descs);
89 #endif
90 
91  // -----------------------------------------------------------------
92  // MRPT Wrapping
93  // -----------------------------------------------------------------
94  feats.clear();
95  unsigned int nCFeats = init_ID;
96  int offset = (int)this->options.patchSize / 2 + 1;
97  unsigned int imgH = inImg.getHeight();
98  unsigned int imgW = inImg.getWidth();
99 
100  const size_t n_feats =
101  (nDesiredFeatures == 0)
102  ? cv_feats.size()
103  : std::min((size_t)nDesiredFeatures, cv_feats.size());
104 
105  for (size_t i = 0; i < n_feats; i++)
106  {
107  // Get the OpenCV SURF point
108  CFeature::Ptr ft = mrpt::make_aligned_shared<CFeature>();
109  const KeyPoint& point = cv_feats[i];
110 
111  const int xBorderInf = (int)floor(point.pt.x - options.patchSize / 2);
112  const int xBorderSup = (int)floor(point.pt.x + options.patchSize / 2);
113  const int yBorderInf = (int)floor(point.pt.y - options.patchSize / 2);
114  const int yBorderSup = (int)floor(point.pt.y + options.patchSize / 2);
115 
116  if (options.patchSize == 0 ||
117  ((xBorderSup < (int)imgW) && (xBorderInf > 0) &&
118  (yBorderSup < (int)imgH) && (yBorderInf > 0)))
119  {
120  ft->type = featSURF;
121  ft->x = point.pt.x; // X position
122  ft->y = point.pt.y; // Y position
123  ft->orientation = point.angle; // Orientation
124  ft->scale = point.size * 1.2 / 9; // Scale
125  ft->ID = nCFeats++; // Feature ID into extraction
126  ft->patchSize = options.patchSize; // The size of the feature patch
127 
128  if (options.patchSize > 0)
129  {
130  inImg.extract_patch(
131  ft->patch, round(ft->x) - offset, round(ft->y) - offset,
132  options.patchSize,
133  options.patchSize); // Image patch surronding the feature
134  }
135 
136  // Get the SURF descriptor
137  ft->descriptors.SURF.resize(cv_descs.cols);
138  for (int m = 0; m < cv_descs.cols; ++m)
139  ft->descriptors.SURF[m] = cv_descs.at<float>(i, m);
140 
141  feats.push_back(ft);
142 
143  } // end if
144  } // end for
145 
146 #else
148  "Method not available: MRPT compiled without OpenCV, or against a "
149  "version of OpenCV without SURF")
150 #endif // MRPT_HAS_OPENCV
151 } // end extractFeaturesSURF
152 
153 /************************************************************************************************
154 * internal_computeSurfDescriptors
155 ************************************************************************************************/
157  const mrpt::utils::CImage& inImg, CFeatureList& in_features) const
158 {
159 #if HAVE_OPENCV_WITH_SURF
160  using namespace cv;
161 
162  if (in_features.empty()) return;
163 
164  const CImage img_grayscale(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
165  const Mat img = cvarrToMat(img_grayscale.getAs<IplImage>());
166 
167  vector<KeyPoint> cv_feats; // OpenCV keypoint output vector
168  Mat cv_descs; // OpenCV descriptor output
169 
170  // Fill in the desired key-points:
171  cv_feats.resize(in_features.size());
172  for (size_t i = 0; i < in_features.size(); ++i)
173  {
174  cv_feats[i].pt.x = in_features[i]->x;
175  cv_feats[i].pt.y = in_features[i]->y;
176  cv_feats[i].size = 16; // sizes[layer];
177  }
178 
179 // Only computes the descriptors:
180 // gb redesign
181 #if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240
182 
183  Ptr<Feature2D> surf = Algorithm::create<Feature2D>("Feature2D.SURF");
184  if (surf.empty())
185  CV_Error(CV_StsNotImplemented, "OpenCV was built without SURF support");
186  surf->set("hessianThreshold", options.SURFOptions.hessianThreshold);
187  surf->set("nOctaves", options.SURFOptions.nOctaves);
188  surf->set("nOctaveLayers", options.SURFOptions.nLayersPerOctave);
189  // surf->set("upright", params.upright != 0);
190  surf->set("extended", options.SURFOptions.rotation_invariant);
191 
192  surf->compute(
193  img, cv_feats,
194  cv_descs); // is that correct? the version from above is:
195 // surf->operator()(img, Mat(), cv_feats, cv_descs);
196 #else
197 
198  Ptr<xfeatures2d::SURF> surf =
199  xfeatures2d::SURF::create(100.0, 4, 3, 0, 0); // gb
200 
201  if (surf.empty())
202  CV_Error(CV_StsNotImplemented, "OpenCV was built without SURF support");
203  surf->setHessianThreshold(options.SURFOptions.hessianThreshold);
204  surf->setNOctaves(options.SURFOptions.nOctaves);
205  surf->setNOctaveLayers(options.SURFOptions.nLayersPerOctave);
206  // surf->setUpright(params.upright != 0);
207  surf->setExtended(options.SURFOptions.rotation_invariant);
208 
209  surf->detectAndCompute(img, Mat(), cv_feats, cv_descs);
210 #endif
211  // gb redesign end
212  // -----------------------------------------------------------------
213  // MRPT Wrapping
214  // -----------------------------------------------------------------
215  CFeatureList::iterator itList;
216  int i;
217  for (i = 0, itList = in_features.begin(); itList != in_features.end();
218  itList++, i++)
219  {
220  // Get the OpenCV SURF point
221  CFeature::Ptr ft = *itList;
222  const KeyPoint& point = cv_feats[i];
223 
224  ft->orientation = point.angle; // Orientation
225  ft->scale = point.size * 1.2 / 9; // Scale
226 
227  // Get the SURF descriptor
228  ft->descriptors.SURF.resize(cv_descs.cols);
229  for (int m = 0; m < cv_descs.cols; ++m)
230  ft->descriptors.SURF[m] =
231  cv_descs.at<float>(i, m); // Get the SURF descriptor
232  } // end for
233 
234 #else
236  "Method not available: MRPT compiled without OpenCV, or against a "
237  "version of OpenCV without SURF")
238 #endif // MRPT_HAS_OPENCV
239 } // end internal_computeSurfDescriptors
std::shared_ptr< CFeature > Ptr
Definition: CFeature.h:58
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define min(a, b)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:118
size_t size() const
Definition: CFeature.h:387
#define THROW_EXCEPTION(msg)
GLintptr offset
Definition: glext.h:3925
void internal_computeSurfDescriptors(const mrpt::utils::CImage &in_img, CFeatureList &in_features) const
Compute the SURF descriptor of the provided features into the input image.
STL namespace.
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img.getAs<IplImage>()" so we can avoid here including OpenCV&#39;s headers.
Definition: CImage.h:587
void extractFeaturesSURF(const mrpt::utils::CImage &img, CFeatureList &feats, unsigned int init_ID=0, unsigned int nDesiredFeatures=0, const TImageROI &ROI=TImageROI()) const
Extract features from the image based on the SURF method.
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:869
GLint GLvoid * img
Definition: glext.h:3763
size_t getHeight() const override
Returns the height of the image in pixels.
Definition: CImage.cpp:897
Classes for computer vision, detectors, features, etc.
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:305
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Speeded Up Robust Feature [BAY&#39;06].
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:1367
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
TInternalFeatList::iterator iterator
Definition: CFeature.h:366
void push_back(const CFeature::Ptr &f)
Definition: CFeature.h:399



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019