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