]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/complexmat.hpp
Added DynMem class
[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
9 #ifdef TEMPLATE_COMPLEXMAT
10 template <typename T> class ComplexMat_ {
11   public:
12     uint cols;
13     uint rows;
14     uint n_channels;
15     uint n_scales = 1;
16
17     ComplexMat_() : cols(0), rows(0), n_channels(0) {}
18     ComplexMat_(uint _rows, uint _cols, uint _n_channels) : cols(_cols), rows(_rows), n_channels(_n_channels)
19     {
20         p_data.resize(n_channels * cols * rows);
21     }
22
23     ComplexMat_(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
24         : cols(_cols), rows(_rows), n_channels(_n_channels), n_scales(_n_scales)
25     {
26         p_data.resize(n_channels * cols * rows);
27     }
28
29     // assuming that mat has 2 channels (real, img)
30     ComplexMat_(const cv::Mat &mat) : cols(uint(mat.cols)), rows(uint(mat.rows)), n_channels(1)
31     {
32         p_data = convert(mat);
33     }
34
35     void create(uint _rows, uint _cols, uint _n_channels)
36     {
37         rows = _rows;
38         cols = _cols;
39         n_channels = _n_channels;
40         p_data.resize(n_channels * cols * rows);
41     }
42
43     void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
44     {
45         rows = _rows;
46         cols = _cols;
47         n_channels = _n_channels;
48         n_scales = _n_scales;
49         p_data.resize(n_channels * cols * rows);
50     }
51     // cv::Mat API compatibility
52     cv::Size size() { return cv::Size(cols, rows); }
53     int channels() { return n_channels; }
54     int channels() const { return n_channels; }
55
56     // assuming that mat has 2 channels (real, imag)
57     void set_channel(int idx, const cv::Mat &mat)
58     {
59         assert(idx >= 0 && idx < n_channels);
60         for (int i = 0; i < rows; ++i) {
61             const std::complex<T> *row = mat.ptr<std::complex<T>>(i);
62             for (int j = 0; j < cols; ++j)
63                 p_data[idx * rows * cols + i * cols + j] = row[j];
64         }
65     }
66
67     T sqr_norm() const
68     {
69         int n_channels_per_scale = n_channels / n_scales;
70         T sum_sqr_norm = 0;
71         for (int i = 0; i < n_channels_per_scale; ++i) {
72             for (auto lhs = p_data.begin() + i * rows * cols; lhs != p_data.begin() + (i + 1) * rows * cols; ++lhs)
73                 sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
74         }
75         sum_sqr_norm = sum_sqr_norm / static_cast<T>(cols * rows);
76         return sum_sqr_norm;
77     }
78
79     void sqr_norm(T *sums_sqr_norms) const
80     {
81         int n_channels_per_scale = n_channels / n_scales;
82         int scale_offset = n_channels_per_scale * rows * cols;
83         T sum_sqr_norm;
84         for (uint scale = 0; scale < n_scales; ++scale) {
85             sum_sqr_norm = 0;
86             for (int i = 0; i < n_channels_per_scale; ++i)
87                 for (auto lhs = p_data.begin() + i * rows * cols + scale * scale_offset;
88                      lhs != p_data.begin() + (i + 1) * rows * cols + scale * scale_offset; ++lhs)
89                     sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
90             sums_sqr_norms[scale] = sum_sqr_norm / static_cast<T>(cols * rows);
91         }
92         return;
93     }
94
95     ComplexMat_<T> sqr_mag() const
96     {
97         return mat_const_operator([](std::complex<T> &c) { c = c.real() * c.real() + c.imag() * c.imag(); });
98     }
99
100     ComplexMat_<T> conj() const
101     {
102         return mat_const_operator([](std::complex<T> &c) { c = std::complex<T>(c.real(), -c.imag()); });
103     }
104
105     ComplexMat_<T> sum_over_channels() const
106     {
107         assert(p_data.size() > 1);
108
109         int n_channels_per_scale = n_channels / n_scales;
110         int scale_offset = n_channels_per_scale * rows * cols;
111
112         ComplexMat_<T> result(this->rows, this->cols, n_scales);
113         for (uint scale = 0; scale < n_scales; ++scale) {
114             std::copy(p_data.begin() + scale * scale_offset, p_data.begin() + rows * cols + scale * scale_offset,
115                       result.p_data.begin() + scale * rows * cols);
116             for (int i = 1; i < n_channels_per_scale; ++i) {
117                 std::transform(result.p_data.begin() + scale * rows * cols,
118                                result.p_data.begin() + (scale + 1) * rows * cols,
119                                p_data.begin() + i * rows * cols + scale * scale_offset,
120                                result.p_data.begin() + scale * rows * cols, std::plus<std::complex<T>>());
121             }
122         }
123         return result;
124     }
125
126     // return 2 channels (real, imag) for first complex channel
127     cv::Mat to_cv_mat() const
128     {
129         assert(p_data.size() >= 1);
130         return channel_to_cv_mat(0);
131     }
132     // return a vector of 2 channels (real, imag) per one complex channel
133     std::vector<cv::Mat> to_cv_mat_vector() const
134     {
135         std::vector<cv::Mat> result;
136         result.reserve(n_channels);
137
138         for (int i = 0; i < n_channels; ++i)
139             result.push_back(channel_to_cv_mat(i));
140
141         return result;
142     }
143
144     std::complex<T> *get_p_data() const { return p_data.data(); }
145
146     // element-wise per channel multiplication, division and addition
147     ComplexMat_<T> operator*(const ComplexMat_<T> &rhs) const
148     {
149         return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
150     }
151     ComplexMat_<T> operator/(const ComplexMat_<T> &rhs) const
152     {
153         return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs /= c_rhs; }, rhs);
154     }
155     ComplexMat_<T> operator+(const ComplexMat_<T> &rhs) const
156     {
157         return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs += c_rhs; }, rhs);
158     }
159
160     // multiplying or adding constant
161     ComplexMat_<T> operator*(const T &rhs) const
162     {
163         return mat_const_operator([&rhs](std::complex<T> &c) { c *= rhs; });
164     }
165     ComplexMat_<T> operator+(const T &rhs) const
166     {
167         return mat_const_operator([&rhs](std::complex<T> &c) { c += rhs; });
168     }
169
170     // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
171     ComplexMat_<T> mul(const ComplexMat_<T> &rhs) const
172     {
173         return matn_mat1_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
174     }
175
176     // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
177     ComplexMat_<T> mul2(const ComplexMat_<T> &rhs) const
178     {
179         return matn_mat2_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
180     }
181
182     // text output
183     friend std::ostream &operator<<(std::ostream &os, const ComplexMat_<T> &mat)
184     {
185         // for (int i = 0; i < mat.n_channels; ++i){
186         for (int i = 0; i < 1; ++i) {
187             os << "Channel " << i << std::endl;
188             for (uint j = 0; j < mat.rows; ++j) {
189                 for (uint k = 0; k < mat.cols - 1; ++k)
190                     os << mat.p_data[j * mat.cols + k] << ", ";
191                 os << mat.p_data[j * mat.cols + mat.cols - 1] << std::endl;
192             }
193         }
194         return os;
195     }
196
197   private:
198     mutable std::vector<std::complex<T>> p_data;
199
200     // convert 2 channel mat (real, imag) to vector row-by-row
201     std::vector<std::complex<T>> convert(const cv::Mat &mat)
202     {
203         std::vector<std::complex<T>> result;
204         result.reserve(mat.cols * mat.rows);
205         for (int y = 0; y < mat.rows; ++y) {
206             const T *row_ptr = mat.ptr<T>(y);
207             for (int x = 0; x < 2 * mat.cols; x += 2) {
208                 result.push_back(std::complex<T>(row_ptr[x], row_ptr[x + 1]));
209             }
210         }
211         return result;
212     }
213
214     ComplexMat_<T> mat_mat_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
215                                     const ComplexMat_<T> &mat_rhs) const
216     {
217         assert(mat_rhs.n_channels == n_channels && mat_rhs.cols == cols && mat_rhs.rows == rows);
218
219         ComplexMat_<T> result = *this;
220         for (uint i = 0; i < n_channels; ++i) {
221             auto lhs = result.p_data.begin() + i * rows * cols;
222             auto rhs = mat_rhs.p_data.begin() + i * rows * cols;
223             for (; lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs, ++rhs)
224                 op(*lhs, *rhs);
225         }
226
227         return result;
228     }
229     ComplexMat_<T> matn_mat1_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
230                                       const ComplexMat_<T> &mat_rhs) const
231     {
232         assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
233
234         ComplexMat_<T> result = *this;
235         for (uint i = 0; i < n_channels; ++i) {
236             auto lhs = result.p_data.begin() + i * rows * cols;
237             auto rhs = mat_rhs.p_data.begin();
238             for (; lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs, ++rhs)
239                 op(*lhs, *rhs);
240         }
241
242         return result;
243     }
244     ComplexMat_<T> matn_mat2_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
245                                       const ComplexMat_<T> &mat_rhs) const
246     {
247         assert(mat_rhs.n_channels == n_channels / n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
248
249         int n_channels_per_scale = n_channels / n_scales;
250         int scale_offset = n_channels_per_scale * rows * cols;
251         ComplexMat_<T> result = *this;
252         for (uint i = 0; i < n_scales; ++i) {
253             for (int j = 0; j < n_channels_per_scale; ++j) {
254                 auto lhs = result.p_data.begin() + (j * rows * cols) + (i * scale_offset);
255                 auto rhs = mat_rhs.p_data.begin() + (j * rows * cols);
256                 for (; lhs != result.p_data.begin() + ((j + 1) * rows * cols) + (i * scale_offset); ++lhs, ++rhs)
257                     op(*lhs, *rhs);
258             }
259         }
260
261         return result;
262     }
263     ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> &c_rhs)> &op) const
264     {
265         ComplexMat_<T> result = *this;
266         for (uint i = 0; i < n_channels; ++i)
267             for (auto lhs = result.p_data.begin() + i * rows * cols;
268                  lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs)
269                 op(*lhs);
270         return result;
271     }
272
273     cv::Mat channel_to_cv_mat(int channel_id) const
274     {
275         cv::Mat result(rows, cols, CV_32FC2);
276         for (int y = 0; y < rows; ++y) {
277             std::complex<T> *row_ptr = result.ptr<std::complex<T>>(y);
278             for (int x = 0; x < cols; ++x) {
279                 row_ptr[x] = p_data[channel_id * rows * cols + y * cols + x];
280             }
281         }
282         return result;
283     }
284 };
285
286 typedef ComplexMat_<float> ComplexMat;
287 #else
288 class ComplexMat {
289   public:
290     int cols;
291     int rows;
292     int n_channels;
293     int n_scales = 1;
294
295     ComplexMat();
296     ComplexMat(int _rows, int _cols, int _n_channels);
297     ComplexMat(int _rows, int _cols, int _n_channels, int _n_scales);
298     ComplexMat(const cv::Mat &mat);
299
300     void create(int _rows, int _cols, int _n_channels);
301
302     void create(int _rows, int _cols, int _n_channels, int _n_scales);
303     // cv::Mat API compatibility
304     cv::Size size();
305     int channels();
306     int channels() const;
307
308     // assuming that mat has 2 channels (real, imag)
309     void set_channel(int idx, const cv::Mat &mat);
310
311     float sqr_norm();
312     void sqr_norm(float *sums_sqr_norms) const;
313
314     ComplexMat sqr_mag() const;
315
316     ComplexMat conj() const;
317
318     ComplexMat sum_over_channels() const;
319
320     // return 2 channels (real, imag) for first complex channel
321     cv::Mat to_cv_mat() const;
322     // return a vector of 2 channels (real, imag) per one complex channel
323     std::vector<cv::Mat> to_cv_mat_vector() const;
324
325     std::complex<float> *get_p_data() const;
326
327     // element-wise per channel multiplication, division and addition
328     ComplexMat operator*(const ComplexMat &rhs) const;
329     ComplexMat operator/(const ComplexMat &rhs) const;
330     ComplexMat operator+(const ComplexMat &rhs) const;
331
332     // multiplying or adding constant
333     ComplexMat operator*(const float &rhs) const;
334     ComplexMat operator+(const float &rhs) const;
335
336     // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
337     ComplexMat mul(const ComplexMat &rhs) const;
338
339     // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
340     ComplexMat mul2(const ComplexMat &rhs) const;
341
342     // text output
343     friend std::ostream &operator<<(std::ostream &os, const ComplexMat &mat)
344     {
345         // for (int i = 0; i < mat.n_channels; ++i){
346         for (int i = 0; i < 1; ++i) {
347             os << "Channel " << i << std::endl;
348             for (int j = 0; j < mat.rows; ++j) {
349                 for (int k = 0; k < mat.cols - 1; ++k)
350                     os << mat.p_data[j * mat.cols + k] << ", ";
351                 os << mat.p_data[j * mat.cols + mat.cols - 1] << std::endl;
352             }
353         }
354         return os;
355     }
356
357   private:
358     mutable std::vector<std::complex<float>> p_data;
359
360     // convert 2 channel mat (real, imag) to vector row-by-row
361     std::vector<std::complex<float>> convert(const cv::Mat &mat);
362
363     ComplexMat mat_mat_operator(void (*op)(std::complex<float> &c_lhs, const std::complex<float> &c_rhs),
364                                 const ComplexMat &mat_rhs) const;
365     ComplexMat matn_mat1_operator(void (*op)(std::complex<float> &c_lhs, const std::complex<float> &c_rhs),
366                                   const ComplexMat &mat_rhs) const;
367     ComplexMat matn_mat2_operator(void (*op)(std::complex<float> &c_lhs, const std::complex<float> &c_rhs),
368                                   const ComplexMat &mat_rhs) const;
369     ComplexMat mat_const_operator(const std::function<void(std::complex<float> &c_rhs)> &op) const;
370
371     cv::Mat channel_to_cv_mat(int channel_id) const;
372 };
373 #endif
374
375 #endif // COMPLEX_MAT_HPP_213123048309482094