]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/complexmat.hpp
Unify CPU and GPU implementations of ComplexMat
[hercules2020/kcf.git] / src / complexmat.hpp
1 #ifndef COMPLEX_MAT_HPP_213123048309482094
2 #define COMPLEX_MAT_HPP_213123048309482094
3
4 #include <opencv2/opencv.hpp>
5 #include <vector>
6 #include <algorithm>
7 #include <functional>
8 #include "dynmem.hpp"
9
10 #ifdef CUFFT
11 #include <cufft.h>
12 #endif
13
14 class ComplexMat_ {
15   public:
16     typedef float T;
17
18     uint cols;
19     uint rows;
20     uint n_channels;
21     uint n_scales;
22
23     ComplexMat_(uint _rows, uint _cols, uint _n_channels, uint _n_scales = 1)
24         : cols(_cols), rows(_rows), n_channels(_n_channels * _n_scales), n_scales(_n_scales),
25           p_data(n_channels * cols * rows) {}
26     ComplexMat_(cv::Size size, uint _n_channels, uint _n_scales = 1)
27         : cols(size.width), rows(size.height), n_channels(_n_channels * _n_scales), n_scales(_n_scales)
28         , p_data(n_channels * cols * rows) {}
29
30     // assuming that mat has 2 channels (real, img)
31     ComplexMat_(const cv::Mat &mat) : cols(uint(mat.cols)), rows(uint(mat.rows)), n_channels(1), n_scales(1)
32                                     , p_data(n_channels * cols * rows)
33     {
34         memcpy(p_data.hostMem(), mat.ptr<std::complex<T>>(), mat.total() * mat.elemSize());
35     }
36
37     static ComplexMat_ same_size(const ComplexMat_ &o)
38     {
39         return ComplexMat_(o.rows, o.cols, o.n_channels / o.n_scales, o.n_scales);
40     }
41
42     // cv::Mat API compatibility
43     cv::Size size() const { return cv::Size(cols, rows); }
44     uint channels() const { return n_channels; }
45
46     // assuming that mat has 2 channels (real, imag)
47     void set_channel(uint idx, const cv::Mat &mat)
48     {
49         assert(idx < n_channels);
50         for (uint i = 0; i < rows; ++i) {
51             const std::complex<T> *row = mat.ptr<std::complex<T>>(i);
52             for (uint j = 0; j < cols; ++j)
53                 p_data.hostMem()[idx * rows * cols + i * cols + j] = row[j];
54         }
55     }
56
57     T sqr_norm() const;
58
59     void sqr_norm(DynMem_<T> &result) const;
60
61     ComplexMat_ sqr_mag() const;
62
63     ComplexMat_ conj() const;
64
65     ComplexMat_ sum_over_channels() const;
66
67     // return 2 channels (real, imag) for first complex channel
68     cv::Mat to_cv_mat() const
69     {
70         assert(p_data.num_elem >= 1);
71         return channel_to_cv_mat(0);
72     }
73     // return a vector of 2 channels (real, imag) per one complex channel
74     std::vector<cv::Mat> to_cv_mat_vector() const
75     {
76         std::vector<cv::Mat> result;
77         result.reserve(n_channels);
78
79         for (uint i = 0; i < n_channels; ++i)
80             result.push_back(channel_to_cv_mat(i));
81
82         return result;
83     }
84
85     std::complex<T> *get_p_data() { return p_data.hostMem(); }
86     const std::complex<T> *get_p_data() const { return p_data.hostMem(); }
87
88 #ifdef CUFFT
89     cufftComplex *get_dev_data() { return (cufftComplex*)p_data.deviceMem(); }
90     const cufftComplex *get_dev_data() const { return (cufftComplex*)p_data.deviceMem(); }
91 #endif
92
93     // element-wise per channel multiplication, division and addition
94     ComplexMat_ operator*(const ComplexMat_ &rhs) const;
95     ComplexMat_ operator/(const ComplexMat_ &rhs) const;
96     ComplexMat_ operator+(const ComplexMat_ &rhs) const;
97
98     // multiplying or adding constant
99     ComplexMat_ operator*(const T &rhs) const;
100     ComplexMat_ operator+(const T &rhs) const;
101
102     // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
103     ComplexMat_ mul(const ComplexMat_ &rhs) const;
104
105     // multiplying element-wise multichannel mats - same as operator*(ComplexMat), but without allocating memory for the result
106     ComplexMat_ muln(const ComplexMat_ &rhs) const
107     {
108         return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
109     }
110
111     // text output
112     friend std::ostream &operator<<(std::ostream &os, const ComplexMat_ &mat)
113     {
114         // for (int i = 0; i < mat.n_channels; ++i){
115         for (int i = 0; i < 1; ++i) {
116             os << "Channel " << i << std::endl;
117             for (uint j = 0; j < mat.rows; ++j) {
118                 for (uint k = 0; k < mat.cols - 1; ++k)
119                     os << mat.p_data[j * mat.cols + k] << ", ";
120                 os << mat.p_data[j * mat.cols + mat.cols - 1] << std::endl;
121             }
122         }
123         return os;
124     }
125
126   private:
127     DynMem_<std::complex<T>> p_data;
128
129     // convert 2 channel mat (real, imag) to vector row-by-row
130     std::vector<std::complex<T>> convert(const cv::Mat &mat)
131     {
132         std::vector<std::complex<T>> result;
133         result.reserve(mat.cols * mat.rows);
134         for (int y = 0; y < mat.rows; ++y) {
135             const T *row_ptr = mat.ptr<T>(y);
136             for (int x = 0; x < 2 * mat.cols; x += 2) {
137                 result.push_back(std::complex<T>(row_ptr[x], row_ptr[x + 1]));
138             }
139         }
140         return result;
141     }
142
143     ComplexMat_ mat_mat_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
144                                     const ComplexMat_ &mat_rhs) const
145     {
146         assert(mat_rhs.n_channels == n_channels/n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
147
148         ComplexMat_ result = *this;
149         for (uint s = 0; s < n_scales; ++s) {
150             auto lhs = result.p_data.hostMem() + (s * n_channels/n_scales * rows * cols);
151             auto rhs = mat_rhs.p_data.hostMem();
152             for (uint i = 0; i < n_channels/n_scales * rows * cols; ++i)
153                 op(*(lhs + i), *(rhs + i));
154         }
155
156         return result;
157     }
158     ComplexMat_ matn_mat1_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
159                                       const ComplexMat_ &mat_rhs) const
160     {
161         assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
162
163         ComplexMat_ result = *this;
164         for (uint i = 0; i < n_channels; ++i) {
165             auto lhs = result.p_data.hostMem() + i * rows * cols;
166             auto rhs = mat_rhs.p_data.hostMem();
167             for (; lhs != result.p_data.hostMem() + (i + 1) * rows * cols; ++lhs, ++rhs)
168                 op(*lhs, *rhs);
169         }
170
171         return result;
172     }
173     ComplexMat_ matn_mat2_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
174                                       const ComplexMat_ &mat_rhs) const
175     {
176         assert(mat_rhs.n_channels == n_channels / n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
177
178         int n_channels_per_scale = n_channels / n_scales;
179         int scale_offset = n_channels_per_scale * rows * cols;
180         ComplexMat_ result = *this;
181         for (uint i = 0; i < n_scales; ++i) {
182             for (int j = 0; j < n_channels_per_scale; ++j) {
183                 auto lhs = result.p_data.hostMem() + (j * rows * cols) + (i * scale_offset);
184                 auto rhs = mat_rhs.p_data.hostMem() + (j * rows * cols);
185                 for (; lhs != result.p_data.hostMem() + ((j + 1) * rows * cols) + (i * scale_offset); ++lhs, ++rhs)
186                     op(*lhs, *rhs);
187             }
188         }
189
190         return result;
191     }
192     ComplexMat_ mat_const_operator(const std::function<void(std::complex<T> &c_rhs)> &op) const
193     {
194         ComplexMat_ result = *this;
195         for (uint i = 0; i < n_channels; ++i)
196             for (auto lhs = result.p_data.hostMem() + i * rows * cols;
197                  lhs != result.p_data.hostMem() + (i + 1) * rows * cols; ++lhs)
198                 op(*lhs);
199         return result;
200     }
201
202     cv::Mat channel_to_cv_mat(int channel_id) const
203     {
204         cv::Mat result(rows, cols, CV_32FC2);
205         for (uint y = 0; y < rows; ++y) {
206             std::complex<T> *row_ptr = result.ptr<std::complex<T>>(y);
207             for (uint x = 0; x < cols; ++x) {
208                 row_ptr[x] = p_data[channel_id * rows * cols + y * cols + x];
209             }
210         }
211         return result;
212     }
213 };
214
215 typedef ComplexMat_ ComplexMat;
216
217 #endif // COMPLEX_MAT_HPP_213123048309482094