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;
36 #if defined(HAVE_OPENCV_XFEATURES2D) && defined(HAVE_OPENCV_LINE_DESCRIPTOR) 37 #define HAVE_OPENCV_WITH_LSD 1 39 #define HAVE_OPENCV_WITH_LSD 0 42 void CFeatureExtraction::extractFeaturesLSD(
44 unsigned int nDesiredFeatures,
const TImageROI& ROI)
const 47 #if (!HAVE_OPENCV_WITH_LSD) 49 "This function requires OpenCV modules: xfeatures2d, line_descriptor");
53 vector<KeyPoint> cv_feats;
54 vector<KeyLine> cv_line;
59 const Mat theImg = cvarrToMat(inImg_gray.
getAs<IplImage>());
63 Ptr<LSDDetector> bd = LSDDetector::createLSDDetector();
66 cv::Mat output = theImg.clone();
68 theImg, cv_line, options.LSDOptions.scale, options.LSDOptions.nOctaves,
72 const size_t N = cv_line.size();
77 for (
size_t i = 0; i < N; i++)
79 for (
size_t j = i + 1; j < N; j++)
81 if (cv_line.at(j).lineLength > cv_line.at(i).lineLength)
83 KeyLine temp_line = cv_line.at(i);
84 cv_line.at(i) = cv_line.at(j);
85 cv_line.at(j) = temp_line;
104 (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
105 const int offset = (int)this->options.patchSize / 2 + 1;
106 const size_t size_2 = options.patchSize / 2;
108 const size_t imgW = inImg.
getWidth();
110 unsigned int cont = 0;
113 if (!options.addNewFeatures) feats.
clear();
116 if (output.channels() == 1) cvtColor(output, output, COLOR_GRAY2BGR);
118 while (cont != nMax && i != N)
120 KeyLine kl = cv_line[i];
125 Point pt1 = Point2f(kl.startPointX, kl.startPointY);
126 Point pt2 = Point2f(kl.endPointX, kl.endPointY);
128 kp.pt.x = (pt1.x + pt2.x) / 2;
129 kp.pt.y = (pt1.y + pt2.y) / 2;
132 const int xBorderInf = (int)floor(kp.pt.x - size_2);
133 const int xBorderSup = (int)floor(kp.pt.x + size_2);
134 const int yBorderInf = (int)floor(kp.pt.y - size_2);
135 const int yBorderSup = (int)floor(kp.pt.y + size_2);
137 if (!(xBorderSup < (
int)imgW && xBorderInf > 0 &&
138 yBorderSup < (int)imgH && yBorderInf > 0))
151 ft->response = kl.response;
153 ft->scale = kl.octave;
159 if (options.patchSize > 0)
179 void CFeatureExtraction::internal_computeBLDLineDescriptors(
182 #if (!HAVE_OPENCV_WITH_LSD) 184 "This function requires OpenCV modules: xfeatures2d, line_descriptor");
188 if (in_features.
empty())
return;
191 const Mat
img = cvarrToMat(img_grayscale.
getAs<IplImage>());
193 vector<KeyPoint> cv_feats;
198 BinaryDescriptor::Params
params;
199 params.ksize_ = options.BLDOptions.ksize_;
200 params.reductionRatio = options.BLDOptions.reductionRatio;
201 params.numOfOctave_ = options.BLDOptions.numOfOctave;
202 params.widthOfBand_ = options.BLDOptions.widthOfBand;
204 Ptr<BinaryDescriptor> bd2 =
205 BinaryDescriptor::createBinaryDescriptor(
params);
207 std::vector<KeyLine> keylines;
209 bd2->detect(
img, keylines,
mask);
212 bd2->compute(
img, keylines, cv_descs);
213 keylines.resize(in_features.
size());
220 for (i = 0, itList = in_features.
begin(); itList != in_features.
end();
226 ft->descriptors.BLD.resize(cv_descs.cols);
227 for (
int m = 0; m < cv_descs.cols; ++m)
228 ft->descriptors.BLD[m] =
229 cv_descs.at<
int>(i, m);
232 #endif // end of opencv3 version check std::shared_ptr< CFeature > Ptr
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
LSD detector, OpenCV's implementation.
A class for storing images as grayscale or RGB bitmaps.
#define THROW_EXCEPTION(msg)
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.
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.
size_t getHeight() const override
Returns the height of the image in pixels.
Classes for computer vision, detectors, features, etc.
uint64_t TFeatureID
Definition of a feature ID.
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)...
int round(const T value)
Returns the closer integer (int) to x.
TInternalFeatList::iterator iterator
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)