1 #ifndef COMPLEX_MAT_HPP_213123048309482094
2 #define COMPLEX_MAT_HPP_213123048309482094
4 #include <opencv2/opencv.hpp>
10 template <typename T> class ComplexMat_ {
17 ComplexMat_() : cols(0), rows(0), n_channels(0), n_scales(0) {}
18 ComplexMat_(uint _rows, uint _cols, uint _n_channels, uint _n_scales = 1)
19 : cols(_cols), rows(_rows), n_channels(_n_channels * _n_scales), n_scales(_n_scales)
21 p_data.resize(n_channels * cols * rows);
23 ComplexMat_(cv::Size size, uint _n_channels, uint _n_scales = 1)
24 : cols(size.width), rows(size.height), n_channels(_n_channels * _n_scales), 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), n_scales(1)
32 p_data = convert(mat);
35 void create(uint _rows, uint _cols, uint _n_channels)
39 n_channels = _n_channels;
41 p_data.resize(n_channels * cols * rows);
44 void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
48 n_channels = _n_channels * _n_scales;
50 p_data.resize(n_channels * cols * rows);
52 // cv::Mat API compatibility
53 cv::Size size() const { return cv::Size(cols, rows); }
54 uint channels() const { return n_channels; }
56 // assuming that mat has 2 channels (real, imag)
57 void set_channel(uint idx, const cv::Mat &mat)
59 assert(idx >= 0 && idx < n_channels);
60 for (uint i = 0; i < rows; ++i) {
61 const std::complex<T> *row = mat.ptr<std::complex<T>>(i);
62 for (uint j = 0; j < cols; ++j)
63 p_data[idx * rows * cols + i * cols + j] = row[j];
69 assert(n_scales == 1);
71 int n_channels_per_scale = n_channels / n_scales;
73 for (int i = 0; i < n_channels_per_scale; ++i) {
74 for (auto lhs = p_data.begin() + i * rows * cols; lhs != p_data.begin() + (i + 1) * rows * cols; ++lhs)
75 sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
77 sum_sqr_norm = sum_sqr_norm / static_cast<T>(cols * rows);
81 void sqr_norm(DynMem_<T> &result) const
83 int n_channels_per_scale = n_channels / n_scales;
84 int scale_offset = n_channels_per_scale * rows * cols;
85 for (uint scale = 0; scale < n_scales; ++scale) {
87 for (int i = 0; i < n_channels_per_scale; ++i)
88 for (auto lhs = p_data.begin() + i * rows * cols + scale * scale_offset;
89 lhs != p_data.begin() + (i + 1) * rows * cols + scale * scale_offset; ++lhs)
90 sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
91 result.hostMem()[scale] = sum_sqr_norm / static_cast<T>(cols * rows);
96 ComplexMat_<T> sqr_mag() const
98 return mat_const_operator([](std::complex<T> &c) { c = c.real() * c.real() + c.imag() * c.imag(); });
101 ComplexMat_<T> conj() const
103 return mat_const_operator([](std::complex<T> &c) { c = std::complex<T>(c.real(), -c.imag()); });
106 ComplexMat_<T> sum_over_channels() const
108 assert(p_data.size() == n_channels * rows * cols);
110 uint n_channels_per_scale = n_channels / n_scales;
111 uint scale_offset = n_channels_per_scale * rows * cols;
113 ComplexMat_<T> result(this->rows, this->cols, 1, n_scales);
114 for (uint scale = 0; scale < n_scales; ++scale) {
115 for (uint i = 0; i < rows * cols; ++i) {
116 std::complex<T> acc = 0;
117 for (uint ch = 0; ch < n_channels_per_scale; ++ch)
118 acc += p_data[scale * scale_offset + i + ch * rows * cols];
119 result.p_data[scale * rows * cols + i] = acc;
125 // return 2 channels (real, imag) for first complex channel
126 cv::Mat to_cv_mat() const
128 assert(p_data.size() >= 1);
129 return channel_to_cv_mat(0);
131 // return a vector of 2 channels (real, imag) per one complex channel
132 std::vector<cv::Mat> to_cv_mat_vector() const
134 std::vector<cv::Mat> result;
135 result.reserve(n_channels);
137 for (uint i = 0; i < n_channels; ++i)
138 result.push_back(channel_to_cv_mat(i));
143 std::complex<T> *get_p_data() { return p_data.data(); }
144 const std::complex<T> *get_p_data() const { return p_data.data(); }
146 // element-wise per channel multiplication, division and addition
147 ComplexMat_<T> operator*(const ComplexMat_<T> &rhs) const
149 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
151 ComplexMat_<T> operator/(const ComplexMat_<T> &rhs) const
153 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs /= c_rhs; }, rhs);
155 ComplexMat_<T> operator+(const ComplexMat_<T> &rhs) const
157 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs += c_rhs; }, rhs);
160 // multiplying or adding constant
161 ComplexMat_<T> operator*(const T &rhs) const
163 return mat_const_operator([&rhs](std::complex<T> &c) { c *= rhs; });
165 ComplexMat_<T> operator+(const T &rhs) const
167 return mat_const_operator([&rhs](std::complex<T> &c) { c += rhs; });
170 // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
171 ComplexMat_<T> mul(const ComplexMat_<T> &rhs) const
173 return matn_mat1_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
176 // multiplying element-wise multichannel mats - same as operator*(ComplexMat), but without allocating memory for the result
177 ComplexMat_<T> muln(const ComplexMat_<T> &rhs) const
179 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
183 friend std::ostream &operator<<(std::ostream &os, const ComplexMat_<T> &mat)
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;
198 std::vector<std::complex<T>> p_data;
200 // convert 2 channel mat (real, imag) to vector row-by-row
201 std::vector<std::complex<T>> convert(const cv::Mat &mat)
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]));
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
217 assert(mat_rhs.n_channels == n_channels/n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
219 ComplexMat_<T> result = *this;
220 for (uint s = 0; s < n_scales; ++s) {
221 auto lhs = result.p_data.begin() + (s * n_channels/n_scales * rows * cols);
222 auto rhs = mat_rhs.p_data.begin();
223 for (uint i = 0; i < n_channels/n_scales * rows * cols; ++i)
224 op(*(lhs + i), *(rhs + i));
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
232 assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
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
247 assert(mat_rhs.n_channels == n_channels / n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
263 ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> &c_rhs)> &op) const
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)
273 cv::Mat channel_to_cv_mat(int channel_id) const
275 cv::Mat result(rows, cols, CV_32FC2);
276 for (uint y = 0; y < rows; ++y) {
277 std::complex<T> *row_ptr = result.ptr<std::complex<T>>(y);
278 for (uint x = 0; x < cols; ++x) {
279 row_ptr[x] = p_data[channel_id * rows * cols + y * cols + x];
286 typedef ComplexMat_<float> ComplexMat;
288 #endif // COMPLEX_MAT_HPP_213123048309482094