18 #include <mrpt/3rdparty/do_opencv_includes.h> 23 #ifdef HAVE_OPENCV_XFEATURES2D 24 #include <opencv2/xfeatures2d.hpp> 26 #ifdef HAVE_OPENCV_LINE_DESCRIPTOR 27 #include <opencv2/line_descriptor.hpp> 28 using namespace cv::line_descriptor;
38 #if defined(HAVE_OPENCV_XFEATURES2D) && defined(HAVE_OPENCV_LINE_DESCRIPTOR) 39 #define HAVE_OPENCV_WITH_LSD 1 41 #define HAVE_OPENCV_WITH_LSD 0 44 void CFeatureExtraction::extractFeaturesLSD(
46 unsigned int nDesiredFeatures,
const TImageROI& ROI)
52 #if (!HAVE_OPENCV_WITH_LSD) 54 "This function requires OpenCV modules: xfeatures2d, line_descriptor");
58 vector<KeyPoint> cv_feats;
59 vector<KeyLine> cv_line;
65 cv::Mat mask = Mat::ones(theImg.size(), CV_8UC1);
67 Ptr<LSDDetector> bd = LSDDetector::createLSDDetector();
70 cv::Mat output = theImg.clone();
72 theImg, cv_line, options.LSDOptions.scale, options.LSDOptions.nOctaves,
76 const size_t N = cv_line.size();
81 for (
size_t i = 0; i < N; i++)
83 for (
size_t j = i + 1; j < N; j++)
85 if (cv_line.at(j).lineLength > cv_line.at(i).lineLength)
87 KeyLine temp_line = cv_line.at(i);
88 cv_line.at(i) = cv_line.at(j);
89 cv_line.at(j) = temp_line;
108 (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
109 const int offset = (int)this->options.patchSize / 2 + 1;
110 const size_t size_2 = options.patchSize / 2;
112 const size_t imgW = inImg.
getWidth();
114 unsigned int cont = 0;
117 if (!options.addNewFeatures) feats.
clear();
120 if (output.channels() == 1) cvtColor(output, output, COLOR_GRAY2BGR);
122 while (cont != nMax && i != N)
124 KeyLine kl = cv_line[i];
129 Point pt1 = Point2f(kl.startPointX, kl.startPointY);
130 Point pt2 = Point2f(kl.endPointX, kl.endPointY);
132 kp.pt.x = (pt1.x + pt2.x) / 2;
133 kp.pt.y = (pt1.y + pt2.y) / 2;
136 const int xBorderInf = (int)floor(kp.pt.x - size_2);
137 const int xBorderSup = (int)floor(kp.pt.x + size_2);
138 const int yBorderInf = (int)floor(kp.pt.y - size_2);
139 const int yBorderSup = (int)floor(kp.pt.y + size_2);
141 if (!(xBorderSup < (
int)imgW && xBorderInf > 0 &&
142 yBorderSup < (int)imgH && yBorderInf > 0))
158 if (options.patchSize > 0)
162 p,
round(kp.pt.x) - offset,
round(kp.pt.y) - offset,
165 ft.
patch = std::move(p);
177 void CFeatureExtraction::internal_computeBLDLineDescriptors(
180 #if (!HAVE_OPENCV_WITH_LSD) 182 "This function requires OpenCV modules: xfeatures2d, line_descriptor");
186 profiler,
"internal_computeBLDLineDescriptors");
190 if (in_features.
empty())
return;
195 vector<KeyPoint> cv_feats;
198 cv::Mat mask = Mat::ones(img.size(), CV_8UC1);
200 BinaryDescriptor::Params
params;
201 params.ksize_ = options.BLDOptions.ksize_;
202 params.reductionRatio = options.BLDOptions.reductionRatio;
203 params.numOfOctave_ = options.BLDOptions.numOfOctave;
204 params.widthOfBand_ = options.BLDOptions.widthOfBand;
206 Ptr<BinaryDescriptor> bd2 =
207 BinaryDescriptor::createBinaryDescriptor(
params);
209 std::vector<KeyLine> keylines;
211 bd2->detect(img, keylines, mask);
214 bd2->compute(img, keylines, cv_descs);
215 keylines.resize(in_features.
size());
221 for (
auto& ft : in_features)
224 ft.descriptors.BLD.emplace();
225 auto& desc = ft.descriptors.BLD.value();
226 desc.resize(cv_descs.cols);
227 for (
int m = 0; m < cv_descs.cols; ++m)
228 desc[m] = cv_descs.at<
int>(i, m);
231 #endif // end of opencv3 version check
uint64_t TFeatureID
Definition of a feature ID.
#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.
mrpt::vision::TStereoCalibParams params
TFeatureID ID
ID of the feature.
LSD detector, OpenCV's implementation.
A structure for defining a ROI within an image.
This base provides a set of functions for maths stuff.
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...
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.
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 y2[2]
Coordinates for a LSD Detector to represent a line.
float response
A measure of the "goodness" of the feature (typically, the KLT_response value)
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.