Main MRPT website > C++ reference for MRPT 1.5.7
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 
15 // Universal include for all versions of OpenCV
16 #include <mrpt/otherlibs/do_opencv_includes.h>
17 
18 #ifdef HAVE_OPENCV_NONFREE //MRPT_HAS_OPENCV_NONFREE
19 # include <opencv2/nonfree/nonfree.hpp>
20 #endif
21 #ifdef HAVE_OPENCV_FEATURES2D
22 # include <opencv2/features2d/features2d.hpp>
23 #endif
24 #ifdef HAVE_OPENCV_XFEATURES2D
25 # include <opencv2/xfeatures2d.hpp>
26 #endif
27 
28 
29 using namespace mrpt;
30 using namespace mrpt::vision;
31 using namespace mrpt::system;
32 using namespace mrpt::utils;
33 using namespace std;
34 
35 //Was: MRPT_HAS_OPENCV_NONFREE
36 #if defined(HAVE_OPENCV_XFEATURES2D) || (MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240)
37 # define HAVE_OPENCV_WITH_SURF 1
38 #else
39 # define HAVE_OPENCV_WITH_SURF 0
40 #endif
41 
42 /************************************************************************************************
43 * extractFeaturesSURF *
44 ************************************************************************************************/
46  const mrpt::utils::CImage &inImg,
47  CFeatureList &feats,
48  unsigned int init_ID,
49  unsigned int nDesiredFeatures,
50  const TImageROI &ROI) const
51 {
52 #if HAVE_OPENCV_WITH_SURF
53  using namespace cv;
54 
55  const CImage img_grayscale(inImg, FAST_REF_OR_CONVERT_TO_GRAY);
56  const Mat img = cvarrToMat( img_grayscale.getAs<IplImage>() );
57 
58  vector<KeyPoint> cv_feats; // OpenCV keypoint output vector
59  Mat cv_descs; // OpenCV descriptor output
60 
61 //gb redesign start -make sure that the Algorithm::create is used only for older openCV versions
62 # if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240
63 
64  Ptr<Feature2D> surf = Algorithm::create<Feature2D>("Feature2D.SURF");
65  if( surf.empty() )
66  CV_Error(CV_StsNotImplemented, "OpenCV <v3.0 was built without SURF support");
67 
68  surf->set("hessianThreshold", options.SURFOptions.hessianThreshold);
69  surf->set("nOctaves", options.SURFOptions.nOctaves);
70  surf->set("nOctaveLayers", options.SURFOptions.nLayersPerOctave);
71  //surf->set("upright", params.upright != 0);
72  surf->set("extended", options.SURFOptions.rotation_invariant);
73 
74  surf->operator()(img, Mat(), cv_feats, cv_descs);
75 
76  #else
77  Ptr<xfeatures2d::SURF> surf = xfeatures2d::SURF::create(100.0, 4, 3, 0, 0);//gb
78 
79  if( surf.empty() )
80  CV_Error(CV_StsNotImplemented, "OpenCV 3.0 was built without SURF support");
81  surf->setHessianThreshold(options.SURFOptions.hessianThreshold);
82  surf->setNOctaves(options.SURFOptions.nOctaves);
83  surf->setNOctaveLayers(options.SURFOptions.nLayersPerOctave);
84  //surf->setUpright(params.upright != 0);
85  surf->setExtended(options.SURFOptions.rotation_invariant);
86 //gb redesign end
87  surf->detectAndCompute(img, Mat(), cv_feats, cv_descs);
88  #endif
89 
90  // -----------------------------------------------------------------
91  // MRPT Wrapping
92  // -----------------------------------------------------------------
93  feats.clear();
94  unsigned int nCFeats = init_ID;
95  int offset = (int)this->options.patchSize/2 + 1;
96  unsigned int imgH = inImg.getHeight();
97  unsigned int imgW = inImg.getWidth();
98 
99  const size_t n_feats = (nDesiredFeatures== 0) ?
100  cv_feats.size()
101  :
102  std::min((size_t)nDesiredFeatures, cv_feats.size());
103 
104  for(size_t i = 0; i < n_feats; i++ )
105  {
106  // Get the OpenCV SURF point
107  CFeaturePtr ft = CFeature::Create();
108  const KeyPoint &point = cv_feats[i];
109 
110  const int xBorderInf = (int)floor( point.pt.x - options.patchSize/2 );
111  const int xBorderSup = (int)floor( point.pt.x + options.patchSize/2 );
112  const int yBorderInf = (int)floor( point.pt.y - options.patchSize/2 );
113  const int yBorderSup = (int)floor( point.pt.y + options.patchSize/2 );
114 
115  if( options.patchSize == 0 || ( (xBorderSup < (int)imgW) && (xBorderInf > 0) && (yBorderSup < (int)imgH) && (yBorderInf > 0) ) )
116  {
117  ft->type = featSURF;
118  ft->x = point.pt.x; // X position
119  ft->y = point.pt.y; // Y position
120  ft->orientation = point.angle; // Orientation
121  ft->scale = point.size*1.2/9; // Scale
122  ft->ID = nCFeats++; // Feature ID into extraction
123  ft->patchSize = options.patchSize; // The size of the feature patch
124 
125  if( options.patchSize > 0 )
126  {
127  inImg.extract_patch(
128  ft->patch,
129  round( ft->x ) - offset,
130  round( ft->y ) - offset,
131  options.patchSize,
132  options.patchSize ); // Image patch surronding the feature
133  }
134 
135  // Get the SURF descriptor
136  ft->descriptors.SURF.resize( cv_descs.cols );
137  for( int m = 0; m < cv_descs.cols; ++m )
138  ft->descriptors.SURF[m] = cv_descs.at<float>(i,m);
139 
140  feats.push_back( ft );
141 
142  } // end if
143  } // end for
144 
145 #else
146 
147  THROW_EXCEPTION("Method not available: MRPT compiled without OpenCV, or against a version of OpenCV without SURF")
148 #endif //MRPT_HAS_OPENCV
149 } // end extractFeaturesSURF
150 
151 
152 /************************************************************************************************
153 * internal_computeSurfDescriptors
154 ************************************************************************************************/
156  const mrpt::utils::CImage &inImg,
157  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(img, cv_feats, cv_descs); //is that correct? the version from above is:
193  //surf->operator()(img, Mat(), cv_feats, cv_descs);
194  #else
195 
196  Ptr<xfeatures2d::SURF> surf = xfeatures2d::SURF::create(100.0, 4, 3, 0, 0);//gb
197 
198  if( surf.empty() )
199  CV_Error(CV_StsNotImplemented, "OpenCV was built without SURF support");
200  surf->setHessianThreshold(options.SURFOptions.hessianThreshold);
201  surf->setNOctaves(options.SURFOptions.nOctaves);
202  surf->setNOctaveLayers(options.SURFOptions.nLayersPerOctave);
203  //surf->setUpright(params.upright != 0);
204  surf->setExtended(options.SURFOptions.rotation_invariant);
205 
206  surf->detectAndCompute(img, Mat(), cv_feats, cv_descs);
207  #endif
208 //gb redesign end
209  // -----------------------------------------------------------------
210  // MRPT Wrapping
211  // -----------------------------------------------------------------
212  CFeatureList::iterator itList;
213  int i;
214  for (i=0, itList=in_features.begin();itList!=in_features.end();itList++,i++)
215  {
216  // Get the OpenCV SURF point
217  CFeaturePtr ft = *itList;
218  const KeyPoint &point = cv_feats[i];
219 
220  ft->orientation = point.angle; // Orientation
221  ft->scale = point.size*1.2/9; // Scale
222 
223  // Get the SURF descriptor
224  ft->descriptors.SURF.resize( cv_descs.cols );
225  for( int m = 0; m < cv_descs.cols; ++m )
226  ft->descriptors.SURF[m] = cv_descs.at<float>(i,m); // Get the SURF descriptor
227  } // end for
228 
229 #else
230  THROW_EXCEPTION("Method not available: MRPT compiled without OpenCV, or against a version of OpenCV without SURF")
231 #endif //MRPT_HAS_OPENCV
232 } // end internal_computeSurfDescriptors
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
#define min(a, b)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
size_t size() const
Definition: CFeature.h:280
#define THROW_EXCEPTION(msg)
GLintptr offset
Definition: glext.h:3780
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:517
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.
GLint GLvoid * img
Definition: glext.h:3645
Classes for computer vision, detectors, features, etc.
void push_back(const CFeaturePtr &f)
Definition: CFeature.h:285
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
Definition: CFeature.h:211
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:1368
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:26
TInternalFeatList::iterator iterator
Definition: CFeature.h:261
static CFeaturePtr Create()
size_t getWidth() const MRPT_OVERRIDE
Returns the width of the image in pixels.
Definition: CImage.cpp:855
size_t getHeight() const MRPT_OVERRIDE
Returns the height of the image in pixels.
Definition: CImage.cpp:884



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019