17 #include <mrpt/otherlibs/do_opencv_includes.h>
36 template <
typename FEATLIST>
38 const CImage& old_img,
const CImage& new_img, FEATLIST& featureList)
43 const unsigned int window_width =
44 extra_params.getWithDefaultVal(
"window_width", 15);
45 const unsigned int window_height =
46 extra_params.getWithDefaultVal(
"window_height", 15);
48 const int LK_levels = extra_params.getWithDefaultVal(
"LK_levels", 3);
49 const int LK_max_iters = extra_params.getWithDefaultVal(
"LK_max_iters", 10);
50 const int LK_epsilon = extra_params.getWithDefaultVal(
"LK_epsilon", 0.1);
51 const float LK_max_tracking_error =
52 extra_params.getWithDefaultVal(
"LK_max_tracking_error", 150.0f);
59 const size_t img_width = old_img.
getWidth();
60 const size_t img_height = old_img.
getHeight();
62 const size_t nFeatures = featureList.size();
73 points[0] =
reinterpret_cast<CvPoint2D32f*
>(
75 points[1] =
reinterpret_cast<CvPoint2D32f*
>(
78 std::vector<char>
status(nFeatures);
80 for (
size_t i = 0; i < nFeatures; ++i)
82 points[0][i].x = featureList.getFeatureX(i);
83 points[0][i].y = featureList.getFeatureY(i);
87 const IplImage* prev_gray_ipl = prev_gray.
getAs<IplImage>();
88 const IplImage* cur_gray_ipl = cur_gray.
getAs<IplImage>();
93 IplImage* pPyr =
nullptr;
94 IplImage* cPyr =
nullptr;
99 reinterpret_cast<float*
>(
mrpt_alloca(
sizeof(
float) * nFeatures));
101 cvCalcOpticalFlowPyrLK(
102 prev_gray_ipl, cur_gray_ipl, pPyr, cPyr, &
points[0][0],
103 &
points[1][0], nFeatures, cvSize(window_width, window_height),
104 LK_levels, &
status[0], track_error,
106 CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, LK_max_iters, LK_epsilon),
109 cvReleaseImage(&pPyr);
110 cvReleaseImage(&cPyr);
112 for (
size_t i = 0; i < nFeatures; ++i)
114 const bool trck_err_too_large =
115 track_error[i] > LK_max_tracking_error;
117 if (
status[i] == 1 && !trck_err_too_large &&
points[1][i].
x > 0 &&
122 featureList.setFeatureXf(i,
points[1][i].
x);
123 featureList.setFeatureYf(i,
points[1][i].
y);
128 featureList.setFeatureX(i, -1);
129 featureList.setFeatureY(i, -1);
130 featureList.setTrackStatus(
140 featureList.mark_as_outdated();
144 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
153 trackFeatures_impl_templ<CFeatureList>(old_img, new_img, featureList);
160 trackFeatures_impl_templ<TSimpleFeatureList>(old_img, new_img, featureList);
167 trackFeatures_impl_templ<TSimpleFeaturefList>(
168 old_img, new_img, featureList);