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