15 #include <mrpt/3rdparty/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 44 unsigned int nDesiredFeatures,
const TImageROI& ROI)
46 #if HAVE_OPENCV_WITH_SURF 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)))
129 if (options.patchSize > 0)
133 p,
round(point.pt.x) - offset,
round(point.pt.y) - offset,
134 options.patchSize, options.patchSize);
136 ft.
patch = std::move(p);
142 desc.resize(cv_descs.cols);
143 for (
int m = 0; m < cv_descs.cols; ++m)
144 desc[m] = cv_descs.at<
float>(i, m);
153 "Method not available: MRPT compiled without OpenCV, or against a " 154 "version of OpenCV without SURF");
155 #endif // MRPT_HAS_OPENCV 161 #if HAVE_OPENCV_WITH_SURF 164 profiler,
"internal_computeSurfDescriptors");
166 if (in_features.
empty())
return;
171 vector<KeyPoint> cv_feats;
175 cv_feats.resize(in_features.
size());
176 for (
size_t i = 0; i < in_features.
size(); ++i)
178 cv_feats[i].pt.x = in_features[i].keypoint.pt.x;
179 cv_feats[i].pt.y = in_features[i].keypoint.pt.y;
180 cv_feats[i].size = 1 << in_features[i].keypoint.octave;
185 #if MRPT_OPENCV_VERSION_NUM < 0x300 && MRPT_OPENCV_VERSION_NUM >= 0x240 187 Ptr<Feature2D> surf = Algorithm::create<Feature2D>(
"Feature2D.SURF");
189 CV_Error(CV_StsNotImplemented,
"OpenCV was built without SURF support");
190 surf->set(
"hessianThreshold", options.SURFOptions.hessianThreshold);
191 surf->set(
"nOctaves", options.SURFOptions.nOctaves);
192 surf->set(
"nOctaveLayers", options.SURFOptions.nLayersPerOctave);
194 surf->set(
"extended", options.SURFOptions.rotation_invariant);
202 Ptr<xfeatures2d::SURF> surf =
203 xfeatures2d::SURF::create(100.0, 4, 3, 0, 0);
206 CV_Error(CV_StsNotImplemented,
"OpenCV was built without SURF support");
207 surf->setHessianThreshold(options.SURFOptions.hessianThreshold);
208 surf->setNOctaves(options.SURFOptions.nOctaves);
209 surf->setNOctaveLayers(options.SURFOptions.nLayersPerOctave);
211 surf->setExtended(options.SURFOptions.rotation_invariant);
213 surf->detectAndCompute(img, Mat(), cv_feats, cv_descs);
220 for (
auto& ft : in_features)
223 const KeyPoint& point = cv_feats[i];
225 ft.orientation = point.angle;
226 ft.keypoint.octave = point.octave;
229 ft.descriptors.SURF.emplace();
230 auto& desc = ft.descriptors.SURF.value();
231 desc.resize(cv_descs.cols);
232 for (
int m = 0; m < cv_descs.cols; ++m)
233 desc[m] = cv_descs.at<
float>(i, m);
240 "Method not available: MRPT compiled without OpenCV, or against a " 241 "version of OpenCV without SURF");
242 #endif // MRPT_HAS_OPENCV
#define THROW_EXCEPTION(msg)
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.
cv::Mat & asCvMatRef()
Get a reference to the internal cv::Mat, which can be resized, etc.
size_t getHeight() const override
Returns the height of the image in pixels.
TFeatureID ID
ID of the feature.
A structure for defining a ROI within an image.
size_t getWidth() const override
Returns the width of the image in pixels.
std::optional< mrpt::img::CImage > patch
A patch of the image surrounding the feature.
Classes for computer vision, detectors, features, etc.
A generic 2D feature from an image, extracted with CFeatureExtraction Each feature may have one or mo...
Speeded Up Robust Feature [BAY'06].
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.
std::optional< std::vector< float > > SURF
SURF feature descriptor.
uint8_t octave
The image octave the image was found in: 0=original image, 1=1/2 image, 2=1/4 image, etc.
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)...
float orientation
Main orientation of the feature.
uint16_t patchSize
Size of the patch (patchSize x patchSize) (it must be an odd number)
void emplace_back(CFeature &&f)
pixel_coords_t pt
Coordinates in the image.
A class for storing images as grayscale or RGB bitmaps.
int round(const T value)
Returns the closer integer (int) to x.