1 #ifndef COMPLEX_MAT_HPP_213123048309482094
2 #define COMPLEX_MAT_HPP_213123048309482094
4 #include <opencv2/opencv.hpp>
9 #ifdef TEMPLATE_COMPLEXMAT
10 template <typename T> class ComplexMat_ {
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)
20 p_data.resize(n_channels * cols * rows);
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)
26 p_data.resize(n_channels * cols * rows);
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); }
32 void create(uint _rows, uint _cols, uint _n_channels)
36 n_channels = _n_channels;
37 p_data.resize(n_channels * cols * rows);
40 void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
44 n_channels = _n_channels;
46 p_data.resize(n_channels * cols * rows);
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; }
53 // assuming that mat has 2 channels (real, imag)
54 void set_channel(int idx, const cv::Mat &mat)
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];
66 int n_channels_per_scale = n_channels / n_scales;
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();
72 sum_sqr_norm = sum_sqr_norm / static_cast<T>(cols * rows);
76 void sqr_norm(T *sums_sqr_norms) const
78 int n_channels_per_scale = n_channels / n_scales;
79 int scale_offset = n_channels_per_scale * rows * cols;
81 for (uint scale = 0; scale < n_scales; ++scale) {
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);
92 ComplexMat_<T> sqr_mag() const
94 return mat_const_operator([](std::complex<T> &c) { c = c.real() * c.real() + c.imag() * c.imag(); });
97 ComplexMat_<T> conj() const
99 return mat_const_operator([](std::complex<T> &c) { c = std::complex<T>(c.real(), -c.imag()); });
102 ComplexMat_<T> sum_over_channels() const
104 assert(p_data.size() > 1);
106 int n_channels_per_scale = n_channels / n_scales;
107 int scale_offset = n_channels_per_scale * rows * cols;
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>>());
123 // return 2 channels (real, imag) for first complex channel
124 cv::Mat to_cv_mat() const
126 assert(p_data.size() >= 1);
127 return channel_to_cv_mat(0);
129 // return a vector of 2 channels (real, imag) per one complex channel
130 std::vector<cv::Mat> to_cv_mat_vector() const
132 std::vector<cv::Mat> result;
133 result.reserve(n_channels);
135 for (int i = 0; i < n_channels; ++i)
136 result.push_back(channel_to_cv_mat(i));
141 std::complex<T> *get_p_data() const { return p_data.data(); }
143 // element-wise per channel multiplication, division and addition
144 ComplexMat_<T> operator*(const ComplexMat_<T> &rhs) const
146 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
148 ComplexMat_<T> operator/(const ComplexMat_<T> &rhs) const
150 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs /= c_rhs; }, rhs);
152 ComplexMat_<T> operator+(const ComplexMat_<T> &rhs) const
154 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs += c_rhs; }, rhs);
157 // multiplying or adding constant
158 ComplexMat_<T> operator*(const T &rhs) const
160 return mat_const_operator([&rhs](std::complex<T> &c) { c *= rhs; });
162 ComplexMat_<T> operator+(const T &rhs) const
164 return mat_const_operator([&rhs](std::complex<T> &c) { c += rhs; });
167 // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
168 ComplexMat_<T> mul(const ComplexMat_<T> &rhs) const
170 return matn_mat1_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
173 // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
174 ComplexMat_<T> mul2(const ComplexMat_<T> &rhs) const
176 return matn_mat2_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
180 friend std::ostream &operator<<(std::ostream &os, const ComplexMat_<T> &mat)
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;
195 mutable std::vector<std::complex<T>> p_data;
197 // convert 2 channel mat (real, imag) to vector row-by-row
198 std::vector<std::complex<T>> convert(const cv::Mat &mat)
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]));
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
214 assert(mat_rhs.n_channels == n_channels && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
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
229 assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
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
244 assert(mat_rhs.n_channels == n_channels / n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
260 ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> &c_rhs)> &op) const
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)
270 cv::Mat channel_to_cv_mat(int channel_id) const
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];
283 typedef ComplexMat_<float> 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);
297 void create(int _rows, int _cols, int _n_channels);
299 void create(int _rows, int _cols, int _n_channels, int _n_scales);
300 // cv::Mat API compatibility
303 int channels() const;
305 // assuming that mat has 2 channels (real, imag)
306 void set_channel(int idx, const cv::Mat &mat);
309 void sqr_norm(float *sums_sqr_norms) const;
311 ComplexMat sqr_mag() const;
313 ComplexMat conj() const;
315 ComplexMat sum_over_channels() const;
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;
322 std::complex<float> *get_p_data() const;
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;
329 // multiplying or adding constant
330 ComplexMat operator*(const float &rhs) const;
331 ComplexMat operator+(const float &rhs) const;
333 // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
334 ComplexMat mul(const ComplexMat &rhs) const;
336 // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
337 ComplexMat mul2(const ComplexMat &rhs) const;
340 friend std::ostream &operator<<(std::ostream &os, const ComplexMat &mat)
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;
355 mutable std::vector<std::complex<float>> p_data;
357 // convert 2 channel mat (real, imag) to vector row-by-row
358 std::vector<std::complex<float>> convert(const cv::Mat &mat);
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;
368 cv::Mat channel_to_cv_mat(int channel_id) const;
372 #endif // COMPLEX_MAT_HPP_213123048309482094