]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blobdiff - src/threadctx.hpp
Unify CPU and GPU implementations of ComplexMat
[hercules2020/kcf.git] / src / threadctx.hpp
index 6c460376f0cd171ec5797100c5e6b171a442e256..982905b96699b5928d39eb2ba158fc6920c2120f 100644 (file)
@@ -4,96 +4,63 @@
 #include <future>
 #include "dynmem.hpp"
 #include "kcf.h"
-
-#ifdef CUFFT
-#include "complexmat.cuh"
-#else
 #include "complexmat.hpp"
-#endif
 
 class KCF_Tracker;
 
 struct ThreadCtx {
   public:
-    ThreadCtx(cv::Size roi, uint num_channels, double scale, uint num_of_scales)
-        : scale(scale)
-        , gc(num_of_scales)
-    {
-        uint cells_size = roi.width * roi.height * sizeof(float);
-
-#if defined(CUFFT) || defined(FFTW)
-        this->gauss_corr_res = DynMem(cells_size * num_of_scales);
-        this->data_features = DynMem(cells_size * num_channels);
-
-        uint width_freq = roi.width / 2 + 1;
-
-        this->in_all = cv::Mat(roi.height * num_of_scales, roi.width, CV_32F, this->gauss_corr_res.hostMem());
-        this->fw_all = cv::Mat(roi.height * num_channels, roi.width, CV_32F, this->data_features.hostMem());
+    ThreadCtx(cv::Size roi, uint num_features
+#ifdef BIG_BATCH
+              , uint num_scales
 #else
-        uint width_freq = roi.width;
-
-        this->in_all = cv::Mat(roi, CV_32F);
+              , double scale
 #endif
-
-        this->data_i_features = DynMem(cells_size * num_channels);
-        this->data_i_1ch = DynMem(cells_size * num_of_scales);
-
-        this->ifft2_res = cv::Mat(roi, CV_32FC(num_channels), this->data_i_features.hostMem());
-        this->response = cv::Mat(roi, CV_32FC(num_of_scales), this->data_i_1ch.hostMem());
-
-#ifdef CUFFT
-        this->zf.create(roi.height, width_freq, num_channels, num_of_scales);
-        this->kzf.create(roi.height, width_freq, num_of_scales);
-        this->kf.create(roi.height, width_freq, num_of_scales);
-#else
-        this->zf.create(roi.height, width_freq, num_channels, num_of_scales);
-        this->kzf.create(roi.height, width_freq, num_of_scales);
-        this->kf.create(roi.height, width_freq, num_of_scales);
+             )
+        : roi(roi)
+        , num_features(num_features)
+        , num_scales(IF_BIG_BATCH(num_scales, 1))
+#ifndef BIG_BATCH
+        , scale(scale)
 #endif
+    {}
 
-#ifdef BIG_BATCH
-        if (num_of_scales > 1) {
-            this->max_responses.reserve(num_of_scales);
-            this->max_locs.reserve(num_of_scales);
-            this->response_maps.reserve(num_of_scales);
-        }
-#endif
-    }
     ThreadCtx(ThreadCtx &&) = default;
 
-    const double scale;
-#ifdef ASYNC
-    std::future<void> async_res;
-#endif
+    void track(const KCF_Tracker &kcf, cv::Mat &input_rgb, cv::Mat &input_gray);
+private:
+    cv::Size roi;
+    uint num_features;
+    uint num_scales;
+    cv::Size freq_size = Fft::freq_size(roi);
 
-    class gaussian_correlation_data {
-        friend void KCF_Tracker::gaussian_correlation(struct ThreadCtx &vars, const ComplexMat &xf, const ComplexMat &yf, double sigma, bool auto_correlation);
-        DynMem xf_sqr_norm;
-        DynMem yf_sqr_norm{sizeof(float)};
+    MatScaleFeats patch_feats{num_scales, num_features, roi};
+    MatScaleFeats temp{num_scales, num_features, roi};
 
-      public:
-        gaussian_correlation_data(uint num_of_scales) : xf_sqr_norm(num_of_scales * sizeof(float)) {}
-    } gc;
+    KCF_Tracker::GaussianCorrelation gaussian_correlation{num_scales, num_features, roi};
 
-    cv::Mat in_all, fw_all, ifft2_res, response;
-    ComplexMat zf, kzf, kf, xyf;
+    MatScales ifft2_res{num_scales, roi};
 
-    DynMem data_i_features, data_i_1ch;
-    // CuFFT and FFTW variables
-    DynMem gauss_corr_res, data_features;
+    ComplexMat zf{uint(freq_size.height), uint(freq_size.width), num_features, num_scales};
+    ComplexMat kzf{uint(freq_size.height), uint(freq_size.width), 1, num_scales};
 
-    // CuFFT variables
-    ComplexMat model_alphaf, model_xf;
+public:
+#ifdef ASYNC
+    std::future<void> async_res;
+#endif
 
-    // Variables used during non big batch mode and in big batch mode with ThreadCtx in p_threadctxs in kcf  on zero index.
-    cv::Point2i max_loc;
-    double max_val, max_response;
+    MatScales response{num_scales, roi};
+
+    struct Max {
+        cv::Point2i loc;
+        double response;
+    };
 
 #ifdef BIG_BATCH
-    // Stores value of responses, location of maximal response and response maps for each scale
-    std::vector<double> max_responses;
-    std::vector<cv::Point2i> max_locs;
-    std::vector<cv::Mat> response_maps;
+    std::vector<Max> max = std::vector<Max>(num_scales);
+#else
+    Max max;
+    const double scale;
 #endif
 };