]> 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 d14edebd947e2cddfda21133cba3af692fab671a..f6aaef265985e41a0f0bdb46eb6b2176c61843bc 100644 (file)
 #include <vector>
 #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 = 1;
+    uint n_scales;
 
-    ComplexMat_() : cols(0), rows(0), n_channels(0) {}
-    ComplexMat_(uint _rows, uint _cols, uint _n_channels) : cols(_cols), rows(_rows), n_channels(_n_channels)
-    {
-        p_data.resize(n_channels * cols * rows);
-    }
-
-    ComplexMat_(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
-        : cols(_cols), rows(_rows), n_channels(_n_channels), n_scales(_n_scales)
-    {
-        p_data.resize(n_channels * cols * rows);
-    }
+    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(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(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)
+    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;
-        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;
-        p_data.resize(n_channels * cols * rows);
-    }
     // cv::Mat API compatibility
-    cv::Size size() { return cv::Size(cols, rows); }
-    int channels() { return n_channels; }
-    int channels() const { return n_channels; }
+    cv::Size size() const { return cv::Size(cols, rows); }
+    uint channels() const { return n_channels; }
 
     // 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];
-        }
-    }
-
-    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(T *sums_sqr_norms) const
-    {
-        int n_channels_per_scale = n_channels / n_scales;
-        int scale_offset = n_channels_per_scale * rows * cols;
-        T sum_sqr_norm;
-        for (uint scale = 0; scale < n_scales; ++scale) {
-            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();
-            sums_sqr_norms[scale] = sum_sqr_norm / static_cast<T>(cols * rows);
+                p_data.hostMem()[idx * rows * cols + i * cols + j] = row[j];
         }
-        return;
     }
 
-    ComplexMat_<T> sqr_mag() const
-    {
-        return mat_const_operator([](std::complex<T> &c) { c = c.real() * c.real() + c.imag() * c.imag(); });
-    }
+    T sqr_norm() const;
 
-    ComplexMat_<T> conj() const
-    {
-        return mat_const_operator([](std::complex<T> &c) { c = std::complex<T>(c.real(), -c.imag()); });
-    }
+    void sqr_norm(DynMem_<T> &result) const;
 
-    ComplexMat_<T> sum_over_channels() const
-    {
-        assert(p_data.size() > 1);
+    ComplexMat_ sqr_mag() const;
 
-        int n_channels_per_scale = n_channels / n_scales;
-        int scale_offset = n_channels_per_scale * rows * cols;
+    ComplexMat_ conj() const;
 
-        ComplexMat_<T> result(this->rows, this->cols, n_scales);
-        for (uint scale = 0; scale < n_scales; ++scale) {
-            std::copy(p_data.begin() + scale * scale_offset, p_data.begin() + rows * cols + scale * scale_offset,
-                      result.p_data.begin() + scale * rows * cols);
-            for (int i = 1; i < n_channels_per_scale; ++i) {
-                std::transform(result.p_data.begin() + scale * rows * cols,
-                               result.p_data.begin() + (scale + 1) * rows * cols,
-                               p_data.begin() + i * rows * cols + scale * scale_offset,
-                               result.p_data.begin() + scale * rows * cols, std::plus<std::complex<T>>());
-            }
-        }
-        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,46 +85,40 @@ template <typename T> class ComplexMat_ {
         return result;
     }
 
-    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);
+    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);
-    }
-    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 by one channel mats (rhs mat is with multiple channel)
-    ComplexMat_<T> mul2(const ComplexMat_<T> &rhs) const
+    // multiplying element-wise multichannel mats - same as operator*(ComplexMat), but without allocating memory for the result
+    ComplexMat_ muln(const ComplexMat_ &rhs) const
     {
-        return matn_mat2_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
+        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) {
@@ -194,7 +133,7 @@ template <typename T> class ComplexMat_ {
     }
 
   private:
-    mutable 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)
@@ -210,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 && 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() + i * rows * cols;
-            for (; lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs, ++rhs)
-                op(*lhs, *rhs);
-        }
-
-        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
     {
@@ -280,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