15 #include <mrpt/otherlibs/do_opencv_includes.h> 17 #ifdef HAVE_OPENCV_NONFREE // MRPT_HAS_OPENCV_NONFREE 18 #include <opencv2/nonfree/nonfree.hpp> 20 #ifdef HAVE_OPENCV_FEATURES2D 21 #include <opencv2/features2d/features2d.hpp> 23 #ifdef HAVE_OPENCV_XFEATURES2D 24 #include <opencv2/xfeatures2d.hpp> 34 #if defined(HAVE_OPENCV_XFEATURES2D) || \ 35 (MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240) 36 #define HAVE_OPENCV_WITH_SURF 1 38 #define HAVE_OPENCV_WITH_SURF 0 46 unsigned int nDesiredFeatures,
const TImageROI& ROI)
const 48 #if HAVE_OPENCV_WITH_SURF 52 const Mat
img = cvarrToMat(img_grayscale.
getAs<IplImage>());
54 vector<KeyPoint> cv_feats;
59 #if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240 61 Ptr<Feature2D> surf = Algorithm::create<Feature2D>(
"Feature2D.SURF");
65 "OpenCV <v3.0 was built without SURF support");
67 surf->set(
"hessianThreshold", options.SURFOptions.hessianThreshold);
68 surf->set(
"nOctaves", options.SURFOptions.nOctaves);
69 surf->set(
"nOctaveLayers", options.SURFOptions.nLayersPerOctave);
71 surf->set(
"extended", options.SURFOptions.rotation_invariant);
73 surf->operator()(
img, Mat(), cv_feats, cv_descs);
76 Ptr<xfeatures2d::SURF> surf =
77 xfeatures2d::SURF::create(100.0, 4, 3, 0, 0);
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);
86 surf->setExtended(options.SURFOptions.rotation_invariant);
88 surf->detectAndCompute(
img, Mat(), cv_feats, cv_descs);
95 unsigned int nCFeats = init_ID;
96 int offset = (int)this->options.patchSize / 2 + 1;
98 unsigned int imgW = inImg.
getWidth();
100 const size_t n_feats =
101 (nDesiredFeatures == 0)
103 :
std::min((
size_t)nDesiredFeatures, cv_feats.size());
105 for (
size_t i = 0; i < n_feats; i++)
109 const KeyPoint& point = cv_feats[i];
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);
116 if (options.patchSize == 0 ||
117 ((xBorderSup < (
int)imgW) && (xBorderInf > 0) &&
118 (yBorderSup < (int)imgH) && (yBorderInf > 0)))
123 ft->orientation = point.angle;
124 ft->scale = point.size * 1.2 / 9;
126 ft->patchSize = options.patchSize;
128 if (options.patchSize > 0)
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);
148 "Method not available: MRPT compiled without OpenCV, or against a " 149 "version of OpenCV without SURF")
150 #endif // MRPT_HAS_OPENCV 159 #if HAVE_OPENCV_WITH_SURF 162 if (in_features.
empty())
return;
165 const Mat
img = cvarrToMat(img_grayscale.
getAs<IplImage>());
167 vector<KeyPoint> cv_feats;
171 cv_feats.resize(in_features.
size());
172 for (
size_t i = 0; i < in_features.
size(); ++i)
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;
181 #if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240 183 Ptr<Feature2D> surf = Algorithm::create<Feature2D>(
"Feature2D.SURF");
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);
190 surf->set(
"extended", options.SURFOptions.rotation_invariant);
198 Ptr<xfeatures2d::SURF> surf =
199 xfeatures2d::SURF::create(100.0, 4, 3, 0, 0);
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);
207 surf->setExtended(options.SURFOptions.rotation_invariant);
209 surf->detectAndCompute(
img, Mat(), cv_feats, cv_descs);
217 for (i = 0, itList = in_features.
begin(); itList != in_features.
end();
222 const KeyPoint& point = cv_feats[i];
224 ft->orientation = point.angle;
225 ft->scale = point.size * 1.2 / 9;
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);
236 "Method not available: MRPT compiled without OpenCV, or against a " 237 "version of OpenCV without SURF")
238 #endif // MRPT_HAS_OPENCV std::shared_ptr< CFeature > Ptr
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
A class for storing images as grayscale or RGB bitmaps.
#define THROW_EXCEPTION(msg)
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's headers.
A structure for defining a ROI within an image.
size_t getWidth() const override
Returns the width of the image in pixels.
size_t getHeight() const override
Returns the height of the image in pixels.
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.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Speeded Up Robust Feature [BAY'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)...
int round(const T value)
Returns the closer integer (int) to x.
TInternalFeatList::iterator iterator
void push_back(const CFeature::Ptr &f)