]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blobdiff - src/kcf.cpp
Draw maximum (cross) in the middle of pixels, not at the edge
[hercules2020/kcf.git] / src / kcf.cpp
index 382dac377a6cbd8cb58211ba8df0b6304ceb7cc7..64baea78d4f3c8105459c9ec1c03dbe082631791 100644 (file)
 #include "kcf.h"
 #include <numeric>
 #include <thread>
-#include <future>
 #include <algorithm>
+#include "threadctx.hpp"
+#include "debug.h"
+#include <limits>
 
-void KCF_Tracker::init(cv::Mat &img, const cv::Rect & bbox)
+#ifdef FFTW
+#include "fft_fftw.h"
+#define FFT Fftw
+#elif defined(CUFFT)
+#include "fft_cufft.h"
+#define FFT cuFFT
+#else
+#include "fft_opencv.h"
+#define FFT FftOpencv
+#endif
+
+#ifdef OPENMP
+#include <omp.h>
+#endif // OPENMP
+
+DbgTracer __dbgTracer;
+
+template <typename T>
+T clamp(const T& n, const T& lower, const T& upper)
+{
+    return std::max(lower, std::min(n, upper));
+}
+
+template <typename T>
+void clamp2(T& n, const T& lower, const T& upper)
+{
+    n = std::max(lower, std::min(n, upper));
+}
+
+#if CV_MAJOR_VERSION < 3
+template<typename _Tp> static inline
+cv::Size_<_Tp> operator / (const cv::Size_<_Tp>& a, _Tp b)
+{
+    return cv::Size_<_Tp>(a.width / b, a.height / b);
+}
+
+template<typename _Tp> static inline
+cv::Point_<_Tp> operator / (const cv::Point_<_Tp>& a, double b)
+{
+    return cv::Point_<_Tp>(a.x / b, a.y / b);
+}
+
+#endif
+
+class Kcf_Tracker_Private {
+    friend KCF_Tracker;
+
+    Kcf_Tracker_Private(const KCF_Tracker &kcf) : kcf(kcf) {}
+
+    const KCF_Tracker &kcf;
+#ifdef BIG_BATCH
+    std::vector<ThreadCtx> threadctxs;
+#else
+    ScaleRotVector<ThreadCtx> threadctxs{kcf.p_scales, kcf.p_angles};
+#endif
+};
+
+KCF_Tracker::KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor,
+                         double output_sigma_factor, int cell_size)
+    : p_cell_size(cell_size), fft(*new FFT()), p_padding(padding), p_output_sigma_factor(output_sigma_factor), p_kernel_sigma(kernel_sigma),
+      p_lambda(lambda), p_interp_factor(interp_factor)
+{
+}
+
+KCF_Tracker::KCF_Tracker() : fft(*new FFT()) {}
+
+KCF_Tracker::~KCF_Tracker()
+{
+    delete &fft;
+}
+
+void KCF_Tracker::train(cv::Mat input_rgb, cv::Mat input_gray, double interp_factor)
+{
+    TRACE("");
+
+    // obtain a sub-window for training
+    get_features(input_rgb, input_gray, nullptr, p_current_center.x, p_current_center.y,
+                 p_windows_size.width, p_windows_size.height,
+                 p_current_scale, p_current_angle).copyTo(model->patch_feats.scale(0));
+    DEBUG_PRINT(model->patch_feats);
+    fft.forward_window(model->patch_feats, model->xf, model->temp);
+    DEBUG_PRINTM(model->xf);
+    model->model_xf = model->model_xf * (1. - interp_factor) + model->xf * interp_factor;
+    DEBUG_PRINTM(model->model_xf);
+
+    if (m_use_linearkernel) {
+        ComplexMat xfconj = model->xf.conj();
+        model->model_alphaf_num = xfconj.mul(model->yf);
+        model->model_alphaf_den = (model->xf * xfconj);
+    } else {
+        // Kernel Ridge Regression, calculate alphas (in Fourier domain)
+        cv::Size sz(Fft::freq_size(feature_size));
+        ComplexMat kf(sz.height, sz.width, 1);
+        (*gaussian_correlation)(kf, model->model_xf, model->model_xf, p_kernel_sigma, true, *this);
+        DEBUG_PRINTM(kf);
+        model->model_alphaf_num = model->yf * kf;
+        model->model_alphaf_den = kf * (kf + p_lambda);
+    }
+    model->model_alphaf = model->model_alphaf_num / model->model_alphaf_den;
+    DEBUG_PRINTM(model->model_alphaf);
+    //        p_model_alphaf = p_yf / (kf + p_lambda);   //equation for fast training
+}
+
+static int round_pw2_down(int x)
 {
-    //check boundary, enforce min size
+        for (int i = 1; i < 32; i <<= 1)
+            x |= x >> i;
+        x++;
+        return x >> 1;
+}
+
+
+void KCF_Tracker::init(cv::Mat &img, const cv::Rect &bbox, int fit_size_x, int fit_size_y)
+{
+    __dbgTracer.debug = m_debug;
+    TRACE("");
+
+    // check boundary, enforce min size
     double x1 = bbox.x, x2 = bbox.x + bbox.width, y1 = bbox.y, y2 = bbox.y + bbox.height;
     if (x1 < 0) x1 = 0.;
-    if (x2 > img.cols-1) x2 = img.cols - 1;
+    if (x2 > img.cols - 1) x2 = img.cols - 1;
     if (y1 < 0) y1 = 0;
-    if (y2 > img.rows-1) y2 = img.rows - 1;
+    if (y2 > img.rows - 1) y2 = img.rows - 1;
 
-    if (x2-x1 < 2*p_cell_size) {
-        double diff = (2*p_cell_size -x2+x1)/2.;
-        if (x1 - diff >= 0 && x2 + diff < img.cols){
+    if (x2 - x1 < 2 * p_cell_size) {
+        double diff = (2 * p_cell_size - x2 + x1) / 2.;
+        if (x1 - diff >= 0 && x2 + diff < img.cols) {
             x1 -= diff;
             x2 += diff;
-        } else if (x1 - 2*diff >= 0) {
-            x1 -= 2*diff;
+        } else if (x1 - 2 * diff >= 0) {
+            x1 -= 2 * diff;
         } else {
-            x2 += 2*diff;
+            x2 += 2 * diff;
         }
     }
-    if (y2-y1 < 2*p_cell_size) {
-        double diff = (2*p_cell_size -y2+y1)/2.;
-        if (y1 - diff >= 0 && y2 + diff < img.rows){
+    if (y2 - y1 < 2 * p_cell_size) {
+        double diff = (2 * p_cell_size - y2 + y1) / 2.;
+        if (y1 - diff >= 0 && y2 + diff < img.rows) {
             y1 -= diff;
             y2 += diff;
-        } else if (y1 - 2*diff >= 0) {
-            y1 -= 2*diff;
+        } else if (y1 - 2 * diff >= 0) {
+            y1 -= 2 * diff;
         } else {
-            y2 += 2*diff;
+            y2 += 2 * diff;
         }
     }
 
-    p_pose.w = x2-x1;
-    p_pose.h = y2-y1;
-    p_pose.cx = x1 + p_pose.w/2.;
-    p_pose.cy = y1 + p_pose.h/2.;
-
+    p_init_pose.w = x2 - x1;
+    p_init_pose.h = y2 - y1;
+    p_init_pose.cx = x1 + p_init_pose.w / 2.;
+    p_init_pose.cy = y1 + p_init_pose.h / 2.;
 
     cv::Mat input_gray, input_rgb = img.clone();
-    if (img.channels() == 3){
+    if (img.channels() == 3) {
         cv::cvtColor(img, input_gray, CV_BGR2GRAY);
         input_gray.convertTo(input_gray, CV_32FC1);
-    }else
+    } else
         img.convertTo(input_gray, CV_32FC1);
 
     // don't need too large image
-    if (p_pose.w * p_pose.h > 100.*100.) {
-        std::cout << "resizing image by factor of 2" << std::endl;
+    if (p_init_pose.w * p_init_pose.h > 100. * 100.) {
+        std::cout << "resizing image by factor of " << 1 / p_downscale_factor << std::endl;
         p_resize_image = true;
-        p_pose.scale(0.5);
-        cv::resize(input_gray, input_gray, cv::Size(0,0), 0.5, 0.5, cv::INTER_AREA);
-        cv::resize(input_rgb, input_rgb, cv::Size(0,0), 0.5, 0.5, cv::INTER_AREA);
+        p_init_pose.scale(p_downscale_factor);
+        cv::resize(input_gray, input_gray, cv::Size(0, 0), p_downscale_factor, p_downscale_factor, cv::INTER_AREA);
+        cv::resize(input_rgb, input_rgb, cv::Size(0, 0), p_downscale_factor, p_downscale_factor, cv::INTER_AREA);
     }
 
-    //compute win size + fit to fhog cell size
-    p_windows_size[0] = round(p_pose.w * (1. + p_padding) / p_cell_size) * p_cell_size;
-    p_windows_size[1] = round(p_pose.h * (1. + p_padding) / p_cell_size) * p_cell_size;
+    // compute win size + fit to fhog cell size
+    p_windows_size.width = round(p_init_pose.w * (1. + p_padding) / p_cell_size) * p_cell_size;
+    p_windows_size.height = round(p_init_pose.h * (1. + p_padding) / p_cell_size) * p_cell_size;
+
+    if (fit_size_x == 0 || fit_size_y == 0) {
+        // Round down to the next highest power of 2
+        fit_size = cv::Size(round_pw2_down(p_windows_size.width),
+                            round_pw2_down(p_windows_size.height));
+    } else if (fit_size_x == -1 || fit_size_y == -1) {
+        fit_size =  p_windows_size;
+    } else {
+        fit_size = cv::Size(fit_size_x, fit_size_y);
+    }
+
+    feature_size = fit_size / p_cell_size;
 
     p_scales.clear();
-    if (m_use_scale)
-        for (int i = -p_num_scales/2; i <= 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 - 1) / 2; i <= int(p_num_scales) / 2; ++i)
+        p_scales.push_back(std::pow(p_scale_step, i));
+
+    p_angles.clear();
+    for (int i = -int(p_num_angles - 1) / 2; i <= int(p_num_angles) / 2; ++i)
+        p_angles.push_back(i * p_angle_step);
+
+#ifdef CUFFT
+    if (m_use_linearkernel) {
+        std::cerr << "cuFFT supports only Gaussian kernel." << std::endl;
+        std::exit(EXIT_FAILURE);
+    }
+#endif
 
+    model.reset(new Model(feature_size, p_num_of_feats));
+    d.reset(new Kcf_Tracker_Private(*this));
+
+#ifndef BIG_BATCH
+    for (auto scale: p_scales)
+        for (auto angle : p_angles)
+            d->threadctxs.emplace_back(feature_size, p_num_of_feats, scale, angle);
+#else
+    d->threadctxs.emplace_back(feature_size, p_num_of_feats, p_scales, p_angles);
+#endif
+
+    gaussian_correlation.reset(new GaussianCorrelation(1, p_num_of_feats, feature_size));
+
+    p_current_center = p_init_pose.center();
     p_current_scale = 1.;
 
-    double min_size_ratio = std::max(5.*p_cell_size/p_windows_size[0], 5.*p_cell_size/p_windows_size[1]);
-    double max_size_ratio = std::min(floor((img.cols + p_windows_size[0]/3)/p_cell_size)*p_cell_size/p_windows_size[0], floor((img.rows + p_windows_size[1]/3)/p_cell_size)*p_cell_size/p_windows_size[1]);
+    double min_size_ratio = std::max(5. * p_cell_size / p_windows_size.width, 5. * p_cell_size / p_windows_size.height);
+    double max_size_ratio =
+        std::min(floor((img.cols + p_windows_size.width / 3) / p_cell_size) * p_cell_size / p_windows_size.width,
+                 floor((img.rows + p_windows_size.height / 3) / p_cell_size) * p_cell_size / p_windows_size.height);
     p_min_max_scale[0] = std::pow(p_scale_step, std::ceil(std::log(min_size_ratio) / log(p_scale_step)));
     p_min_max_scale[1] = std::pow(p_scale_step, std::floor(std::log(max_size_ratio) / log(p_scale_step)));
 
-    std::cout << "init: img size " << img.cols << " " << img.rows << std::endl;
-    std::cout << "init: win size. " << p_windows_size[0] << " " << p_windows_size[1] << std::endl;
+    std::cout << "init: img size " << img.size() << std::endl;
+    std::cout << "init: win size " << p_windows_size;
+    if (p_windows_size != fit_size)
+        std::cout << " resized to " << fit_size;
+    std::cout << std::endl;
+    std::cout << "init: FFT size " << feature_size << std::endl;
     std::cout << "init: min max scales factors: " << p_min_max_scale[0] << " " << p_min_max_scale[1] << std::endl;
 
-    p_output_sigma = std::sqrt(p_pose.w*p_pose.h) * p_output_sigma_factor / static_cast<double>(p_cell_size);
+    p_output_sigma = std::sqrt(p_init_pose.w * p_init_pose.h * double(fit_size.area()) / p_windows_size.area())
+           * p_output_sigma_factor / p_cell_size;
 
-#if defined(FFTW) && defined(OPENMP)
-    fftw_init_threads();
-    fftw_plan_with_nthreads(omp_get_max_threads());
-#endif
+    fft.init(feature_size.width, feature_size.height, p_num_of_feats, p_num_scales * p_num_angles);
+    fft.set_window(MatDynMem(cosine_window_function(feature_size.width, feature_size.height)));
 
-    //window weights, i.e. labels
-    p_yf = fft2(gaussian_shaped_labels(p_output_sigma, p_windows_size[0]/p_cell_size, p_windows_size[1]/p_cell_size));
-    p_cos_window = cosine_window_function(p_yf.cols, p_yf.rows);
-    //obtain a sub-window for training initial model
-    std::vector<cv::Mat> path_feat = get_features(input_rgb, input_gray, p_pose.cx, p_pose.cy, p_windows_size[0], p_windows_size[1]);
-    p_model_xf = fft2(path_feat, p_cos_window);
+    // window weights, i.e. labels
+    MatScales gsl(1, feature_size);
+    gaussian_shaped_labels(p_output_sigma, feature_size.width, feature_size.height).copyTo(gsl.plane(0));
+    fft.forward(gsl, model->yf);
+    DEBUG_PRINTM(model->yf);
 
-    if (m_use_linearkernel) {
-        ComplexMat xfconj = p_model_xf.conj();
-        p_model_alphaf_num = xfconj.mul(p_yf);
-        p_model_alphaf_den = (p_model_xf * xfconj);
-    } else {
-        //Kernel Ridge Regression, calculate alphas (in Fourier domain)
-        ComplexMat kf = gaussian_correlation(p_model_xf, p_model_xf, p_kernel_sigma, true);
-        p_model_alphaf_num = p_yf * kf;
-        p_model_alphaf_den = kf * (kf + p_lambda);
-    }
-    p_model_alphaf = p_model_alphaf_num / p_model_alphaf_den;
-//        p_model_alphaf = p_yf / (kf + p_lambda);   //equation for fast training
+    // train initial model
+    train(input_rgb, input_gray, 1.0);
 }
 
-void KCF_Tracker::setTrackerPose(BBox_c &bbox, cv::Mat & img)
+void KCF_Tracker::setTrackerPose(BBox_c &bbox, cv::Mat &img, int fit_size_x, int fit_size_y)
 {
-    init(img, bbox.get_rect());
+    init(img, bbox.get_rect(), fit_size_x, fit_size_y);
 }
 
 void KCF_Tracker::updateTrackerPosition(BBox_c &bbox)
 {
+    BBox_c tmp = bbox;
     if (p_resize_image) {
-        BBox_c tmp = bbox;
-        tmp.scale(0.5);
-        p_pose.cx = tmp.cx;
-        p_pose.cy = tmp.cy;
-    } else {
-        p_pose.cx = bbox.cx;
-        p_pose.cy = bbox.cy;
+        tmp.scale(p_downscale_factor);
     }
+    p_current_center = tmp.center();
 }
 
 BBox_c KCF_Tracker::getBBox()
 {
-    BBox_c tmp = p_pose;
-    tmp.w *= p_current_scale;
-    tmp.h *= p_current_scale;
+    BBox_c tmp;
+    tmp.cx = p_current_center.x;
+    tmp.cy = p_current_center.y;
+    tmp.w = p_init_pose.w * p_current_scale;
+    tmp.h = p_init_pose.h * p_current_scale;
+    tmp.a = p_current_angle;
 
     if (p_resize_image)
-        tmp.scale(2);
+        tmp.scale(1 / p_downscale_factor);
 
     return tmp;
 }
 
-void KCF_Tracker::track(cv::Mat &img)
+double KCF_Tracker::getFilterResponse() const
 {
+    return this->max_response;
+}
 
-    cv::Mat input_gray, input_rgb = img.clone();
-    if (img.channels() == 3){
-        cv::cvtColor(img, input_gray, CV_BGR2GRAY);
-        input_gray.convertTo(input_gray, CV_32FC1);
-    }else
-        img.convertTo(input_gray, CV_32FC1);
-
-    // don't need too large image
+void KCF_Tracker::resizeImgs(cv::Mat &input_rgb, cv::Mat &input_gray)
+{
     if (p_resize_image) {
-        cv::resize(input_gray, input_gray, cv::Size(0, 0), 0.5, 0.5, cv::INTER_AREA);
-        cv::resize(input_rgb, input_rgb, cv::Size(0, 0), 0.5, 0.5, cv::INTER_AREA);
+        cv::resize(input_gray, input_gray, cv::Size(0, 0), p_downscale_factor, p_downscale_factor, cv::INTER_AREA);
+        cv::resize(input_rgb, input_rgb, cv::Size(0, 0), p_downscale_factor, p_downscale_factor, cv::INTER_AREA);
     }
+}
 
+static void drawCross(cv::Mat &img, cv::Point center, bool green)
+{
+    cv::Scalar col = green ? cv::Scalar(0, 1, 0) : cv::Scalar(0, 0, 1);
+    cv::line(img, cv::Point(center.x, 0), cv::Point(center.x, img.size().height), col);
+    cv::line(img, cv::Point(0, center.y), cv::Point(img.size().height, center.y), col);
+}
 
-    std::vector<cv::Mat> patch_feat;
-    double max_response = -1.;
-    cv::Mat max_response_map;
-    cv::Point2i max_response_pt;
-    int scale_index = 0;
-    std::vector<double> scale_responses;
+static cv::Point2d wrapAroundFreq(cv::Point2d pt, cv::Mat &resp_map)
+{
+    if (pt.y > resp_map.rows / 2) // wrap around to negative half-space of vertical axis
+        pt.y = pt.y - resp_map.rows;
+    if (pt.x > resp_map.cols / 2) // same for horizontal axis
+        pt.x = pt.x - resp_map.cols;
+    return pt;
+}
 
-    if (m_use_multithreading){
-        std::vector<std::future<cv::Mat>> async_res(p_scales.size());
-        for (size_t i = 0; i < p_scales.size(); ++i) {
-            async_res[i] = std::async(std::launch::async,
-                                      [this, &input_gray, &input_rgb, i]() -> cv::Mat
-                                      {
-                                          std::vector<cv::Mat> patch_feat_async = get_features(input_rgb, input_gray, this->p_pose.cx, this->p_pose.cy, this->p_windows_size[0],
-                                                                                               this->p_windows_size[1], this->p_current_scale * this->p_scales[i]);
-                                          ComplexMat zf = fft2(patch_feat_async, this->p_cos_window);
-                                          if (m_use_linearkernel)
-                                              return ifft2((p_model_alphaf * zf).sum_over_channels());
-                                          else {
-                                              ComplexMat kzf = gaussian_correlation(zf, this->p_model_xf, this->p_kernel_sigma);
-                                              return ifft2(this->p_model_alphaf * kzf);
-                                          }
-                                      });
-        }
+double KCF_Tracker::findMaxReponse(uint &max_idx, cv::Point2d &new_location) const
+{
+    double max;
+    const auto &vec = IF_BIG_BATCH(d->threadctxs[0].max, d->threadctxs);
 
-        for (size_t i = 0; i < p_scales.size(); ++i) {
-            // wait for result
-            async_res[i].wait();
-            cv::Mat response = async_res[i].get();
-
-            double min_val, max_val;
-            cv::Point2i min_loc, max_loc;
-            cv::minMaxLoc(response, &min_val, &max_val, &min_loc, &max_loc);
-
-            double weight = p_scales[i] < 1. ? p_scales[i] : 1./p_scales[i];
-            if (max_val*weight > max_response) {
-                max_response = max_val*weight;
-                max_response_map = response;
-                max_response_pt = max_loc;
-                scale_index = i;
-            }
-            scale_responses.push_back(max_val*weight);
-        }
-    } else {
-#pragma omp parallel for ordered  private(patch_feat) schedule(dynamic)
-        for (size_t i = 0; i < p_scales.size(); ++i) {
-            patch_feat = get_features(input_rgb, input_gray, p_pose.cx, p_pose.cy, p_windows_size[0], p_windows_size[1], p_current_scale * p_scales[i]);
-            ComplexMat zf = fft2(patch_feat, p_cos_window);
-            cv::Mat response;
-            if (m_use_linearkernel)
-                response = ifft2((p_model_alphaf * zf).sum_over_channels());
-            else {
-                ComplexMat kzf = gaussian_correlation(zf, p_model_xf, p_kernel_sigma);
-                response = ifft2(p_model_alphaf * kzf);
-            }
+#ifndef BIG_BATCH
+    auto max_it = std::max_element(vec.begin(), vec.end(),
+                                   [](const ThreadCtx &a, const ThreadCtx &b)
+                                   { return a.max.response < b.max.response; });
+#else
+    auto max_it = std::max_element(vec.begin(), vec.end(),
+                                   [](const ThreadCtx::Max &a, const ThreadCtx::Max &b)
+                                   { return a.response < b.response; });
+#endif
+    assert(max_it != vec.end());
+    max = max_it->IF_BIG_BATCH(response, max.response);
+
+    max_idx = std::distance(vec.begin(), max_it);
+
+    cv::Point2i max_response_pt = IF_BIG_BATCH(max_it->loc, max_it->max.loc);
+    cv::Mat max_response_map    = IF_BIG_BATCH(d->threadctxs[0].response.plane(max_idx),
+                                               max_it->response.plane(0));
+
+    DEBUG_PRINTM(max_response_map);
+    DEBUG_PRINT(max_response_pt);
 
-            /* target location is at the maximum response. we must take into
-               account the fact that, if the target doesn't move, the peak
-               will appear at the top-left corner, not at the center (this is
-               discussed in the paper). the responses wrap around cyclically. */
-            double min_val, max_val;
-            cv::Point2i min_loc, max_loc;
-            cv::minMaxLoc(response, &min_val, &max_val, &min_loc, &max_loc);
-
-            double weight = p_scales[i] < 1. ? p_scales[i] : 1./p_scales[i];
-            if (max_val*weight > max_response) {
-#pragma omp critical
-                max_response = max_val*weight;
-                max_response_map = response;
-                max_response_pt = max_loc;
-                scale_index = i;
+    max_response_pt = wrapAroundFreq(max_response_pt, max_response_map);
+
+    // sub pixel quadratic interpolation from neighbours
+    if (m_use_subpixel_localization) {
+        new_location = sub_pixel_peak(max_response_pt, max_response_map);
+    } else {
+        new_location = max_response_pt;
+    }
+    DEBUG_PRINT(new_location);
+
+    if (m_visual_debug != vd::NONE) {
+        const bool fit = 1;
+        int w = fit ? 100 : (m_visual_debug == vd::PATCH ? fit_size.width  : feature_size.width);
+        int h = fit ? 100 : (m_visual_debug == vd::PATCH ? fit_size.height : feature_size.height);
+        cv::Mat all_responses((h + 1) * p_num_scales - 1,
+                              (w + 1) * p_num_angles - 1, CV_32FC3, cv::Scalar::all(0));
+        for (size_t i = 0; i < p_num_scales; ++i) {
+            for (size_t j = 0; j < p_num_angles; ++j) {
+                auto &threadctx = d->IF_BIG_BATCH(threadctxs[0], threadctxs(i, j));
+                cv::Mat tmp;
+                cv::Point2d cross = threadctx.IF_BIG_BATCH(max(i, j), max).loc;
+                cross = wrapAroundFreq(cross, max_response_map);
+                if (m_visual_debug == vd::PATCH ) {
+                    threadctx.dbg_patch IF_BIG_BATCH((i, j),)
+                            .convertTo(tmp, all_responses.type(), 1.0 / 255);
+                    cross.x = cross.x / fit_size.width  * tmp.cols + tmp.cols / 2;
+                    cross.y = cross.y / fit_size.height * tmp.rows + tmp.rows / 2;
+                } else {
+                    cv::cvtColor(threadctx.response.plane(IF_BIG_BATCH(threadctx.max.getIdx(i, j), 0)),
+                            tmp, cv::COLOR_GRAY2BGR);
+                    tmp /= max; // Normalize to 1
+                    cross += cv::Point2d(tmp.size())/2;
+                    tmp = circshift(tmp, -tmp.cols/2, -tmp.rows/2);
+                    //drawCross(tmp, cross, false);
+                }
+                bool green = false;
+                if (&*max_it == &IF_BIG_BATCH(threadctx.max(i, j), threadctx)) {
+                    // Show the green cross at position of sub-pixel interpolation (if enabled)
+                    cross = new_location + cv::Point2d(tmp.size())/2;
+                    green = true;
+                }
+                // Move to the center of pixes (if scaling up) and scale
+                cross.x = (cross.x + 0.5) * double(w)/tmp.cols;
+                cross.y = (cross.y + 0.5) * double(h)/tmp.rows;
+                cv::resize(tmp, tmp, cv::Size(w, h)); //, 0, 0, cv::INTER_NEAREST);
+                drawCross(tmp, cross, green);
+                cv::Mat resp_roi(all_responses, cv::Rect(j * (w+1), i * (h+1), w, h));
+                tmp.copyTo(resp_roi);
             }
-#pragma omp ordered
-            scale_responses.push_back(max_val*weight);
         }
+        cv::namedWindow("KCF visual debug", CV_WINDOW_AUTOSIZE);
+        cv::imshow("KCF visual debug", all_responses);
     }
 
-    //sub pixel quadratic interpolation from neighbours
-    if (max_response_pt.y > max_response_map.rows / 2) //wrap around to negative half-space of vertical axis
-        max_response_pt.y = max_response_pt.y - max_response_map.rows;
-    if (max_response_pt.x > max_response_map.cols / 2) //same for horizontal axis
-        max_response_pt.x = max_response_pt.x - max_response_map.cols;
+    return max;
+}
 
-    cv::Point2f new_location(max_response_pt.x, max_response_pt.y);
+void KCF_Tracker::track(cv::Mat &img)
+{
+    __dbgTracer.debug = m_debug;
+    TRACE("");
 
-    if (m_use_subpixel_localization)
-        new_location = sub_pixel_peak(max_response_pt, max_response_map);
+    cv::Mat input_gray, input_rgb = img.clone();
+    if (img.channels() == 3) {
+        cv::cvtColor(img, input_gray, CV_BGR2GRAY);
+        input_gray.convertTo(input_gray, CV_32FC1);
+    } else
+        img.convertTo(input_gray, CV_32FC1);
 
-    p_pose.cx += p_current_scale*p_cell_size*new_location.x;
-    p_pose.cy += p_current_scale*p_cell_size*new_location.y;
-    if (p_pose.cx < 0) p_pose.cx = 0;
-    if (p_pose.cx > img.cols-1) p_pose.cx = img.cols-1;
-    if (p_pose.cy < 0) p_pose.cy = 0;
-    if (p_pose.cy > img.rows-1) p_pose.cy = img.rows-1;
+    // don't need too large image
+    resizeImgs(input_rgb, input_gray);
+
+#ifdef ASYNC
+    for (auto &it : d->threadctxs)
+        it.async_res = std::async(std::launch::async, [this, &input_gray, &input_rgb, &it]() -> void {
+            it.track(*this, input_rgb, input_gray);
+        });
+    for (auto const &it : d->threadctxs)
+        it.async_res.wait();
+
+#else  // !ASYNC
+    NORMAL_OMP_PARALLEL_FOR
+    for (uint i = 0; i < d->threadctxs.size(); ++i)
+        d->threadctxs[i].track(*this, input_rgb, input_gray);
+#endif
 
-    //sub grid scale interpolation
-    double new_scale = p_scales[scale_index];
-    if (m_use_subgrid_scale)
-        new_scale = sub_grid_scale(scale_responses, scale_index);
+    cv::Point2d new_location;
+    uint max_idx;
+    max_response = findMaxReponse(max_idx, new_location);
 
-    p_current_scale *= new_scale;
+    double angle_change = d->IF_BIG_BATCH(threadctxs[0].max, threadctxs).angle(max_idx);
+    p_current_angle += angle_change;
 
-    if (p_current_scale < p_min_max_scale[0])
-        p_current_scale = p_min_max_scale[0];
-    if (p_current_scale > p_min_max_scale[1])
-        p_current_scale = p_min_max_scale[1];
+    new_location.x = new_location.x * cos(-p_current_angle/180*M_PI) + new_location.y * sin(-p_current_angle/180*M_PI);
+    new_location.y = new_location.y * cos(-p_current_angle/180*M_PI) - new_location.x * sin(-p_current_angle/180*M_PI);
 
-    //obtain a subwindow for training at newly estimated target position
-    patch_feat = get_features(input_rgb, input_gray, p_pose.cx, p_pose.cy, p_windows_size[0], p_windows_size[1], p_current_scale);
-    ComplexMat xf = fft2(patch_feat, p_cos_window);
+    new_location.x *= double(p_windows_size.width) / fit_size.width;
+    new_location.y *= double(p_windows_size.height) / fit_size.height;
 
-    //subsequent frames, interpolate model
-    p_model_xf = p_model_xf * (1. - p_interp_factor) + xf * p_interp_factor;
+    p_current_center += p_current_scale * p_cell_size * new_location;
 
-    ComplexMat alphaf_num, alphaf_den;
+    clamp2(p_current_center.x, 0.0, img.cols - 1.0);
+    clamp2(p_current_center.y, 0.0, img.rows - 1.0);
 
-    if (m_use_linearkernel) {
-        ComplexMat xfconj = xf.conj();
-        alphaf_num = xfconj.mul(p_yf);
-        alphaf_den = (xf * xfconj);
+    // sub grid scale interpolation
+    if (m_use_subgrid_scale) {
+        p_current_scale *= sub_grid_scale(max_idx);
+    } else {
+        p_current_scale *= d->IF_BIG_BATCH(threadctxs[0].max, threadctxs).scale(max_idx);
+    }
+
+    clamp2(p_current_scale, p_min_max_scale[0], p_min_max_scale[1]);
+
+
+    // train at newly estimated target position
+    train(input_rgb, input_gray, p_interp_factor);
+}
+
+void ThreadCtx::track(const KCF_Tracker &kcf, cv::Mat &input_rgb, cv::Mat &input_gray)
+{
+    TRACE("");
+
+    BIG_BATCH_OMP_PARALLEL_FOR
+    for (uint i = 0; i < IF_BIG_BATCH(max.size(), 1); ++i)
+    {
+        kcf.get_features(input_rgb, input_gray, &dbg_patch IF_BIG_BATCH([i],),
+                         kcf.p_current_center.x, kcf.p_current_center.y,
+                         kcf.p_windows_size.width, kcf.p_windows_size.height,
+                         kcf.p_current_scale * IF_BIG_BATCH(max.scale(i), scale),
+                         kcf.p_current_angle + IF_BIG_BATCH(max.angle(i), angle))
+                .copyTo(patch_feats.scale(i));
+        DEBUG_PRINT(patch_feats.scale(i));
+    }
+
+    kcf.fft.forward_window(patch_feats, zf, temp);
+    DEBUG_PRINTM(zf);
+
+    if (kcf.m_use_linearkernel) {
+        kzf = zf.mul(kcf.model->model_alphaf).sum_over_channels();
     } else {
-        //Kernel Ridge Regression, calculate alphas (in Fourier domain)
-        ComplexMat kf = gaussian_correlation(xf, xf, p_kernel_sigma, true);
-//        ComplexMat alphaf = p_yf / (kf + p_lambda); //equation for fast training
-//        p_model_alphaf = p_model_alphaf * (1. - p_interp_factor) + alphaf * p_interp_factor;
-        alphaf_num = p_yf * kf;
-        alphaf_den = kf * (kf + p_lambda);
+        gaussian_correlation(kzf, zf, kcf.model->model_xf, kcf.p_kernel_sigma, false, kcf);
+        DEBUG_PRINTM(kzf);
+        kzf = kzf.mul(kcf.model->model_alphaf);
+    }
+    kcf.fft.inverse(kzf, response);
+
+    DEBUG_PRINTM(response);
+
+    /* target location is at the maximum response. we must take into
+    account the fact that, if the target doesn't move, the peak
+    will appear at the top-left corner, not at the center (this is
+    discussed in the paper). the responses wrap around cyclically. */
+    double min_val, max_val;
+    cv::Point2i min_loc, max_loc;
+#ifdef BIG_BATCH
+    for (size_t i = 0; i < max.size(); ++i) {
+        cv::minMaxLoc(response.plane(i), &min_val, &max_val, &min_loc, &max_loc);
+        DEBUG_PRINT(max_loc);
+        double weight = kcf.p_scales[i] < 1. ? kcf.p_scales[i] : 1. / kcf.p_scales[i];
+        max[i].response = max_val * weight;
+        max[i].loc = max_loc;
     }
+#else
+    cv::minMaxLoc(response.plane(0), &min_val, &max_val, &min_loc, &max_loc);
+
+    DEBUG_PRINT(max_loc);
+    DEBUG_PRINT(max_val);
 
-    p_model_alphaf_num = p_model_alphaf_num * (1. - p_interp_factor) + alphaf_num * p_interp_factor;
-    p_model_alphaf_den = p_model_alphaf_den * (1. - p_interp_factor) + alphaf_den * p_interp_factor;
-    p_model_alphaf = p_model_alphaf_num / p_model_alphaf_den;
+    double weight = scale < 1. ? scale : 1. / scale;
+    max.response = max_val * weight;
+    max.loc = max_loc;
+#endif
 }
 
 // ****************************************************************************
 
-std::vector<cv::Mat> KCF_Tracker::get_features(cv::Mat & input_rgb, cv::Mat & input_gray, int cx, int cy, int size_x, int size_y, double scale)
+cv::Mat KCF_Tracker::get_features(cv::Mat &input_rgb, cv::Mat &input_gray, cv::Mat *dbg_patch,
+                                  int cx, int cy, int size_x, int size_y, double scale, double angle) const
 {
-    int size_x_scaled = floor(size_x*scale);
-    int size_y_scaled = floor(size_y*scale);
-
-    cv::Mat patch_gray = get_subwindow(input_gray, cx, cy, size_x_scaled, size_y_scaled);
-    cv::Mat patch_rgb = get_subwindow(input_rgb, cx, cy, size_x_scaled, size_y_scaled);
-
-    //resize to default size
-    if (scale > 1.){
-        //if we downsample use  INTER_AREA interpolation
-        cv::resize(patch_gray, patch_gray, cv::Size(size_x, size_y), 0., 0., cv::INTER_AREA);
-    }else {
-        cv::resize(patch_gray, patch_gray, cv::Size(size_x, size_y), 0., 0., cv::INTER_LINEAR);
+    cv::Size scaled = cv::Size(floor(size_x * scale), floor(size_y * scale));
+
+    cv::Mat patch_gray = get_subwindow(input_gray, cx, cy, scaled.width, scaled.height, angle);
+    cv::Mat patch_rgb = get_subwindow(input_rgb, cx, cy, scaled.width, scaled.height, angle);
+
+    if (dbg_patch)
+        patch_rgb.copyTo(*dbg_patch);
+
+    // resize to default size
+    if (scaled.area() > fit_size.area()) {
+        // if we downsample use  INTER_AREA interpolation
+        // note: this is just a guess - we may downsample in X and upsample in Y (or vice versa)
+        cv::resize(patch_gray, patch_gray, fit_size, 0., 0., cv::INTER_AREA);
+    } else {
+        cv::resize(patch_gray, patch_gray, fit_size, 0., 0., cv::INTER_LINEAR);
     }
 
     // get hog(Histogram of Oriented Gradients) features
     std::vector<cv::Mat> hog_feat = FHoG::extract(patch_gray, 2, p_cell_size, 9);
 
-    //get color rgb features (simple r,g,b channels)
+    // get color rgb features (simple r,g,b channels)
     std::vector<cv::Mat> color_feat;
     if ((m_use_color || m_use_cnfeat) && input_rgb.channels() == 3) {
-        //resize to default size
-        if (scale > 1.){
-            //if we downsample use  INTER_AREA interpolation
-            cv::resize(patch_rgb, patch_rgb, cv::Size(size_x/p_cell_size, size_y/p_cell_size), 0., 0., cv::INTER_AREA);
-        }else {
-            cv::resize(patch_rgb, patch_rgb, cv::Size(size_x/p_cell_size, size_y/p_cell_size), 0., 0., cv::INTER_LINEAR);
+        // resize to default size
+        if (scaled.area() > (fit_size / p_cell_size).area()) {
+            // if we downsample use  INTER_AREA interpolation
+            cv::resize(patch_rgb, patch_rgb, fit_size / p_cell_size, 0., 0., cv::INTER_AREA);
+        } else {
+            cv::resize(patch_rgb, patch_rgb, fit_size / p_cell_size, 0., 0., cv::INTER_LINEAR);
         }
     }
 
     if (m_use_color && input_rgb.channels() == 3) {
-        //use rgb color space
+        // use rgb color space
         cv::Mat patch_rgb_norm;
         patch_rgb.convertTo(patch_rgb_norm, CV_32F, 1. / 255., -0.5);
         cv::Mat ch1(patch_rgb_norm.size(), CV_32FC1);
@@ -340,7 +565,13 @@ std::vector<cv::Mat> KCF_Tracker::get_features(cv::Mat & input_rgb, cv::Mat & in
     }
 
     hog_feat.insert(hog_feat.end(), color_feat.begin(), color_feat.end());
-    return hog_feat;
+
+    int size[] = {p_num_of_feats, feature_size.height, feature_size.width};
+    cv::Mat result(3, size, CV_32F);
+    for (uint i = 0; i < hog_feat.size(); ++i)
+        hog_feat[i].copyTo(cv::Mat(size[1], size[2], CV_32FC1, result.ptr(i)));
+
+    return result;
 }
 
 cv::Mat KCF_Tracker::gaussian_shaped_labels(double sigma, int dim1, int dim2)
@@ -349,80 +580,80 @@ cv::Mat KCF_Tracker::gaussian_shaped_labels(double sigma, int dim1, int dim2)
     int range_y[2] = {-dim2 / 2, dim2 - dim2 / 2};
     int range_x[2] = {-dim1 / 2, dim1 - dim1 / 2};
 
-    double sigma_s = sigma*sigma;
+    double sigma_s = sigma * sigma;
 
-    for (int y = range_y[0], j = 0; y < range_y[1]; ++y, ++j){
-        float * row_ptr = labels.ptr<float>(j);
-        double y_s = y*y;
-        for (int x = range_x[0], i = 0; x < range_x[1]; ++x, ++i){
-            row_ptr[i] = std::exp(-0.5 * (y_s + x*x) / sigma_s);//-1/2*e^((y^2+x^2)/sigma^2)
+    for (int y = range_y[0], j = 0; y < range_y[1]; ++y, ++j) {
+        float *row_ptr = labels.ptr<float>(j);
+        double y_s = y * y;
+        for (int x = range_x[0], i = 0; x < range_x[1]; ++x, ++i) {
+            row_ptr[i] = std::exp(-0.5 * (y_s + x * x) / sigma_s); //-1/2*e^((y^2+x^2)/sigma^2)
         }
     }
 
-    //rotate so that 1 is at top-left corner (see KCF paper for explanation)
-    cv::Mat rot_labels = circshift(labels, range_x[0], range_y[0]);
-    //sanity check, 1 at top left corner
-    assert(rot_labels.at<float>(0,0) >= 1.f - 1e-10f);
+    // rotate so that 1 is at top-left corner (see KCF paper for explanation)
+    MatDynMem rot_labels = circshift(labels, range_x[0], range_y[0]);
+    // sanity check, 1 at top left corner
+    assert(rot_labels.at<float>(0, 0) >= 1.f - 1e-10f);
 
     return rot_labels;
 }
 
-cv::Mat KCF_Tracker::circshift(const cv::Mat &patch, int x_rot, int y_rot)
+cv::Mat KCF_Tracker::circshift(const cv::Mat &patch, int x_rot, int y_rot) const
 {
-    cv::Mat rot_patch(patch.size(), CV_32FC1);
-    cv::Mat tmp_x_rot(patch.size(), CV_32FC1);
+    cv::Mat rot_patch(patch.size(), patch.type());
+    cv::Mat tmp_x_rot(patch.size(), patch.type());
 
-    //circular rotate x-axis
+    // circular rotate x-axis
     if (x_rot < 0) {
-        //move part that does not rotate over the edge
+        // move part that does not rotate over the edge
         cv::Range orig_range(-x_rot, patch.cols);
         cv::Range rot_range(0, patch.cols - (-x_rot));
         patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
 
-        //rotated part
+        // rotated part
         orig_range = cv::Range(0, -x_rot);
         rot_range = cv::Range(patch.cols - (-x_rot), patch.cols);
         patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
-    }else if (x_rot > 0){
-        //move part that does not rotate over the edge
+    } else if (x_rot > 0) {
+        // move part that does not rotate over the edge
         cv::Range orig_range(0, patch.cols - x_rot);
         cv::Range rot_range(x_rot, patch.cols);
         patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
 
-        //rotated part
+        // rotated part
         orig_range = cv::Range(patch.cols - x_rot, patch.cols);
         rot_range = cv::Range(0, x_rot);
         patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
-    }else {    //zero rotation
-        //move part that does not rotate over the edge
+    } else { // zero rotation
+        // move part that does not rotate over the edge
         cv::Range orig_range(0, patch.cols);
         cv::Range rot_range(0, patch.cols);
         patch(cv::Range::all(), orig_range).copyTo(tmp_x_rot(cv::Range::all(), rot_range));
     }
 
-    //circular rotate y-axis
+    // circular rotate y-axis
     if (y_rot < 0) {
-        //move part that does not rotate over the edge
+        // move part that does not rotate over the edge
         cv::Range orig_range(-y_rot, patch.rows);
         cv::Range rot_range(0, patch.rows - (-y_rot));
         tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
 
-        //rotated part
+        // rotated part
         orig_range = cv::Range(0, -y_rot);
         rot_range = cv::Range(patch.rows - (-y_rot), patch.rows);
         tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
-    }else if (y_rot > 0){
-        //move part that does not rotate over the edge
+    } else if (y_rot > 0) {
+        // move part that does not rotate over the edge
         cv::Range orig_range(0, patch.rows - y_rot);
         cv::Range rot_range(y_rot, patch.rows);
         tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
 
-        //rotated part
+        // rotated part
         orig_range = cv::Range(patch.rows - y_rot, patch.rows);
         rot_range = cv::Range(0, y_rot);
         tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
-    }else { //zero rotation
-        //move part that does not rotate over the edge
+    } else { // zero rotation
+        // move part that does not rotate over the edge
         cv::Range orig_range(0, patch.rows);
         cv::Range rot_range(0, patch.rows);
         tmp_x_rot(orig_range, cv::Range::all()).copyTo(rot_patch(rot_range, cv::Range::all()));
@@ -431,391 +662,46 @@ cv::Mat KCF_Tracker::circshift(const cv::Mat &patch, int x_rot, int y_rot)
     return rot_patch;
 }
 
-ComplexMat KCF_Tracker::fft2(const cv::Mat &input)
-{
-    cv::Mat complex_result;
-#ifdef OPENCV_CUFFT
-    cv::Mat flip_h,imag_h;
-
-    cv::cuda::HostMem hostmem_input(input, cv::cuda::HostMem::SHARED);
-    cv::cuda::HostMem hostmem_real(cv::Size(input.cols,input.rows/2+1), CV_32FC2, cv::cuda::HostMem::SHARED);
-
-    cv::cuda::dft(hostmem_input,hostmem_real,hostmem_input.size(),0,stream);
-    stream.waitForCompletion();
-
-    cv::Mat real_h = hostmem_real.createMatHeader();
-
-    //create reversed copy of result and merge them
-    cv::flip(hostmem_real,flip_h,1);
-    flip_h(cv::Range(0, flip_h.rows), cv::Range(1, flip_h.cols)).copyTo(imag_h);
-
-    std::vector<cv::Mat> matarray = {real_h,imag_h};
-
-    cv::hconcat(matarray,complex_result);
-#endif
-#ifdef FFTW
-    // Prepare variables and FFTW plan for float precision FFT
-//     float *data_in;
-    fftwf_complex    *fft;
-
-    fftwf_plan       plan_f;
-
-    int  width, height;
-
-    width         = input.cols;
-    height        = input.rows;
-
-    float* outdata = new float[2*width * height];
-
-//     data_in =  fftwf_alloc_real(width * height);
-    #pragma omp critical
-    {
-    fft = fftwf_alloc_complex((width/2+1) * height);
-    plan_f=fftwf_plan_dft_r2c_2d( height , width , (float*)input.data , fft ,  FFTW_ESTIMATE );
-    }
-    // Prepare input data
-//     for(int i = 0,k=0; i < height; ++i) {
-//         const float* row = input.ptr<float>(i);
-//         for(int j = 0; j < width; j++) {
-//             data_in[k]=(float)row[j];
-//             k++;
-//         }
-//     }
-
-    // Exectue fft
-    fftwf_execute( plan_f );
-
-    // Get output data to right format
-    int width2=2*width;
-    for(int  i = 0, k = 0,l=0 ; i < height; i++ ) {
-        for(int  j = 0 ; j < width2 ; j++ ) {
-            if(j<=width2/2-1){
-                outdata[i * width2 + j] = (float)fft[k][0];
-                outdata[i * width2 + j+1] = (float)fft[k][1];
-
-                j++;
-                k++;
-                l++;
-            }else{
-                l--;
-                outdata[i * width2 + j] = (float)fft[l][0];
-                outdata[i * width2 + j+1] = (float)fft[l][1];
-
-                j++;
-            }
-        }
-    }
-    cv::Mat tmp(height,width,CV_32FC2,outdata);
-    complex_result=tmp;
-    // Destroy FFTW plan and variables
-#pragma omp critical
-    {
-    fftwf_destroy_plan(plan_f);
-    fftwf_free(fft); /*fftwf_free(data_in);*/
-    }
-#endif
-#if !defined OPENCV_CUFFT || !defined FFTW
-    cv::dft(input, complex_result, cv::DFT_COMPLEX_OUTPUT);
-#endif
-#ifdef DEBUG_MODE
-    //extraxt x and y channels
-    cv::Mat xy[2]; //X,Y
-    cv::split(complex_result, xy);
-
-    //calculate angle and magnitude
-    cv::Mat magnitude, angle;
-    cv::cartToPolar(xy[0], xy[1], magnitude, angle, true);
-
-    //translate magnitude to range [0;1]
-    double mag_max;
-    cv::minMaxLoc(magnitude, 0, &mag_max);
-    magnitude.convertTo(magnitude, -1, 1.0 / mag_max);
-
-    //build hsv image
-    cv::Mat _hsv[3], hsv;
-    _hsv[0] = angle;
-    _hsv[1] = cv::Mat::ones(angle.size(), CV_32F);
-    _hsv[2] = magnitude;
-    cv::merge(_hsv, 3, hsv);
-
-    //convert to BGR and show
-    cv::Mat bgr;//CV_32FC3 matrix
-    cv::cvtColor(hsv, bgr, cv::COLOR_HSV2BGR);
-    cv::resize(bgr, bgr, cv::Size(600,600));
-    cv::imshow("DFT", bgr);
-    cv::waitKey(0);
-#endif //DEBUG_MODE
-    return ComplexMat(complex_result);
-}
-
-ComplexMat KCF_Tracker::fft2(const std::vector<cv::Mat> &input, const cv::Mat &cos_window)
-{
-    int n_channels = input.size();
-    cv::Mat complex_result;
-
-#ifdef OPENCV_CUFFT
-    cv::Mat flip_h,imag_h;
-    cv::cuda::GpuMat src_gpu;
-    cv::cuda::HostMem hostmem_real(cv::Size(input[0].cols,input[0].rows/2+1), CV_32FC2, cv::cuda::HostMem::SHARED);
-#endif
-#ifdef FFTW
-    // Prepare variables and FFTW plan for float precision FFT
-//     float *data_in;
-    fftwf_complex    *fft;
-
-    fftwf_plan       plan_f;
-
-    int  width, height, width2;
-
-    width         = input[0].cols;
-    height        = input[0].rows;
-    width2=2*width;
-
-    float* outdata = new float[2*width * height];
-    cv::Mat in_img  = cv::Mat::zeros(height, width, CV_32FC1);
-//     data_in =  fftwf_alloc_real(width * height);
-    #pragma omp critical 
-    {
-    fft = fftwf_alloc_complex((width/2+1) * height);
-    plan_f=fftwf_plan_dft_r2c_2d( height , width , (float*) in_img.data , fft ,  FFTW_ESTIMATE );
-    }
-#endif
-
-    ComplexMat result(input[0].rows, input[0].cols, n_channels);
-    for (int i = 0; i < n_channels; ++i){
-#ifdef OPENCV_CUFFT
-        cv::cuda::HostMem hostmem_input(input[i], cv::cuda::HostMem::SHARED);
-        cv::cuda::multiply(hostmem_input,p_cos_window_d,src_gpu);
-        cv::cuda::dft(src_gpu,hostmem_real,src_gpu.size(),0,stream);
-        stream.waitForCompletion();
-
-        cv::Mat real_h = hostmem_real.createMatHeader();
-
-        //create reversed copy of result and merge them
-        cv::flip(hostmem_real,flip_h,1);
-        flip_h(cv::Range(0, flip_h.rows), cv::Range(1, flip_h.cols)).copyTo(imag_h);
-
-        std::vector<cv::Mat> matarray = {real_h,imag_h};
-
-        cv::hconcat(matarray,complex_result);
-#endif
-#ifdef FFTW
-        // Prepare input data
-        cv::Mat in_img = input[i].mul(cos_window);
-//         for(int x = 0,k=0; x< height; ++x) {
-//             const float* row = in_img.ptr<float>(x);
-//             for(int j = 0; j < width; j++) {
-//                 data_in[k]=(float)row[j];
-//                 k++;
-//             }
-//         }
-
-        // Execute FFT
-        fftwf_execute( plan_f );
-
-        // Get output data to right format
-        for(int  x = 0, k = 0,l=0 ; x < height; ++x ) {
-            for(int  j = 0 ; j < width2 ; j++ ) {
-                if(j<=width2/2-1){
-                    outdata[x* width2 + j] = (float)fft[k][0];
-                    outdata[x * width2 + j+1] = (float)fft[k][1];
-                    j++;
-                    k++;
-                    l++;
-                }else{
-                    l--;
-                    outdata[x * width2 + j] = (float)fft[l][0];
-                    outdata[x * width2 + j+1] = (float)fft[l][1];
-                    j++;
-                }
-            }
-        }
-        cv::Mat tmp(height,width,CV_32FC2,outdata);
-        complex_result = tmp;
-
-#endif
-#if !defined OPENCV_CUFFT || !defined FFTW
-        cv::dft(input[i].mul(cos_window), complex_result, cv::DFT_COMPLEX_OUTPUT);
-#endif
-
-        result.set_channel(i, complex_result);
-    }
-#ifdef FFTW
-    // Destroy FFT plans and variables
-    #pragma omp critical
-{
-    fftwf_destroy_plan(plan_f);
-    fftwf_free(fft); /*fftwf_free(data_in);*/
-}
-#endif //FFTW
-    return result;
-}
-
-cv::Mat KCF_Tracker::ifft2(const ComplexMat &inputf)
-{
-#ifdef FFTW
-    // Prepare variables and FFTW plan for float precision IFFT
-    fftwf_complex *data_in;
-    float    *ifft;
-    fftwf_plan       plan_if;
-    int  width, height;
-#endif //FFTW
-    cv::Mat real_result;
-
-    if (inputf.n_channels == 1){
-#ifdef FFTW
-        cv::Mat input=inputf.to_cv_mat()  ;
-
-        width     = input.cols;
-        height    = input.rows;
-
-        float* outdata = new float[width * height];
-#pragma omp critical
-        {
-        data_in =  fftwf_alloc_complex(2*(width/2+1) * height);
-        ifft = fftwf_alloc_real(width * height);
-
-        plan_if=fftwf_plan_dft_c2r_2d( height , width , data_in , ifft ,  FFTW_MEASURE );
-        }
-        //Prepare input data
-        for(int x = 0,k=0; x< height; ++x) {
-            const float* row = input.ptr<float>(x);
-            for(int j = 0; j < width; j++) {
-                data_in[k][0]=(float)row[j];
-                data_in[k][1]=(float)row[j+1];
-
-                k++;
-                j++;
-            }
-        }
-
-        // Execute IFFT
-        fftwf_execute( plan_if );
-
-        // Get output data to right format
-        for(int x = 0,k=0; x< height; ++x) {
-            for(int j = 0; j < width; j++) {
-                outdata[k]=(float)ifft[x*width+j]/(float)(width*height);
-
-                k++;
-            }
-        }
-
-        cv::Mat  tmp(height,width,CV_32FC1,outdata);
-        real_result = tmp;
-        // Destroy FFTW plans and variables
-#pragma omp critical
-        {
-        fftwf_destroy_plan(plan_if);
-        fftwf_free(ifft); fftwf_free(data_in);
-        }
-#else
-        cv::dft(inputf.to_cv_mat(),real_result, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT | cv::DFT_SCALE);
-#endif //FFTW
-
-    } else {
-        std::vector<cv::Mat> mat_channels = inputf.to_cv_mat_vector();
-        std::vector<cv::Mat> ifft_mats(inputf.n_channels);
-#ifdef FFTW
-        width    = mat_channels[0].cols;
-        height    = mat_channels[0].rows;
-
-        float* outdata = new float[width * height];
-#pragma omp critical
-        {
-        data_in =  fftwf_alloc_complex(2*(width/2+1) * height);
-        ifft = fftwf_alloc_real(width * height);
-            
-        plan_if=fftwf_plan_dft_c2r_2d( height , width , data_in , ifft ,  FFTW_MEASURE );
-        }
-#endif //FFTW
-        for (int i = 0; i < inputf.n_channels; ++i) {
-#ifdef FFTW
-            //Prepare input data
-            for(int x = 0,k=0; x< height; ++x) {
-                const float* row = mat_channels[i].ptr<float>(x);
-                for(int j = 0; j < width; j++) {
-                    data_in[k][0]=(float)row[j];
-                    data_in[k][1]=(float)row[j+1];
-
-                    k++;
-                    j++;
-                }
-            }
-
-            // Execute IFFT
-            fftwf_execute( plan_if );
-
-            // Get output data to right format
-            for(int x = 0,k=0; x< height; ++x) {
-                for(int j = 0; j < width; j++) {
-                    outdata[k]=(float)ifft[x*width+j]/(float)(width*height);
-
-                    k++;
-                }
-            }
-
-            cv::Mat  tmp(height,width,CV_32FC1,outdata);
-
-            ifft_mats[i]=tmp;
-
-#else
-            cv::dft(mat_channels[i], ifft_mats[i], cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT | cv::DFT_SCALE);
-#endif //FFTW
-        }
-#ifdef FFTW
-        // Destroy FFTW plans and variables
-#pragma omp critical
-{
-        fftwf_destroy_plan(plan_if);
-        fftwf_free(ifft); fftwf_free(data_in);
-}
-#endif //FFTW
-        cv::merge(ifft_mats, real_result);
-    }
-    return real_result;
-}
-
-//hann window actually (Power-of-cosine windows)
+// hann window actually (Power-of-cosine windows)
 cv::Mat KCF_Tracker::cosine_window_function(int dim1, int dim2)
 {
     cv::Mat m1(1, dim1, CV_32FC1), m2(dim2, 1, CV_32FC1);
-    double N_inv = 1./(static_cast<double>(dim1)-1.);
+    double N_inv = 1. / (static_cast<double>(dim1) - 1.);
     for (int i = 0; i < dim1; ++i)
-        m1.at<float>(i) = 0.5*(1. - std::cos(2. * CV_PI * static_cast<double>(i) * N_inv));
-    N_inv = 1./(static_cast<double>(dim2)-1.);
+        m1.at<float>(i) = float(0.5 * (1. - std::cos(2. * CV_PI * static_cast<double>(i) * N_inv)));
+    N_inv = 1. / (static_cast<double>(dim2) - 1.);
     for (int i = 0; i < dim2; ++i)
-        m2.at<float>(i) = 0.5*(1. - std::cos(2. * CV_PI * static_cast<double>(i) * N_inv));
-    cv::Mat ret = m2*m1;
-#ifdef OPENCV_CUFFT
-    cv::cuda::createContinuous(cv::Size(ret.cols,ret.rows),CV_32FC1,p_cos_window_d);
-    p_cos_window_d.upload(ret);
-#endif
+        m2.at<float>(i) = float(0.5 * (1. - std::cos(2. * CV_PI * static_cast<double>(i) * N_inv)));
+    cv::Mat ret = m2 * m1;
     return ret;
 }
 
 // Returns sub-window of image input centered at [cx, cy] coordinates),
 // with size [width, height]. If any pixels are outside of the image,
 // they will replicate the values at the borders.
-cv::Mat KCF_Tracker::get_subwindow(const cv::Mat &input, int cx, int cy, int width, int height)
+cv::Mat KCF_Tracker::get_subwindow(const cv::Mat &input, int cx, int cy, int width, int height, double angle) const
 {
     cv::Mat patch;
 
-    int x1 = cx - width/2;
-    int y1 = cy - height/2;
-    int x2 = cx + width/2;
-    int y2 = cy + height/2;
+    cv::Size sz(width, height);
+    cv::RotatedRect rr(cv::Point2f(cx, cy), sz, angle);
+    cv::Rect bb = rr.boundingRect();
+
+    int x1 = bb.tl().x;
+    int y1 = bb.tl().y;
+    int x2 = bb.br().x;
+    int y2 = bb.br().y;
 
-    //out of image
+    // out of image
     if (x1 >= input.cols || y1 >= input.rows || x2 < 0 || y2 < 0) {
         patch.create(height, width, input.type());
-        patch.setTo(0.f);
+        patch.setTo(double(0.f));
         return patch;
     }
 
     int top = 0, bottom = 0, left = 0, right = 0;
 
-    //fit to image coordinates, set border extensions;
+    // fit to image coordinates, set border extensions;
     if (x1 < 0) {
         left = -x1;
         x1 = 0;
@@ -838,71 +724,87 @@ cv::Mat KCF_Tracker::get_subwindow(const cv::Mat &input, int cx, int cy, int wid
 
     if (x2 - x1 == 0 || y2 - y1 == 0)
         patch = cv::Mat::zeros(height, width, CV_32FC1);
-    else
-        {
-            cv::copyMakeBorder(input(cv::Range(y1, y2), cv::Range(x1, x2)), patch, top, bottom, left, right, cv::BORDER_REPLICATE);
-//      imshow( "copyMakeBorder", patch);
-//      cv::waitKey();
-        }
+    else {
+        cv::copyMakeBorder(input(cv::Range(y1, y2), cv::Range(x1, x2)), patch, top, bottom, left, right,
+                           cv::BORDER_REPLICATE);
+        //      imshow( "copyMakeBorder", patch);
+        //      cv::waitKey();
+    }
 
-    //sanity check
+    cv::Point2f src_pts[4];
+    cv::RotatedRect(cv::Point2f(patch.size()) / 2.0, sz, angle).points(src_pts);
+    cv::Point2f dst_pts[3] = { cv::Point2f(0, height), cv::Point2f(0, 0),  cv::Point2f(width, 0)};
+    auto rot = cv::getAffineTransform(src_pts, dst_pts);
+    cv::warpAffine(patch, patch, rot, sz);
+
+    // sanity check
     assert(patch.cols == width && patch.rows == height);
 
     return patch;
 }
 
-ComplexMat KCF_Tracker::gaussian_correlation(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)
 {
-    float xf_sqr_norm = xf.sqr_norm();
-    float yf_sqr_norm = auto_correlation ? xf_sqr_norm : yf.sqr_norm();
-
-    ComplexMat xyf = auto_correlation ? xf.sqr_mag() : xf * yf.conj();
-
-    //ifft2 and sum over 3rd dimension, we dont care about individual channels
-    cv::Mat xy_sum(xf.rows, xf.cols, CV_32FC1);
-    xy_sum.setTo(0);
-    cv::Mat ifft2_res = ifft2(xyf);
-    for (int y = 0; y < xf.rows; ++y) {
-        float * row_ptr = ifft2_res.ptr<float>(y);
-        float * row_ptr_sum = xy_sum.ptr<float>(y);
-        for (int x = 0; x < xf.cols; ++x){
-            row_ptr_sum[x] = std::accumulate((row_ptr + x*ifft2_res.channels()), (row_ptr + x*ifft2_res.channels() + ifft2_res.channels()), 0.f);
-        }
+    TRACE("");
+    DEBUG_PRINTM(xf);
+    DEBUG_PRINT(xf_sqr_norm.num_elem);
+    xf.sqr_norm(xf_sqr_norm);
+    for (uint s = 0; s < xf.n_scales; ++s)
+        DEBUG_PRINT(xf_sqr_norm[s]);
+    if (auto_correlation) {
+        yf_sqr_norm = xf_sqr_norm;
+    } else {
+        DEBUG_PRINTM(yf);
+        yf.sqr_norm(yf_sqr_norm);
+    }
+    for (uint s = 0; s < yf.n_scales; ++s)
+        DEBUG_PRINTM(yf_sqr_norm[s]);
+    xyf = auto_correlation ? xf.sqr_mag() : xf * yf.conj(); // xf.muln(yf.conj());
+    DEBUG_PRINTM(xyf);
+
+    // ifft2 and sum over 3rd dimension, we dont care about individual channels
+    ComplexMat xyf_sum = xyf.sum_over_channels();
+    DEBUG_PRINTM(xyf_sum);
+    kcf.fft.inverse(xyf_sum, ifft_res);
+    DEBUG_PRINTM(ifft_res);
+
+    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);
     }
 
-    float numel_xf_inv = 1.f/(xf.cols * xf.rows * xf.n_channels);
-    cv::Mat tmp;
-    cv::exp(- 1.f / (sigma * sigma) * cv::max((xf_sqr_norm + yf_sqr_norm - 2 * xy_sum) * numel_xf_inv, 0), tmp);
-
-    return fft2(tmp);
+    kcf.fft.forward(ifft_res, result);
 }
 
-float get_response_circular(cv::Point2i & pt, cv::Mat & response)
+float get_response_circular(cv::Point2i &pt, cv::Mat &response)
 {
     int x = pt.x;
     int y = pt.y;
-    if (x < 0)
-        x = response.cols + x;
-    if (y < 0)
-        y = response.rows + y;
-    if (x >= response.cols)
-        x = x - response.cols;
-    if (y >= response.rows)
-        y = y - response.rows;
-
-    return response.at<float>(y,x);
+    assert(response.dims == 2); // ensure .cols and .rows are valid
+    if (x < 0) x = response.cols + x;
+    if (y < 0) y = response.rows + y;
+    if (x >= response.cols) x = x - response.cols;
+    if (y >= response.rows) y = y - response.rows;
+
+    return response.at<float>(y, x);
 }
 
-cv::Point2f KCF_Tracker::sub_pixel_peak(cv::Point & max_loc, cv::Mat & response)
+cv::Point2f KCF_Tracker::sub_pixel_peak(cv::Point &max_loc, cv::Mat &response) const
 {
-    //find neighbourhood of max_loc (response is circular)
+    // find neighbourhood of max_loc (response is circular)
     // 1 2 3
     // 4   5
     // 6 7 8
-    cv::Point2i p1(max_loc.x-1, max_loc.y-1), p2(max_loc.x, max_loc.y-1), p3(max_loc.x+1, max_loc.y-1);
-    cv::Point2i p4(max_loc.x-1, max_loc.y), p5(max_loc.x+1, max_loc.y);
-    cv::Point2i p6(max_loc.x-1, max_loc.y+1), p7(max_loc.x, max_loc.y+1), p8(max_loc.x+1, max_loc.y+1);
+    cv::Point2i p1(max_loc.x - 1, max_loc.y - 1), p2(max_loc.x, max_loc.y - 1), p3(max_loc.x + 1, max_loc.y - 1);
+    cv::Point2i p4(max_loc.x - 1, max_loc.y), p5(max_loc.x + 1, max_loc.y);
+    cv::Point2i p6(max_loc.x - 1, max_loc.y + 1), p7(max_loc.x, max_loc.y + 1), p8(max_loc.x + 1, max_loc.y + 1);
 
+    // clang-format off
     // fit 2d quadratic function f(x, y) = a*x^2 + b*x*y + c*y^2 + d*x + e*y + f
     cv::Mat A = (cv::Mat_<float>(9, 6) <<
                  p1.x*p1.x, p1.x*p1.y, p1.y*p1.y, p1.x, p1.y, 1.f,
@@ -924,11 +826,11 @@ cv::Point2f KCF_Tracker::sub_pixel_peak(cv::Point & max_loc, cv::Mat & response)
                     get_response_circular(p7, response),
                     get_response_circular(p8, response),
                     get_response_circular(max_loc, response));
+    // clang-format on
     cv::Mat x;
     cv::solve(A, fval, x, cv::DECOMP_SVD);
 
-    double a = x.at<float>(0), b = x.at<float>(1), c = x.at<float>(2),
-           d = x.at<float>(3), e = x.at<float>(4);
+    float a = x.at<float>(0), b = x.at<float>(1), c = x.at<float>(2), d = x.at<float>(3), e = x.at<float>(4);
 
     cv::Point2f sub_peak(max_loc.x, max_loc.y);
     if (b > 0 || b < 0) {
@@ -939,35 +841,49 @@ 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, int index)
+double KCF_Tracker::sub_grid_scale(uint max_index)
 {
     cv::Mat A, fval;
-    if (index < 0 || index > (int)p_scales.size()-1) {
+    const auto &vec = d->IF_BIG_BATCH(threadctxs[0].max, threadctxs);
+    uint index = vec.getScaleIdx(max_index);
+    uint angle_idx = vec.getAngleIdx(max_index);
+
+    if (index >= vec.size()) {
         // 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, 0) = float(p_scales[i] * p_scales[i]);
+            A.at<float>(i, 1) = float(p_scales[i]);
             A.at<float>(i, 2) = 1;
-            fval.at<float>(i) = responses[i];
+            fval.at<float>(i) = d->IF_BIG_BATCH(threadctxs[0].max[i].response, threadctxs(i, angle_idx).max.response);
         }
     } else {
-        //only from neighbours
-        if (index == 0 || index == (int)p_scales.size()-1)
-            return p_scales[index];
+        // only from neighbours
+        if (index == 0 || index == 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]);
+             p_scales[index - 1] * p_scales[index - 1], p_scales[index - 1], 1,
+             p_scales[index + 0] * p_scales[index + 0], p_scales[index + 0], 1,
+             p_scales[index + 1] * p_scales[index + 1], p_scales[index + 1], 1);
+#ifdef BIG_BATCH
+        fval = (cv::Mat_<float>(3, 1) <<
+                d->threadctxs[0].max(index - 1, angle_idx).response,
+                d->threadctxs[0].max(index + 0, angle_idx).response,
+                d->threadctxs[0].max(index + 1, angle_idx).response);
+#else
+        fval = (cv::Mat_<float>(3, 1) <<
+                d->threadctxs(index - 1, angle_idx).max.response,
+                d->threadctxs(index + 0, angle_idx).max.response,
+                d->threadctxs(index + 1, angle_idx).max.response);
+#endif
     }
 
     cv::Mat x;
     cv::solve(A, fval, x, cv::DECOMP_SVD);
-    double a = x.at<float>(0), b = x.at<float>(1);
+    float a = x.at<float>(0), b = x.at<float>(1);
     double scale = p_scales[index];
     if (a > 0 || a < 0)
         scale = -b / (2 * a);