17 #include <mrpt/otherlibs/do_opencv_includes.h>
37 template <
typename FEATLIST>
41 FEATLIST &featureList )
46 const unsigned int window_width = extra_params.getWithDefaultVal(
"window_width",15);
47 const unsigned int window_height = extra_params.getWithDefaultVal(
"window_height",15);
49 const int LK_levels = extra_params.getWithDefaultVal(
"LK_levels",3);
50 const int LK_max_iters = extra_params.getWithDefaultVal(
"LK_max_iters",10);
51 const int LK_epsilon = extra_params.getWithDefaultVal(
"LK_epsilon",0.1);
52 const float LK_max_tracking_error = extra_params.getWithDefaultVal(
"LK_max_tracking_error",150.0f);
58 const size_t img_width = old_img.
getWidth();
59 const size_t img_height = old_img.
getHeight();
61 const size_t nFeatures = featureList.size();
72 points[0] =
reinterpret_cast<CvPoint2D32f *
>(
mrpt_alloca(
sizeof(CvPoint2D32f)*nFeatures) );
73 points[1] =
reinterpret_cast<CvPoint2D32f *
>(
mrpt_alloca(
sizeof(CvPoint2D32f)*nFeatures) );
75 std::vector<char>
status(nFeatures);
77 for(
size_t i=0;i<nFeatures;++i)
79 points[0][i].x = featureList.getFeatureX(i);
80 points[0][i].y = featureList.getFeatureY(i);
84 const IplImage *prev_gray_ipl = prev_gray.
getAs<IplImage>();
85 const IplImage *cur_gray_ipl = cur_gray.
getAs<IplImage>();
89 IplImage* pPyr = NULL;
90 IplImage* cPyr = NULL;
94 float* track_error =
reinterpret_cast<float*
>(
mrpt_alloca(
sizeof(
float)*nFeatures) );
96 cvCalcOpticalFlowPyrLK(prev_gray_ipl, cur_gray_ipl, pPyr, cPyr,
97 &
points[0][0], &
points[1][0], nFeatures, cvSize( window_width, window_height ), LK_levels, &
status[0], track_error,
98 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,LK_max_iters,LK_epsilon), flags );
100 cvReleaseImage( &pPyr );
101 cvReleaseImage( &cPyr );
103 for(
size_t i=0;i<nFeatures;++i)
105 const bool trck_err_too_large = track_error[i]>LK_max_tracking_error;
108 !trck_err_too_large &&
113 featureList.setFeatureXf(i,
points[1][i].
x );
114 featureList.setFeatureYf(i,
points[1][i].
y );
119 featureList.setFeatureX(i,-1);
120 featureList.setFeatureY(i,-1);
131 featureList.mark_as_outdated();
135 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
147 trackFeatures_impl_templ<CFeatureList>(old_img,new_img,featureList);
155 trackFeatures_impl_templ<TSimpleFeatureList>(old_img,new_img,featureList);
163 trackFeatures_impl_templ<TSimpleFeaturefList>(old_img,new_img,featureList);
A class for storing images as grayscale or RGB bitmaps.
size_t getHeight() const MRPT_OVERRIDE
Returns the height of the image in pixels.
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img....
size_t getWidth() const MRPT_OVERRIDE
Returns the width of the image in pixels.
A list of visual features, to be used as output by detectors, as input/output by trackers,...
GLsizei const GLfloat * points
#define mrpt_alloca_free(mem_block)
This method must be called to "free" each memory block allocated with "system::alloca": If the block ...
#define mrpt_alloca(nBytes)
In platforms and compilers with support to "alloca", allocate a memory block on the stack; if alloca ...
@ status_TRACKED
Feature correctly tracked.
@ status_OOB
Feature fell Out Of Bounds (out of the image limits, too close to image borders)
@ status_LOST
Unable to track this feature (mismatch is too high for the given tracking window: lack of texture?...
#define THROW_EXCEPTION(msg)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
@ FAST_REF_OR_CONVERT_TO_GRAY
Classes for computer vision, detectors, features, etc.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void trackFeatures_impl_templ(const mrpt::utils::CImage &old_img, const mrpt::utils::CImage &new_img, FEATLIST &inout_featureList)
Track a set of features from old_img -> new_img using sparse optimal flow (classic KL method) Optiona...
virtual void trackFeatures_impl(const mrpt::utils::CImage &old_img, const mrpt::utils::CImage &new_img, vision::CFeatureList &inout_featureList) MRPT_OVERRIDE
This version falls back to the version with TSimpleFeatureList if the derived class does not implemen...