20 #include <mrpt/otherlibs/do_opencv_includes.h> 22 #ifdef HAVE_OPENCV_XFEATURES2D 23 #include <opencv2/xfeatures2d.hpp> 25 #ifdef HAVE_OPENCV_LINE_DESCRIPTOR 26 #include <opencv2/line_descriptor.hpp> 27 using namespace cv::line_descriptor;
37 #if defined(HAVE_OPENCV_XFEATURES2D) && defined(HAVE_OPENCV_LINE_DESCRIPTOR) 38 #define HAVE_OPENCV_WITH_LSD 1 40 #define HAVE_OPENCV_WITH_LSD 0 43 void CFeatureExtraction::extractFeaturesLSD(
45 unsigned int nDesiredFeatures,
const TImageROI& ROI)
const 48 #if (!HAVE_OPENCV_WITH_LSD) 50 "This function requires OpenCV modules: xfeatures2d, line_descriptor");
54 vector<KeyPoint> cv_feats;
55 vector<KeyLine> cv_line;
60 const Mat theImg = cvarrToMat(inImg_gray.
getAs<IplImage>());
64 Ptr<LSDDetector> bd = LSDDetector::createLSDDetector();
67 cv::Mat output = theImg.clone();
69 theImg, cv_line, options.LSDOptions.scale, options.LSDOptions.nOctaves,
73 const size_t N = cv_line.size();
78 for (
size_t i = 0; i < N; i++)
80 for (
size_t j = i + 1; j < N; j++)
82 if (cv_line.at(j).lineLength > cv_line.at(i).lineLength)
84 KeyLine temp_line = cv_line.at(i);
85 cv_line.at(i) = cv_line.at(j);
86 cv_line.at(j) = temp_line;
105 (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
106 const int offset = (int)this->options.patchSize / 2 + 1;
107 const size_t size_2 = options.patchSize / 2;
109 const size_t imgW = inImg.
getWidth();
111 unsigned int cont = 0;
114 if (!options.addNewFeatures) feats.
clear();
117 if (output.channels() == 1) cvtColor(output, output, COLOR_GRAY2BGR);
119 while (cont != nMax && i != N)
121 KeyLine kl = cv_line[i];
126 Point pt1 = Point2f(kl.startPointX, kl.startPointY);
127 Point pt2 = Point2f(kl.endPointX, kl.endPointY);
129 kp.pt.x = (pt1.x + pt2.x) / 2;
130 kp.pt.y = (pt1.y + pt2.y) / 2;
133 const int xBorderInf = (int)floor(kp.pt.x - size_2);
134 const int xBorderSup = (int)floor(kp.pt.x + size_2);
135 const int yBorderInf = (int)floor(kp.pt.y - size_2);
136 const int yBorderSup = (int)floor(kp.pt.y + size_2);
138 if (!(xBorderSup < (
int)imgW && xBorderInf > 0 &&
139 yBorderSup < (int)imgH && yBorderInf > 0))
152 ft->response = kl.response;
154 ft->scale = kl.octave;
160 if (options.patchSize > 0)
180 void CFeatureExtraction::internal_computeBLDLineDescriptors(
183 #if (!HAVE_OPENCV_WITH_LSD) 185 "This function requires OpenCV modules: xfeatures2d, line_descriptor");
189 if (in_features.
empty())
return;
192 const Mat
img = cvarrToMat(img_grayscale.
getAs<IplImage>());
194 vector<KeyPoint> cv_feats;
199 BinaryDescriptor::Params
params;
200 params.ksize_ = options.BLDOptions.ksize_;
201 params.reductionRatio = options.BLDOptions.reductionRatio;
202 params.numOfOctave_ = options.BLDOptions.numOfOctave;
203 params.widthOfBand_ = options.BLDOptions.widthOfBand;
205 Ptr<BinaryDescriptor> bd2 =
206 BinaryDescriptor::createBinaryDescriptor(
params);
208 std::vector<KeyLine> keylines;
210 bd2->detect(
img, keylines,
mask);
213 bd2->compute(
img, keylines, cv_descs);
214 keylines.resize(in_features.
size());
221 for (i = 0, itList = in_features.
begin(); itList != in_features.
end();
227 ft->descriptors.BLD.resize(cv_descs.cols);
228 for (
int m = 0; m < cv_descs.cols; ++m)
229 ft->descriptors.BLD[m] =
230 cv_descs.at<
int>(i, m);
233 #endif // end of opencv3 version check
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.
uint64_t TFeatureID
Definition of a feature ID.
TInternalFeatList::iterator iterator
LSD detector, OpenCV's implementation.
#define THROW_EXCEPTION(msg)
size_t getHeight() const override
Returns the height of the image in pixels.
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.
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.
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)...
EIGEN_STRONG_INLINE void ones(const size_t row, const size_t col)
Resize matrix and set all elements to one.
GLenum const GLfloat * params
void push_back(const CFeature::Ptr &f)
A class for storing images as grayscale or RGB bitmaps.
int round(const T value)
Returns the closer integer (int) to x.