15 #include <mrpt/otherlibs/do_opencv_includes.h> 27 void CFeatureExtraction::extractFeaturesKLT(
29 unsigned int nDesiredFeatures,
const TImageROI& ROI)
const 39 const unsigned int MAX_COUNT = 300;
50 const cv::Mat
img(cv::cvarrToMat(inImg.
getAs<IplImage>()));
53 cout <<
"[KLT] Attach: " << tictac.
Tac() * 1000.0f << endl;
56 const cv::Mat cGrey(cv::cvarrToMat(inImg_gray.
getAs<IplImage>()));
58 nDesiredFeatures <= 0 ? nPts = MAX_COUNT : nPts = nDesiredFeatures;
65 cout <<
"[KLT] Create: " << tictac.
Tac() * 1000.0f << endl;
72 const bool use_harris = (options.featsType ==
featHarris);
77 std::vector<cv::Point2f>
points;
78 cv::goodFeaturesToTrack(
80 (
double)options.harrisOptions.threshold,
83 (
double)options.harrisOptions
88 options.harrisOptions.k);
90 cout <<
"[KLT] Find feats: " << tictac.
Tac() * 1000.0f << endl;
93 if (nDesiredFeatures > 0 &&
count < nPts)
94 cout <<
"\n[WARNING][selectGoodFeaturesKLT]: Only " <<
count <<
" of " 95 << nDesiredFeatures <<
" points could be extracted in the image." 98 if (options.FIND_SUBPIXEL)
100 #ifdef VERBOSE_TIMING 105 cGrey,
points, cv::Size(3, 3), cv::Size(-1, -1),
106 cv::TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.05));
108 #ifdef VERBOSE_TIMING 109 cout <<
"[KLT] subpixel: " << tictac.
Tac() * 1000.0f << endl;
116 #ifdef VERBOSE_TIMING 121 unsigned int borderFeats = 0;
122 unsigned int nCFeats = init_ID;
125 int offset = (int)this->options.patchSize / 2 + 1;
127 unsigned int imgW = inImg.
getWidth();
131 const int xBorderInf = (int)floor(
points[i].
x - options.patchSize / 2);
132 const int xBorderSup = (int)floor(
points[i].
x + options.patchSize / 2);
133 const int yBorderInf = (int)floor(
points[i].
y - options.patchSize / 2);
134 const int yBorderSup = (int)floor(
points[i].
y + options.patchSize / 2);
136 if (options.patchSize == 0 ||
137 ((xBorderSup < (
int)imgW) && (xBorderInf > 0) &&
138 (yBorderSup < (int)imgH) && (yBorderInf > 0)))
149 ft->patchSize = options.patchSize;
151 if (options.patchSize > 0)
168 #ifdef VERBOSE_TIMING 169 cout <<
"[KLT] Create output: " << tictac.
Tac() * 1000.0f << endl;
173 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
180 MRPT_TODO(
"Delete? Refactor / join to mrpt::vision::CGenericFeatureTracker?")
188 unsigned int nDesiredFeats)
const 202 for( itKLT = inList.
begin(); itKLT != inList.
end(); itKLT++ )
204 int cx = (int)(*itKLT)->x;
205 int cy = (
int)(*itKLT)->y;
208 size_t xxI = max( 0, cx - 15 );
209 size_t xxE =
min( cx + 15,
mask.cols()-1 );
210 size_t yyI = max( 0, cy - 15 );
211 size_t yyE =
min( cy + 15, (
int)
mask.rows()-1 );
213 for(
size_t yy = yyI; yy < yyE; yy++)
214 for(
size_t xx = xxI; xx < xxE; xx++)
215 cvSet2D(
mask, yy, xx, zero );
217 if( (*itKLT)->ID > mxID )
221 selectGoodFeaturesKLT(
img, outList, mxID + 1, nDesiredFeats, &
mask );
223 else if( options.featsType ==
featFAST )
226 extractFeaturesFAST(
img, outList, mxID+1 );
231 for( itInList = inList.
begin(); itInList != inList.
end(); ++itInList )
233 for( itOutList = outList.
begin(); itOutList != outList.
end(); )
235 if( fabs((*itOutList)->x-(*itInList)->x) < 15 || fabs((*itOutList)->y-(*itInList)->y) < 15 )
236 itOutList = outList.
erase( itOutList );
242 if( nDesiredFeats != 0 && outList.
size() > nDesiredFeats )
243 outList.
resize( nDesiredFeats );
252 MRPT_TODO(
"Delete? Is not this a duplicate of extractFeaturesKLT ()???")
257 void CFeatureExtraction::selectGoodFeaturesKLT(
260 unsigned int init_ID,
261 unsigned int nDesiredFeatures)
const 265 size_t imgW =
img.getWidth();
266 size_t imgH =
img.getHeight();
269 ASSERT_( imgH > 0 && imgW > 0 );
270 ASSERT_( ROI.xMin >= 0 && ROI.yMin >= 0 && ROI.xMax < imgW && ROI.yMax < imgH );
273 CvMatrix
mask( imgH, imgW, CV_8UC1 );
274 CvScalar zero = cvRealScalar( 0.0 );
275 CvScalar one = cvRealScalar( 1.0 );
277 if( ROI.xMin == 0 && ROI.xMax == 0 && ROI.yMin == 0 && ROI.yMax == 0 )
279 if( options.patchSize == 0 )
280 selectGoodFeaturesKLT(
img, feats, init_ID, nDesiredFeatures,
nullptr );
299 size_t border = (options.patchSize-1)/2 + 1;
301 for( xx = 0; xx < imgW; xx++ )
302 for( yy = 0; yy <
border; yy++ )
303 cvSet2D(
mask, yy, xx, zero );
305 for( xx = 0; xx < imgW; xx++ )
306 for( yy = imgH -
border; yy < imgH; yy++ )
307 cvSet2D(
mask, yy, xx, zero );
309 for( xx = 0; xx <
border; xx++ )
311 cvSet2D(
mask, yy, xx, zero ) ;
313 for( xx = imgW -
border; xx < imgW; xx++ )
315 cvSet2D(
mask, yy, xx, zero ) ;
317 selectGoodFeaturesKLT(
img, feats, init_ID, nDesiredFeatures, &
mask );
323 for(
unsigned int xx = ROI.xMin; xx < ROI.xMax; xx++ )
324 for(
unsigned int yy = ROI.yMin; yy < ROI.yMax; yy++ )
325 cvSet2D(
mask, yy, xx, one) ;
327 selectGoodFeaturesKLT(
img, feats, init_ID, nDesiredFeatures, &
mask );
double Tac() noexcept
Stops the stopwatch.
GLuint GLuint GLsizei count
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
#define THROW_EXCEPTION(msg)
A high-performance stopwatch, with typical resolution of nanoseconds.
size_t getHeight() const override
Returns the height of the image in pixels.
Feature correctly tracked.
GLsizei const GLfloat * points
FAST feature detector, OpenCV's implementation ("Faster and better: A machine learning approach to...
A structure for defining a ROI within an image.
GLint GLint GLsizei GLsizei GLsizei GLint border
#define ASSERT_(f)
Defines an assertion mechanism.
size_t getWidth() const override
Returns the width of the image in pixels.
Harris border and corner detector [HARRIS].
Classes for computer vision, detectors, features, etc.
iterator erase(const iterator &it)
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
TInternalFeatList::const_iterator const_iterator
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
TFeatureID getMaxID() const
Get the maximum ID into the list.
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)...
void Tic() noexcept
Starts the stopwatch.
EIGEN_STRONG_INLINE void ones(const size_t row, const size_t col)
Resize matrix and set all elements to one.
Kanade-Lucas-Tomasi feature [SHI'94].
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.