From 863408c50c11bb5dd5ab91e365c13c5ea818678b Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 19 Oct 2018 00:15:45 +0200 Subject: [PATCH] Fix numerical problems in sub_pixel_peak() Sometimes, finding sub-pixel maximum fails (solution is very far from the maximal value pixel) and the tracked object is lost. These changes fix some the these cases. This problem was present also in the original implementation. --- src/kcf.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/kcf.cpp b/src/kcf.cpp index 64baea7..4cccea5 100644 --- a/src/kcf.cpp +++ b/src/kcf.cpp @@ -833,9 +833,12 @@ cv::Point2f KCF_Tracker::sub_pixel_peak(cv::Point &max_loc, cv::Mat &response) c float a = x.at(0), b = x.at(1), c = x.at(2), d = x.at(3), e = x.at(4); cv::Point2f sub_peak(max_loc.x, max_loc.y); - if (b > 0 || b < 0) { + if (4 * a * c - b * b > p_floating_error) { sub_peak.y = ((2.f * a * e) / b - d) / (b - (4 * a * c) / b); sub_peak.x = (-2 * c * sub_peak.y - e) / b; + if (fabs(sub_peak.x - max_loc.x) > 1 || + fabs(sub_peak.y - max_loc.y) > 1) + sub_peak = max_loc; } return sub_peak; -- 2.39.2