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>
35 #if defined(HAVE_OPENCV_XFEATURES2D) || \
36 (MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240)
37 #define HAVE_OPENCV_WITH_SURF 1
39 #define HAVE_OPENCV_WITH_SURF 0
47 unsigned int nDesiredFeatures,
const TImageROI& ROI)
const
49 #if HAVE_OPENCV_WITH_SURF
53 const Mat
img = cvarrToMat(img_grayscale.
getAs<IplImage>());
55 vector<KeyPoint> cv_feats;
60 #if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240
62 Ptr<Feature2D> surf = Algorithm::create<Feature2D>(
"Feature2D.SURF");
66 "OpenCV <v3.0 was built without SURF support");
68 surf->set(
"hessianThreshold", options.SURFOptions.hessianThreshold);
69 surf->set(
"nOctaves", options.SURFOptions.nOctaves);
70 surf->set(
"nOctaveLayers", options.SURFOptions.nLayersPerOctave);
72 surf->set(
"extended", options.SURFOptions.rotation_invariant);
74 surf->operator()(
img, Mat(), cv_feats, cv_descs);
77 Ptr<xfeatures2d::SURF> surf =
78 xfeatures2d::SURF::create(100.0, 4, 3, 0, 0);
82 CV_StsNotImplemented,
"OpenCV 3.0 was built without SURF support");
83 surf->setHessianThreshold(options.SURFOptions.hessianThreshold);
84 surf->setNOctaves(options.SURFOptions.nOctaves);
85 surf->setNOctaveLayers(options.SURFOptions.nLayersPerOctave);
87 surf->setExtended(options.SURFOptions.rotation_invariant);
89 surf->detectAndCompute(
img, Mat(), cv_feats, cv_descs);
96 unsigned int nCFeats = init_ID;
97 int offset = (int)this->options.patchSize / 2 + 1;
99 unsigned int imgW = inImg.
getWidth();
101 const size_t n_feats =
102 (nDesiredFeatures == 0)
104 :
std::min((
size_t)nDesiredFeatures, cv_feats.size());
106 for (
size_t i = 0; i < n_feats; i++)
110 const KeyPoint& point = cv_feats[i];
112 const int xBorderInf = (int)floor(point.pt.x - options.patchSize / 2);
113 const int xBorderSup = (int)floor(point.pt.x + options.patchSize / 2);
114 const int yBorderInf = (int)floor(point.pt.y - options.patchSize / 2);
115 const int yBorderSup = (int)floor(point.pt.y + options.patchSize / 2);
117 if (options.patchSize == 0 ||
118 ((xBorderSup < (
int)imgW) && (xBorderInf > 0) &&
119 (yBorderSup < (int)imgH) && (yBorderInf > 0)))
124 ft->orientation = point.angle;
125 ft->scale = point.size * 1.2 / 9;
127 ft->patchSize = options.patchSize;
129 if (options.patchSize > 0)
138 ft->descriptors.SURF.resize(cv_descs.cols);
139 for (
int m = 0; m < cv_descs.cols; ++m)
140 ft->descriptors.SURF[m] = cv_descs.at<
float>(i, m);
149 "Method not available: MRPT compiled without OpenCV, or against a "
150 "version of OpenCV without SURF")
151 #endif // MRPT_HAS_OPENCV
160 #if HAVE_OPENCV_WITH_SURF
163 if (in_features.
empty())
return;
166 const Mat
img = cvarrToMat(img_grayscale.
getAs<IplImage>());
168 vector<KeyPoint> cv_feats;
172 cv_feats.resize(in_features.
size());
173 for (
size_t i = 0; i < in_features.
size(); ++i)
175 cv_feats[i].pt.x = in_features[i]->x;
176 cv_feats[i].pt.y = in_features[i]->y;
177 cv_feats[i].size = 16;
182 #if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240
184 Ptr<Feature2D> surf = Algorithm::create<Feature2D>(
"Feature2D.SURF");
186 CV_Error(CV_StsNotImplemented,
"OpenCV was built without SURF support");
187 surf->set(
"hessianThreshold", options.SURFOptions.hessianThreshold);
188 surf->set(
"nOctaves", options.SURFOptions.nOctaves);
189 surf->set(
"nOctaveLayers", options.SURFOptions.nLayersPerOctave);
191 surf->set(
"extended", options.SURFOptions.rotation_invariant);
199 Ptr<xfeatures2d::SURF> surf =
200 xfeatures2d::SURF::create(100.0, 4, 3, 0, 0);
203 CV_Error(CV_StsNotImplemented,
"OpenCV was built without SURF support");
204 surf->setHessianThreshold(options.SURFOptions.hessianThreshold);
205 surf->setNOctaves(options.SURFOptions.nOctaves);
206 surf->setNOctaveLayers(options.SURFOptions.nLayersPerOctave);
208 surf->setExtended(options.SURFOptions.rotation_invariant);
210 surf->detectAndCompute(
img, Mat(), cv_feats, cv_descs);
218 for (i = 0, itList = in_features.
begin(); itList != in_features.
end();
223 const KeyPoint& point = cv_feats[i];
225 ft->orientation = point.angle;
226 ft->scale = point.size * 1.2 / 9;
229 ft->descriptors.SURF.resize(cv_descs.cols);
230 for (
int m = 0; m < cv_descs.cols; ++m)
231 ft->descriptors.SURF[m] =
232 cv_descs.at<
float>(i, m);
237 "Method not available: MRPT compiled without OpenCV, or against a "
238 "version of OpenCV without SURF")
239 #endif // MRPT_HAS_OPENCV