]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blobdiff - src/kcf.cpp
Simplify scale initialization
[hercules2020/kcf.git] / src / kcf.cpp
index 82f1715acefb516593ca78fe0096d9c0c1a905c7..eb8d4c0a0c32007d2e3a4ab68b11b08803cb1bc2 100644 (file)
@@ -80,10 +80,9 @@ void KCF_Tracker::train(cv::Mat input_rgb, cv::Mat input_gray, double interp_fac
         alphaf_den = (p_xf * xfconj);
     } else {
         // Kernel Ridge Regression, calculate alphas (in Fourier domain)
-        const uint num_scales = BIG_BATCH_MODE ? p_num_scales : 1;
         cv::Size sz(Fft::freq_size(p_roi));
-        ComplexMat kf(sz.height, sz.width, num_scales);
-        (*gaussian_correlation)(*this, kf, p_model_xf, p_model_xf, p_kernel_sigma, true);
+        ComplexMat kf(sz.height, sz.width, 1);
+        (*gaussian_correlation)(kf, p_model_xf, p_model_xf, p_kernel_sigma, true, *this);
         DEBUG_PRINTM(kf);
         p_model_alphaf_num = p_yf * kf;
         p_model_alphaf_den = kf * (kf + p_lambda);
@@ -177,11 +176,8 @@ void KCF_Tracker::init(cv::Mat &img, const cv::Rect &bbox, int fit_size_x, int f
     p_roi.height = p_windows_size.height / p_cell_size;
 
     p_scales.clear();
-    if (m_use_scale)
-        for (int i = -int(p_num_scales) / 2; i <= int(p_num_scales) / 2; ++i)
-            p_scales.push_back(std::pow(p_scale_step, i));
-    else
-        p_scales.push_back(1.);
+    for (int i = -int(p_num_scales) / 2; i <= int(p_num_scales) / 2; ++i)
+        p_scales.push_back(std::pow(p_scale_step, i));
 
 #ifdef CUFFT
     if (p_roi.height * (p_roi.width / 2 + 1) > 1024) {
@@ -197,7 +193,6 @@ void KCF_Tracker::init(cv::Mat &img, const cv::Rect &bbox, int fit_size_x, int f
         std::cerr << "cuFFT supports only Gaussian kernel." << std::endl;
         std::exit(EXIT_FAILURE);
     }
-    CudaSafeCall(cudaSetDeviceFlags(cudaDeviceMapHost));
 #else
     p_xf.create(p_roi.height, p_roi.height / 2 + 1, p_num_of_feats);
 #endif
@@ -218,8 +213,7 @@ void KCF_Tracker::init(cv::Mat &img, const cv::Rect &bbox, int fit_size_x, int f
     d.threadctxs.emplace_back(p_roi, p_num_of_feats, p_num_scales);
 #endif
 
-    gaussian_correlation.reset(
-                new GaussianCorrelation(IF_BIG_BATCH(p_num_scales, 1), p_roi));
+    gaussian_correlation.reset(new GaussianCorrelation(1, p_roi));
 
     p_current_scale = 1.;
 
@@ -311,7 +305,7 @@ void KCF_Tracker::resizeImgs(cv::Mat &input_rgb, cv::Mat &input_gray)
     }
 }
 
-void KCF_Tracker::findMaxReponse(uint &max_idx, cv::Point2f &new_location) const
+double KCF_Tracker::findMaxReponse(uint &max_idx, cv::Point2f &new_location) const
 {
     double max = -1.;
 #ifndef BIG_BATCH
@@ -349,6 +343,7 @@ void KCF_Tracker::findMaxReponse(uint &max_idx, cv::Point2f &new_location) const
         new_location = max_response_pt;
     }
     DEBUG_PRINT(new_location);
+    return max;
 }
 
 void KCF_Tracker::track(cv::Mat &img)
@@ -383,7 +378,7 @@ void KCF_Tracker::track(cv::Mat &img)
 
     cv::Point2f new_location;
     uint max_idx;
-    findMaxReponse(max_idx, new_location);
+    max_response = findMaxReponse(max_idx, new_location);
 
     p_pose.cx += p_current_scale * p_cell_size * double(new_location.x);
     p_pose.cy += p_current_scale * p_cell_size * double(new_location.y);
@@ -428,7 +423,7 @@ void ThreadCtx::track(const KCF_Tracker &kcf, cv::Mat &input_rgb, cv::Mat &input
     if (kcf.m_use_linearkernel) {
         kzf = zf.mul(kcf.p_model_alphaf).sum_over_channels();
     } else {
-        gaussian_correlation(kcf, kzf, zf, kcf.p_model_xf, kcf.p_kernel_sigma);
+        gaussian_correlation(kzf, zf, kcf.p_model_xf, kcf.p_kernel_sigma, false, kcf);
         DEBUG_PRINTM(kzf);
         kzf = kzf.mul(kcf.p_model_alphaf);
     }
@@ -682,8 +677,8 @@ cv::Mat KCF_Tracker::get_subwindow(const cv::Mat &input, int cx, int cy, int wid
     return patch;
 }
 
-void KCF_Tracker::GaussianCorrelation::operator()(const KCF_Tracker &kcf, ComplexMat &result, const ComplexMat &xf,
-                                                  const ComplexMat &yf, double sigma, bool auto_correlation)
+void KCF_Tracker::GaussianCorrelation::operator()(ComplexMat &result, const ComplexMat &xf, const ComplexMat &yf,
+                                                  double sigma, bool auto_correlation, const KCF_Tracker &kcf)
 {
     TRACE("");
     xf.sqr_norm(xf_sqr_norm);
@@ -710,6 +705,7 @@ void KCF_Tracker::GaussianCorrelation::operator()(const KCF_Tracker &kcf, Comple
     float numel_xf_inv = 1.f / (xf.cols * xf.rows * (xf.channels() / xf.n_scales));
     for (uint i = 0; i < xf.n_scales; ++i) {
         cv::Mat plane = ifft_res.plane(i);
+        DEBUG_PRINT(ifft_res.plane(i));
         cv::exp(-1. / (sigma * sigma) * cv::max((xf_sqr_norm[i] + yf_sqr_norm[0] - 2 * ifft_res.plane(i))
                 * numel_xf_inv, 0), plane);
         DEBUG_PRINTM(plane);