15 #include <mrpt/otherlibs/do_opencv_includes.h> 30 unsigned int nDesiredFeatures,
const TImageROI& ROI,
37 #if MRPT_OPENCV_VERSION_NUM < 0x210 43 vector<KeyPoint> cv_feats;
53 #if MRPT_OPENCV_VERSION_NUM >= 0x211 59 const Mat theImg = cvarrToMat(inImg_gray.
getAs<IplImage>());
64 cout <<
"using mask" << endl;
65 size_t maskW =
mask->getColCount(), maskH =
mask->getRowCount();
71 for (
int ii = 0; ii < int(maskW); ++ii)
72 for (
int jj = 0; jj < int(maskH); ++jj)
74 if (!
mask->get_unsafe(jj, ii))
76 cvMask.at<
char>(ii, jj) = (
char)0;
81 #if MRPT_OPENCV_VERSION_NUM < 0x300 82 FastFeatureDetector fastDetector(
83 options.FASTOptions.threshold, options.FASTOptions.nonmax_suppression);
84 fastDetector.detect(theImg, cv_feats);
86 Ptr<cv::FastFeatureDetector> fastDetector = cv::FastFeatureDetector::create(
87 options.FASTOptions.threshold, options.FASTOptions.nonmax_suppression);
88 fastDetector->detect(theImg, cv_feats);
91 #elif MRPT_OPENCV_VERSION_NUM >= 0x210 93 inImg_gray.
getAs<IplImage>(), cv_feats, options.FASTOptions.threshold,
94 options.FASTOptions.nonmax_suppression);
98 const size_t N = cv_feats.size();
101 if (options.FASTOptions.use_KLT_response)
103 const unsigned int KLT_half_win = 4;
104 const unsigned int max_x = inImg_gray.
getWidth() - 1 - KLT_half_win;
105 const unsigned int max_y = inImg_gray.
getHeight() - 1 - KLT_half_win;
106 for (
size_t i = 0; i < N; i++)
108 const unsigned int x = cv_feats[i].pt.x;
109 const unsigned int y = cv_feats[i].pt.y;
110 if (
x > KLT_half_win &&
y > KLT_half_win &&
x <= max_x &&
112 cv_feats[i].response =
115 cv_feats[i].response = -100;
123 std::vector<size_t> sorted_indices(N);
124 for (
size_t i = 0; i < N; i++) sorted_indices[i] = i;
126 sorted_indices.begin(), sorted_indices.end(),
141 const bool do_filter_min_dist = options.FASTOptions.min_distance > 1;
145 const unsigned int occupied_grid_cell_size =
146 options.FASTOptions.min_distance / 2.0;
147 const float occupied_grid_cell_size_inv = 1.0f / occupied_grid_cell_size;
149 unsigned int grid_lx =
152 : (
unsigned int)(1 + inImg.
getWidth() * occupied_grid_cell_size_inv);
153 unsigned int grid_ly =
156 : (
unsigned int)(1 + inImg.
getHeight() * occupied_grid_cell_size_inv);
160 occupied_sections.
fillAll(
false);
163 (nDesiredFeatures != 0 && N > nDesiredFeatures) ? nDesiredFeatures : N;
164 const int offset = (int)this->options.patchSize / 2 + 1;
165 const size_t size_2 = options.patchSize / 2;
167 const size_t imgW = inImg.
getWidth();
169 unsigned int cont = 0;
172 if (!options.addNewFeatures) feats.
clear();
174 while (cont != nMax && i != N)
177 const KeyPoint& kp = cv_feats[sorted_indices[i]];
181 const int xBorderInf = (int)floor(kp.pt.x - size_2);
182 const int xBorderSup = (int)floor(kp.pt.x + size_2);
183 const int yBorderInf = (int)floor(kp.pt.y - size_2);
184 const int yBorderSup = (int)floor(kp.pt.y + size_2);
186 if (!(xBorderSup < (
int)imgW && xBorderInf > 0 &&
187 yBorderSup < (int)imgH && yBorderInf > 0))
190 if (do_filter_min_dist)
193 const size_t section_idx_x =
194 size_t(kp.pt.x * occupied_grid_cell_size_inv);
195 const size_t section_idx_y =
196 size_t(kp.pt.y * occupied_grid_cell_size_inv);
198 if (occupied_sections(section_idx_x, section_idx_y))
202 occupied_sections.
set_unsafe(section_idx_x, section_idx_y,
true);
203 if (section_idx_x > 0)
205 section_idx_x - 1, section_idx_y,
true);
206 if (section_idx_y > 0)
208 section_idx_x, section_idx_y - 1,
true);
209 if (section_idx_x < grid_lx - 1)
211 section_idx_x + 1, section_idx_y,
true);
212 if (section_idx_y < grid_ly - 1)
214 section_idx_x, section_idx_y + 1,
true);
223 ft->response = kp.response;
224 ft->orientation = kp.angle;
225 ft->scale = kp.octave;
226 ft->patchSize = options.patchSize;
228 if (options.patchSize > 0)
std::shared_ptr< CFeature > Ptr
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Declares a matrix of booleans (non serializable).
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
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.
FAST feature detector, OpenCV's implementation ("Faster and better: A machine learning approac...
A helper struct to sort keypoints by their response: It can be used with these types: ...
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.
float KLT_response(const unsigned int x, const unsigned int y, const unsigned int half_window_size) const
Compute the KLT response at a given pixel (x,y) - Only for grayscale images (for efficiency it avoids...
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
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.
void set_unsafe(size_t row, size_t col, const T &v)
Fast but unsafe method to write a value in the matrix.
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.
EIGEN_STRONG_INLINE void ones(const size_t row, const size_t col)
Resize matrix and set all elements to one.
void push_back(const CFeature::Ptr &f)
void fillAll(const T &val)