]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/commitdiff
scale subgrid computed only from neigh. scales -> more robust
authorTomas Vojir <vojirtom@cmp.felk.cvut.cz>
Mon, 8 Aug 2016 13:30:43 +0000 (15:30 +0200)
committerTomas Vojir <vojirtom@cmp.felk.cvut.cz>
Mon, 8 Aug 2016 13:30:43 +0000 (15:30 +0200)
src/kcf.cpp
src/kcf.h

index 37837be69ea2e30a11f5e8d9a1e5f2a68e8ba4e4..19105fe74b41cbd2e0f612c364d6b276977bd4cd 100644 (file)
@@ -182,7 +182,7 @@ void KCF_Tracker::track(cv::Mat &img)
     //sub grid scale interpolation
     double new_scale = p_scales[scale_index];
     if (m_use_subgrid_scale)
-        new_scale = sub_grid_scale(scale_responses);
+        new_scale = sub_grid_scale(scale_responses, scale_index);
 
     p_current_scale *= new_scale;
 
@@ -556,22 +556,34 @@ cv::Point2f KCF_Tracker::sub_pixel_peak(cv::Point & max_loc, cv::Mat & response)
     return sub_peak;
 }
 
-double KCF_Tracker::sub_grid_scale(std::vector<double> & responses)
+double KCF_Tracker::sub_grid_scale(std::vector<double> & responses, int index)
 {
-    // interpolate from all values
-    // fit 1d quadratic function f(x) = a*x^2 + b*x + c
-    cv::Mat A (p_scales.size(), 3, CV_32FC1);
-    cv::Mat fval (p_scales.size(), 1, CV_32FC1);
-    for (size_t i = 0; i < p_scales.size(); ++i) {
-        A.at<float>(i, 0) = p_scales[i]*p_scales[i];
-        A.at<float>(i, 1) = p_scales[i];
-        A.at<float>(i, 2) = 1;
-        fval.at<float>(i) = responses[i];
+    cv::Mat A, fval;
+    if (index < 0 || index > (int)p_scales.size()-1) {
+        // interpolate from all values
+        // fit 1d quadratic function f(x) = a*x^2 + b*x + c
+        A.create(p_scales.size(), 3, CV_32FC1);
+        fval.create(p_scales.size(), 1, CV_32FC1);
+        for (size_t i = 0; i < p_scales.size(); ++i) {
+            A.at<float>(i, 0) = p_scales[i] * p_scales[i];
+            A.at<float>(i, 1) = p_scales[i];
+            A.at<float>(i, 2) = 1;
+            fval.at<float>(i) = responses[i];
+        }
+    } else {
+        //only from neighbours
+        if (index == 0 || index == (int)p_scales.size()-1)
+            return p_scales[index];
+
+        A = (cv::Mat_<float>(3, 3) <<
+                       p_scales[index-1] * p_scales[index-1], p_scales[index-1], 1,
+                       p_scales[index] * p_scales[index], p_scales[index], 1,
+                       p_scales[index+1] * p_scales[index+1], p_scales[index+1], 1);
+        fval = (cv::Mat_<float>(3, 1) << responses[index-1], responses[index], responses[index+1]);
     }
 
     cv::Mat x;
     cv::solve(A, fval, x, cv::DECOMP_SVD);
-
     float a = x.at<float>(0), b = x.at<float>(1);
-    return -b/(2*a);
+    return -b / (2 * a);
 }
\ No newline at end of file
index 9f0f9f9f3eb11729f7042c44d5e67a86fd28a8c2..39d31271fad1725026d1d7a8f42e7d618f467a38 100644 (file)
--- a/src/kcf.h
+++ b/src/kcf.h
@@ -94,7 +94,7 @@ private:
     cv::Mat ifft2(const ComplexMat & inputf);
     std::vector<cv::Mat> get_features(cv::Mat & input_rgb, cv::Mat & input_gray, int cx, int cy, int size_x, int size_y, double scale = 1.);
     cv::Point2f sub_pixel_peak(cv::Point & max_loc, cv::Mat & response);
-    double sub_grid_scale(std::vector<double> & responses);
+    double sub_grid_scale(std::vector<double> & responses, int index = -1);
 
 };