]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blobdiff - src/complexmat.hpp
Take current angle into account when updating tracked object position
[hercules2020/kcf.git] / src / complexmat.hpp
index ce939353820b10d55fad5be41e5b0e2dbe92bc90..f6aaef265985e41a0f0bdb46eb6b2176c61843bc 100644 (file)
@@ -6,49 +6,41 @@
 #include <algorithm>
 #include <functional>
 #include "dynmem.hpp"
+#include "pragmas.h"
 
-template <typename T> class ComplexMat_ {
+#ifdef CUFFT
+#include <cufft.h>
+#endif
+
+class ComplexMat_ {
   public:
+    typedef float T;
+
     uint cols;
     uint rows;
     uint n_channels;
     uint n_scales;
 
-    ComplexMat_() : cols(0), rows(0), n_channels(0), n_scales(0) {}
     ComplexMat_(uint _rows, uint _cols, uint _n_channels, uint _n_scales = 1)
-        : cols(_cols), rows(_rows), n_channels(_n_channels * _n_scales), n_scales(_n_scales)
-    {
-        p_data.resize(n_channels * cols * rows);
-    }
+        : cols(_cols), rows(_rows), n_channels(_n_channels * _n_scales), n_scales(_n_scales),
+          p_data(n_channels * cols * rows) {}
     ComplexMat_(cv::Size size, uint _n_channels, uint _n_scales = 1)
         : cols(size.width), rows(size.height), n_channels(_n_channels * _n_scales), n_scales(_n_scales)
-    {
-        p_data.resize(n_channels * cols * rows);
-    }
+        , p_data(n_channels * cols * rows) {}
 
     // assuming that mat has 2 channels (real, img)
     ComplexMat_(const cv::Mat &mat) : cols(uint(mat.cols)), rows(uint(mat.rows)), n_channels(1), n_scales(1)
+                                    , p_data(n_channels * cols * rows)
     {
-        p_data = convert(mat);
+        cudaSync();
+        memcpy(p_data.hostMem(), mat.ptr<std::complex<T>>(), mat.total() * mat.elemSize());
     }
 
-    void create(uint _rows, uint _cols, uint _n_channels)
+    static ComplexMat_ same_size(const ComplexMat_ &o)
     {
-        rows = _rows;
-        cols = _cols;
-        n_channels = _n_channels;
-        n_scales = 1;
-        p_data.resize(n_channels * cols * rows);
+        return ComplexMat_(o.rows, o.cols, o.n_channels / o.n_scales, o.n_scales);
     }
 
-    void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
-    {
-        rows = _rows;
-        cols = _cols;
-        n_channels = _n_channels * _n_scales;
-        n_scales = _n_scales;
-        p_data.resize(n_channels * cols * rows);
-    }
     // cv::Mat API compatibility
     cv::Size size() const { return cv::Size(cols, rows); }
     uint channels() const { return n_channels; }
@@ -56,76 +48,29 @@ template <typename T> class ComplexMat_ {
     // assuming that mat has 2 channels (real, imag)
     void set_channel(uint idx, const cv::Mat &mat)
     {
-        assert(idx >= 0 && idx < n_channels);
+        assert(idx < n_channels);
+        cudaSync();
         for (uint i = 0; i < rows; ++i) {
             const std::complex<T> *row = mat.ptr<std::complex<T>>(i);
             for (uint j = 0; j < cols; ++j)
-                p_data[idx * rows * cols + i * cols + j] = row[j];
+                p_data.hostMem()[idx * rows * cols + i * cols + j] = row[j];
         }
     }
 
-    T sqr_norm() const
-    {
-        assert(n_scales == 1);
+    T sqr_norm() const;
 
-        int n_channels_per_scale = n_channels / n_scales;
-        T sum_sqr_norm = 0;
-        for (int i = 0; i < n_channels_per_scale; ++i) {
-            for (auto lhs = p_data.begin() + i * rows * cols; lhs != p_data.begin() + (i + 1) * rows * cols; ++lhs)
-                sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
-        }
-        sum_sqr_norm = sum_sqr_norm / static_cast<T>(cols * rows);
-        return sum_sqr_norm;
-    }
+    void sqr_norm(DynMem_<T> &result) const;
 
-    void sqr_norm(DynMem_<T> &result) const
-    {
-        int n_channels_per_scale = n_channels / n_scales;
-        int scale_offset = n_channels_per_scale * rows * cols;
-        for (uint scale = 0; scale < n_scales; ++scale) {
-            T sum_sqr_norm = 0;
-            for (int i = 0; i < n_channels_per_scale; ++i)
-                for (auto lhs = p_data.begin() + i * rows * cols + scale * scale_offset;
-                     lhs != p_data.begin() + (i + 1) * rows * cols + scale * scale_offset; ++lhs)
-                    sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
-            result.hostMem()[scale] = sum_sqr_norm / static_cast<T>(cols * rows);
-        }
-        return;
-    }
+    ComplexMat_ sqr_mag() const;
 
-    ComplexMat_<T> sqr_mag() const
-    {
-        return mat_const_operator([](std::complex<T> &c) { c = c.real() * c.real() + c.imag() * c.imag(); });
-    }
+    ComplexMat_ conj() const;
 
-    ComplexMat_<T> conj() const
-    {
-        return mat_const_operator([](std::complex<T> &c) { c = std::complex<T>(c.real(), -c.imag()); });
-    }
-
-    ComplexMat_<T> sum_over_channels() const
-    {
-        assert(p_data.size() == n_channels * rows * cols);
-
-        uint n_channels_per_scale = n_channels / n_scales;
-        uint scale_offset = n_channels_per_scale * rows * cols;
-
-        ComplexMat_<T> result(this->rows, this->cols, 1, n_scales);
-        for (uint scale = 0; scale < n_scales; ++scale) {
-            for (uint i = 0; i < rows * cols; ++i) {
-                std::complex<T> acc = 0;
-                for (uint ch = 0; ch < n_channels_per_scale; ++ch)
-                    acc +=  p_data[scale * scale_offset + i + ch * rows * cols];
-                result.p_data[scale * rows * cols + i] = acc;
-            }
-        }
-        return result;
-    }
+    ComplexMat_ sum_over_channels() const;
 
     // return 2 channels (real, imag) for first complex channel
     cv::Mat to_cv_mat() const
     {
-        assert(p_data.size() >= 1);
+        assert(p_data.num_elem >= 1);
         return channel_to_cv_mat(0);
     }
     // return a vector of 2 channels (real, imag) per one complex channel
@@ -140,47 +85,40 @@ template <typename T> class ComplexMat_ {
         return result;
     }
 
-    std::complex<T> *get_p_data() { return p_data.data(); }
-    const std::complex<T> *get_p_data() const { return p_data.data(); }
-
-    // element-wise per channel multiplication, division and addition
-    ComplexMat_<T> operator*(const ComplexMat_<T> &rhs) const
-    {
-        return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
-    }
-    ComplexMat_<T> operator/(const ComplexMat_<T> &rhs) const
-    {
-        return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs /= c_rhs; }, rhs);
+    std::complex<T> *get_p_data() {
+        cudaSync();
+        return p_data.hostMem();
     }
-    ComplexMat_<T> operator+(const ComplexMat_<T> &rhs) const
-    {
-        return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs += c_rhs; }, rhs);
+    const std::complex<T> *get_p_data() const {
+        cudaSync();
+        return p_data.hostMem();
     }
 
+#ifdef CUFFT
+    cufftComplex *get_dev_data() { return (cufftComplex*)p_data.deviceMem(); }
+    const cufftComplex *get_dev_data() const { return (cufftComplex*)p_data.deviceMem(); }
+#endif
+
+    // element-wise per channel multiplication, division and addition
+    ComplexMat_ operator*(const ComplexMat_ &rhs) const;
+    ComplexMat_ operator/(const ComplexMat_ &rhs) const;
+    ComplexMat_ operator+(const ComplexMat_ &rhs) const;
+
     // multiplying or adding constant
-    ComplexMat_<T> operator*(const T &rhs) const
-    {
-        return mat_const_operator([&rhs](std::complex<T> &c) { c *= rhs; });
-    }
-    ComplexMat_<T> operator+(const T &rhs) const
-    {
-        return mat_const_operator([&rhs](std::complex<T> &c) { c += rhs; });
-    }
+    ComplexMat_ operator*(const T &rhs) const;
+    ComplexMat_ operator+(const T &rhs) const;
 
     // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
-    ComplexMat_<T> mul(const ComplexMat_<T> &rhs) const
-    {
-        return matn_mat1_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
-    }
+    ComplexMat_ mul(const ComplexMat_ &rhs) const;
 
     // multiplying element-wise multichannel mats - same as operator*(ComplexMat), but without allocating memory for the result
-    ComplexMat_<T> muln(const ComplexMat_<T> &rhs) const
+    ComplexMat_ muln(const ComplexMat_ &rhs) const
     {
         return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
     }
 
     // text output
-    friend std::ostream &operator<<(std::ostream &os, const ComplexMat_<T> &mat)
+    friend std::ostream &operator<<(std::ostream &os, const ComplexMat_ &mat)
     {
         // for (int i = 0; i < mat.n_channels; ++i){
         for (int i = 0; i < 1; ++i) {
@@ -195,7 +133,7 @@ template <typename T> class ComplexMat_ {
     }
 
   private:
-    std::vector<std::complex<T>> p_data;
+    DynMem_<std::complex<T>> p_data;
 
     // convert 2 channel mat (real, imag) to vector row-by-row
     std::vector<std::complex<T>> convert(const cv::Mat &mat)
@@ -211,64 +149,13 @@ template <typename T> class ComplexMat_ {
         return result;
     }
 
-    ComplexMat_<T> mat_mat_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
-                                    const ComplexMat_<T> &mat_rhs) const
-    {
-        assert(mat_rhs.n_channels == n_channels/n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
-
-        ComplexMat_<T> result = *this;
-        for (uint s = 0; s < n_scales; ++s) {
-            auto lhs = result.p_data.begin() + (s * n_channels/n_scales * rows * cols);
-            auto rhs = mat_rhs.p_data.begin();
-            for (uint i = 0; i < n_channels/n_scales * rows * cols; ++i)
-                op(*(lhs + i), *(rhs + i));
-        }
-
-        return result;
-    }
-    ComplexMat_<T> matn_mat1_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
-                                      const ComplexMat_<T> &mat_rhs) const
-    {
-        assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
-
-        ComplexMat_<T> result = *this;
-        for (uint i = 0; i < n_channels; ++i) {
-            auto lhs = result.p_data.begin() + i * rows * cols;
-            auto rhs = mat_rhs.p_data.begin();
-            for (; lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs, ++rhs)
-                op(*lhs, *rhs);
-        }
-
-        return result;
-    }
-    ComplexMat_<T> matn_mat2_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
-                                      const ComplexMat_<T> &mat_rhs) const
-    {
-        assert(mat_rhs.n_channels == n_channels / n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
-
-        int n_channels_per_scale = n_channels / n_scales;
-        int scale_offset = n_channels_per_scale * rows * cols;
-        ComplexMat_<T> result = *this;
-        for (uint i = 0; i < n_scales; ++i) {
-            for (int j = 0; j < n_channels_per_scale; ++j) {
-                auto lhs = result.p_data.begin() + (j * rows * cols) + (i * scale_offset);
-                auto rhs = mat_rhs.p_data.begin() + (j * rows * cols);
-                for (; lhs != result.p_data.begin() + ((j + 1) * rows * cols) + (i * scale_offset); ++lhs, ++rhs)
-                    op(*lhs, *rhs);
-            }
-        }
-
-        return result;
-    }
-    ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> &c_rhs)> &op) const
-    {
-        ComplexMat_<T> result = *this;
-        for (uint i = 0; i < n_channels; ++i)
-            for (auto lhs = result.p_data.begin() + i * rows * cols;
-                 lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs)
-                op(*lhs);
-        return result;
-    }
+    ComplexMat_ mat_mat_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
+                                 const ComplexMat_ &mat_rhs) const;
+    ComplexMat_ matn_mat1_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
+                                   const ComplexMat_ &mat_rhs) const;
+    ComplexMat_ matn_mat2_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
+                                   const ComplexMat_ &mat_rhs) const;
+    ComplexMat_ mat_const_operator(const std::function<void(std::complex<T> &c_rhs)> &op) const;
 
     cv::Mat channel_to_cv_mat(int channel_id) const
     {
@@ -281,8 +168,14 @@ template <typename T> class ComplexMat_ {
         }
         return result;
     }
+
+#ifdef CUFFT
+    void cudaSync() const;
+#else
+    void cudaSync() const {}
+#endif
 };
 
-typedef ComplexMat_<float> ComplexMat;
+typedef ComplexMat_ ComplexMat;
 
 #endif // COMPLEX_MAT_HPP_213123048309482094