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