]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/commitdiff
Use new type MatDynMem combining cv::Mat and DynMem
authorMichal Sojka <michal.sojka@cvut.cz>
Mon, 17 Sep 2018 00:15:26 +0000 (02:15 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Thu, 20 Sep 2018 13:48:08 +0000 (15:48 +0200)
This simplifies ThreadCtx.

src/dynmem.hpp
src/threadctx.hpp

index c1d9bbb2ccd0df2c0c67e0c4d196243a3839db97..09e16e336d498bdb4cecadbdbfbc11903bb2e2bc 100644 (file)
@@ -2,6 +2,8 @@
 #define DYNMEM_HPP
 
 #include <cstdlib>
+#include <opencv2/opencv.hpp>
+#include <cassert>
 
 #if defined(CUFFT) || defined(CUFFTW)
 #include "cuda_runtime.h"
@@ -15,6 +17,8 @@ template <typename T> class DynMem_ {
     T *ptr_d = nullptr;
 
   public:
+    typedef T type;
+
     DynMem_()
     {}
     DynMem_(size_t size)
@@ -55,4 +59,15 @@ template <typename T> class DynMem_ {
     }
 };
 typedef DynMem_<float> DynMem;
+
+class MatDynMem : public DynMem, public cv::Mat {
+  public:
+    MatDynMem(cv::Size size, int type)
+        : DynMem(size.area() * sizeof(DynMem::type) * CV_MAT_CN(type)), cv::Mat(size, type, hostMem())
+    {
+        assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
+    }
+    MatDynMem(int height, int width, int type) { MatDynMem(cv::Size(width, height), type); }
+};
+
 #endif // DYNMEM_HPP
index fa95704a005fbb13cf14e71f79de4db44b05d732..d3bccbacec7b55f62b9c470f82e38a615655e73d 100644 (file)
@@ -15,67 +15,45 @@ 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);
-        cv::Size freq_size = Fft::freq_size(roi);
-
-#if defined(CUFFT) || defined(FFTW)
-        this->gauss_corr_res = DynMem(cells_size * num_of_scales);
-        this->data_features = DynMem(cells_size * num_channels);
-
-        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());
-#else
-        this->in_all = cv::Mat(roi, CV_32F);
+    ThreadCtx(cv::Size roi, uint num_channels, uint num_of_scales
+#ifndef BIG_BATCH
+              , 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());
-
-        this->zf.create(freq_size.height, freq_size.width, num_channels, num_of_scales);
-        this->kzf.create(freq_size.height, freq_size.width, num_of_scales);
-        this->kf.create(freq_size.height, freq_size.width, num_of_scales);
-
-#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);
-        }
+             )
+        : roi(roi)
+        , num_channels(num_channels)
+        , num_of_scales(num_of_scales)
+#ifndef BIG_BATCH
+        , scale(scale)
 #endif
-    }
+    {}
 
     ThreadCtx(ThreadCtx &&) = default;
 
-    const double scale;
+private:
+    cv::Size roi;
+    uint num_channels;
+    uint num_of_scales;
+    cv::Size freq_size = Fft::freq_size(roi);
+
+public:
 #ifdef ASYNC
     std::future<void> async_res;
 #endif
 
-    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)};
-
-      public:
-        gaussian_correlation_data(uint num_of_scales) : xf_sqr_norm(num_of_scales * sizeof(float)) {}
-    } gc;
-
-    cv::Mat in_all, fw_all, ifft2_res, response;
-    ComplexMat zf, kzf, kf, xyf;
+    KCF_Tracker::GaussianCorrelation gaussian_correlation{Fft::freq_size(roi), num_of_scales};
 
-    DynMem data_i_features, data_i_1ch;
-    // CuFFT and FFTW variables
-    DynMem gauss_corr_res, data_features;
+#if defined(CUFFT) || defined(FFTW) // TODO: Why this ifdef?
+    MatDynMem in_all{roi.height * int(num_of_scales), roi.width, CV_32F};
+#else
+    MatDynMem in_all{roi, CV_32F};
+#endif
+    MatDynMem fw_all{roi.height * int(num_channels), roi.width, CV_32F};
+    MatDynMem ifft2_res{roi, CV_32FC(num_channels)};
+    MatDynMem response{roi, CV_32FC(num_of_scales)};
 
-    // CuFFT variables
-    ComplexMat model_alphaf, model_xf;
+    ComplexMat zf{uint(freq_size.height), uint(freq_size.width), num_channels, num_of_scales};
+    ComplexMat kzf{uint(freq_size.height), uint(freq_size.width), num_of_scales};
 
     // 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;
@@ -83,9 +61,11 @@ struct ThreadCtx {
 
 #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<double> max_responses{num_of_scales};
+    std::vector<cv::Point2i> max_locs{num_of_scales};
+    std::vector<cv::Mat> response_maps{num_of_scales};
+#else
+    const double scale;
 #endif
 };