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