15 #include <mrpt/otherlibs/do_opencv_includes.h> 28 unsigned int nDesiredFeatures,
const TImageROI& ROI)
const 38 const unsigned int MAX_COUNT = 300;
49 const cv::Mat
img(cv::cvarrToMat(inImg.
getAs<IplImage>()));
52 cout <<
"[KLT] Attach: " << tictac.
Tac() * 1000.0f << endl;
55 const cv::Mat cGrey(cv::cvarrToMat(inImg_gray.
getAs<IplImage>()));
57 nDesiredFeatures <= 0 ? nPts = MAX_COUNT : nPts = nDesiredFeatures;
64 cout <<
"[KLT] Create: " << tictac.
Tac() * 1000.0f << endl;
71 const bool use_harris = (options.featsType ==
featHarris);
76 std::vector<cv::Point2f>
points;
77 cv::goodFeaturesToTrack(
79 (
double)options.harrisOptions.threshold,
82 (
double)options.harrisOptions
87 options.harrisOptions.k);
89 cout <<
"[KLT] Find feats: " << tictac.
Tac() * 1000.0f << endl;
92 if (nDesiredFeatures > 0 &&
count < nPts)
93 cout <<
"\n[WARNING][selectGoodFeaturesKLT]: Only " <<
count <<
" of " 94 << nDesiredFeatures <<
" points could be extracted in the image." 97 if (options.FIND_SUBPIXEL)
104 cGrey,
points, cv::Size(3, 3), cv::Size(-1, -1),
105 cv::TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.05));
107 #ifdef VERBOSE_TIMING 108 cout <<
"[KLT] subpixel: " << tictac.
Tac() * 1000.0f << endl;
115 #ifdef VERBOSE_TIMING 120 unsigned int borderFeats = 0;
121 unsigned int nCFeats = init_ID;
124 int offset = (int)this->options.patchSize / 2 + 1;
126 unsigned int imgW = inImg.
getWidth();
130 const int xBorderInf = (int)floor(
points[i].
x - options.patchSize / 2);
131 const int xBorderSup = (int)floor(
points[i].
x + options.patchSize / 2);
132 const int yBorderInf = (int)floor(
points[i].
y - options.patchSize / 2);
133 const int yBorderSup = (int)floor(
points[i].
y + options.patchSize / 2);
135 if (options.patchSize == 0 ||
136 ((xBorderSup < (
int)imgW) && (xBorderInf > 0) &&
137 (yBorderSup < (int)imgH) && (yBorderInf > 0)))
148 ft->patchSize = options.patchSize;
150 if (options.patchSize > 0)
167 #ifdef VERBOSE_TIMING 168 cout <<
"[KLT] Create output: " << tictac.
Tac() * 1000.0f << endl;
172 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
179 MRPT_TODO(
"Delete? Refactor / join to mrpt::vision::CGenericFeatureTracker?")
187 unsigned int nDesiredFeats)
const 201 for( itKLT = inList.
begin(); itKLT != inList.
end(); itKLT++ )
203 int cx = (int)(*itKLT)->x;
204 int cy = (
int)(*itKLT)->y;
207 size_t xxI = max( 0, cx - 15 );
208 size_t xxE =
min( cx + 15,
mask.cols()-1 );
209 size_t yyI = max( 0, cy - 15 );
210 size_t yyE =
min( cy + 15, (
int)
mask.rows()-1 );
212 for(
size_t yy = yyI; yy < yyE; yy++)
213 for(
size_t xx = xxI; xx < xxE; xx++)
214 cvSet2D(
mask, yy, xx, zero );
216 if( (*itKLT)->ID > mxID )
220 selectGoodFeaturesKLT(
img, outList, mxID + 1, nDesiredFeats, &
mask );
222 else if( options.featsType ==
featFAST )
225 extractFeaturesFAST(
img, outList, mxID+1 );
230 for( itInList = inList.
begin(); itInList != inList.
end(); ++itInList )
232 for( itOutList = outList.
begin(); itOutList != outList.
end(); )
234 if( fabs((*itOutList)->x-(*itInList)->x) < 15 || fabs((*itOutList)->y-(*itInList)->y) < 15 )
235 itOutList = outList.
erase( itOutList );
241 if( nDesiredFeats != 0 && outList.
size() > nDesiredFeats )
242 outList.
resize( nDesiredFeats );
251 MRPT_TODO(
"Delete? Is not this a duplicate of extractFeaturesKLT ()???")
256 void CFeatureExtraction::selectGoodFeaturesKLT(
259 unsigned int init_ID,
260 unsigned int nDesiredFeatures)
const 264 size_t imgW =
img.getWidth();
265 size_t imgH =
img.getHeight();
268 ASSERT_( imgH > 0 && imgW > 0 );
269 ASSERT_( ROI.xMin >= 0 && ROI.yMin >= 0 && ROI.xMax < imgW && ROI.yMax < imgH );
272 CvMatrix
mask( imgH, imgW, CV_8UC1 );
273 CvScalar zero = cvRealScalar( 0.0 );
274 CvScalar one = cvRealScalar( 1.0 );
276 if( ROI.xMin == 0 && ROI.xMax == 0 && ROI.yMin == 0 && ROI.yMax == 0 )
278 if( options.patchSize == 0 )
279 selectGoodFeaturesKLT(
img, feats, init_ID, nDesiredFeatures,
nullptr );
298 size_t border = (options.patchSize-1)/2 + 1;
300 for( xx = 0; xx < imgW; xx++ )
301 for( yy = 0; yy <
border; yy++ )
302 cvSet2D(
mask, yy, xx, zero );
304 for( xx = 0; xx < imgW; xx++ )
305 for( yy = imgH -
border; yy < imgH; yy++ )
306 cvSet2D(
mask, yy, xx, zero );
308 for( xx = 0; xx <
border; xx++ )
310 cvSet2D(
mask, yy, xx, zero ) ;
312 for( xx = imgW -
border; xx < imgW; xx++ )
314 cvSet2D(
mask, yy, xx, zero ) ;
316 selectGoodFeaturesKLT(
img, feats, init_ID, nDesiredFeatures, &
mask );
322 for(
unsigned int xx = ROI.xMin; xx < ROI.xMax; xx++ )
323 for(
unsigned int yy = ROI.yMin; yy < ROI.yMax; yy++ )
324 cvSet2D(
mask, yy, xx, one) ;
326 selectGoodFeaturesKLT(
img, feats, init_ID, nDesiredFeatures, &
mask );
std::shared_ptr< CFeature > Ptr
GLuint GLuint GLsizei count
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
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)
TInternalFeatList::const_iterator const_iterator
Feature correctly tracked.
void Tic()
Starts the stopwatch.
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.
GLsizei const GLfloat * points
FAST feature detector, OpenCV's implementation ("Faster and better: A machine learning approac...
A structure for defining a ROI within an image.
GLint GLint GLsizei GLsizei GLsizei GLint border
size_t getWidth() const override
Returns the width of the image in pixels.
Harris border and corner detector [HARRIS].
This class implements a high-performance stopwatch.
size_t getHeight() const override
Returns the height of the image in pixels.
Classes for computer vision, detectors, features, etc.
iterator erase(const iterator &it)
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)...
double Tac()
Stops the stopwatch.
TFeatureID getMaxID() const
Get the maximum ID into the list.
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.
Kanade-Lucas-Tomasi feature [SHI'94].
void push_back(const CFeature::Ptr &f)