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) {}
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);
28 ComplexMat_(cv::Size size, uint _n_channels)
29 : cols(size.width), rows(size.height), n_channels(_n_channels)
31 p_data.resize(n_channels * cols * rows);
34 // assuming that mat has 2 channels (real, img)
35 ComplexMat_(const cv::Mat &mat) : cols(uint(mat.cols)), rows(uint(mat.rows)), n_channels(1)
37 p_data = convert(mat);
40 void create(uint _rows, uint _cols, uint _n_channels)
44 n_channels = _n_channels;
45 p_data.resize(n_channels * cols * rows);
48 void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
52 n_channels = _n_channels;
54 p_data.resize(n_channels * cols * rows);
56 // cv::Mat API compatibility
57 cv::Size size() const { return cv::Size(cols, rows); }
58 uint channels() const { return n_channels; }
60 // assuming that mat has 2 channels (real, imag)
61 void set_channel(uint idx, const cv::Mat &mat)
63 assert(idx >= 0 && idx < n_channels);
64 for (uint i = 0; i < rows; ++i) {
65 const std::complex<T> *row = mat.ptr<std::complex<T>>(i);
66 for (uint j = 0; j < cols; ++j)
67 p_data[idx * rows * cols + i * cols + j] = row[j];
73 int n_channels_per_scale = n_channels / n_scales;
75 for (int i = 0; i < n_channels_per_scale; ++i) {
76 for (auto lhs = p_data.begin() + i * rows * cols; lhs != p_data.begin() + (i + 1) * rows * cols; ++lhs)
77 sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
79 sum_sqr_norm = sum_sqr_norm / static_cast<T>(cols * rows);
83 void sqr_norm(DynMem_<T> &result) const
85 int n_channels_per_scale = n_channels / n_scales;
86 int scale_offset = n_channels_per_scale * rows * cols;
87 for (uint scale = 0; scale < n_scales; ++scale) {
89 for (int i = 0; i < n_channels_per_scale; ++i)
90 for (auto lhs = p_data.begin() + i * rows * cols + scale * scale_offset;
91 lhs != p_data.begin() + (i + 1) * rows * cols + scale * scale_offset; ++lhs)
92 sum_sqr_norm += lhs->real() * lhs->real() + lhs->imag() * lhs->imag();
93 result.hostMem()[scale] = sum_sqr_norm / static_cast<T>(cols * rows);
98 ComplexMat_<T> sqr_mag() const
100 return mat_const_operator([](std::complex<T> &c) { c = c.real() * c.real() + c.imag() * c.imag(); });
103 ComplexMat_<T> conj() const
105 return mat_const_operator([](std::complex<T> &c) { c = std::complex<T>(c.real(), -c.imag()); });
108 ComplexMat_<T> sum_over_channels() const
110 assert(p_data.size() > 1);
112 int n_channels_per_scale = n_channels / n_scales;
113 int scale_offset = n_channels_per_scale * rows * cols;
115 ComplexMat_<T> result(this->rows, this->cols, n_scales);
116 for (uint scale = 0; scale < n_scales; ++scale) {
117 std::copy(p_data.begin() + scale * scale_offset, p_data.begin() + rows * cols + scale * scale_offset,
118 result.p_data.begin() + scale * rows * cols);
119 for (int i = 1; i < n_channels_per_scale; ++i) {
120 std::transform(result.p_data.begin() + scale * rows * cols,
121 result.p_data.begin() + (scale + 1) * rows * cols,
122 p_data.begin() + i * rows * cols + scale * scale_offset,
123 result.p_data.begin() + scale * rows * cols, std::plus<std::complex<T>>());
129 // return 2 channels (real, imag) for first complex channel
130 cv::Mat to_cv_mat() const
132 assert(p_data.size() >= 1);
133 return channel_to_cv_mat(0);
135 // return a vector of 2 channels (real, imag) per one complex channel
136 std::vector<cv::Mat> to_cv_mat_vector() const
138 std::vector<cv::Mat> result;
139 result.reserve(n_channels);
141 for (uint i = 0; i < n_channels; ++i)
142 result.push_back(channel_to_cv_mat(i));
147 std::complex<T> *get_p_data() { return p_data.data(); }
149 // element-wise per channel multiplication, division and addition
150 ComplexMat_<T> operator*(const ComplexMat_<T> &rhs) const
152 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
154 ComplexMat_<T> operator/(const ComplexMat_<T> &rhs) const
156 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs /= c_rhs; }, rhs);
158 ComplexMat_<T> operator+(const ComplexMat_<T> &rhs) const
160 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs += c_rhs; }, rhs);
163 // multiplying or adding constant
164 ComplexMat_<T> operator*(const T &rhs) const
166 return mat_const_operator([&rhs](std::complex<T> &c) { c *= rhs; });
168 ComplexMat_<T> operator+(const T &rhs) const
170 return mat_const_operator([&rhs](std::complex<T> &c) { c += rhs; });
173 // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
174 ComplexMat_<T> mul(const ComplexMat_<T> &rhs) const
176 return matn_mat1_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
179 // multiplying element-wise multichannel mats - same as operator*(ComplexMat), but without allocating memory for the result
180 ComplexMat_<T> muln(const ComplexMat_<T> &rhs) const
182 return mat_mat_operator([](std::complex<T> &c_lhs, const std::complex<T> &c_rhs) { c_lhs *= c_rhs; }, rhs);
186 friend std::ostream &operator<<(std::ostream &os, const ComplexMat_<T> &mat)
188 // for (int i = 0; i < mat.n_channels; ++i){
189 for (int i = 0; i < 1; ++i) {
190 os << "Channel " << i << std::endl;
191 for (uint j = 0; j < mat.rows; ++j) {
192 for (uint k = 0; k < mat.cols - 1; ++k)
193 os << mat.p_data[j * mat.cols + k] << ", ";
194 os << mat.p_data[j * mat.cols + mat.cols - 1] << std::endl;
201 std::vector<std::complex<T>> p_data;
203 // convert 2 channel mat (real, imag) to vector row-by-row
204 std::vector<std::complex<T>> convert(const cv::Mat &mat)
206 std::vector<std::complex<T>> result;
207 result.reserve(mat.cols * mat.rows);
208 for (int y = 0; y < mat.rows; ++y) {
209 const T *row_ptr = mat.ptr<T>(y);
210 for (int x = 0; x < 2 * mat.cols; x += 2) {
211 result.push_back(std::complex<T>(row_ptr[x], row_ptr[x + 1]));
217 ComplexMat_<T> mat_mat_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
218 const ComplexMat_<T> &mat_rhs) const
220 assert(mat_rhs.n_channels == n_channels && mat_rhs.cols == cols && mat_rhs.rows == rows);
222 ComplexMat_<T> result = *this;
223 for (uint i = 0; i < n_channels; ++i) {
224 auto lhs = result.p_data.begin() + i * rows * cols;
225 auto rhs = mat_rhs.p_data.begin() + i * rows * cols;
226 for (; lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs, ++rhs)
232 ComplexMat_<T> matn_mat1_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
233 const ComplexMat_<T> &mat_rhs) const
235 assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
237 ComplexMat_<T> result = *this;
238 for (uint i = 0; i < n_channels; ++i) {
239 auto lhs = result.p_data.begin() + i * rows * cols;
240 auto rhs = mat_rhs.p_data.begin();
241 for (; lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs, ++rhs)
247 ComplexMat_<T> matn_mat2_operator(void (*op)(std::complex<T> &c_lhs, const std::complex<T> &c_rhs),
248 const ComplexMat_<T> &mat_rhs) const
250 assert(mat_rhs.n_channels == n_channels / n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
252 int n_channels_per_scale = n_channels / n_scales;
253 int scale_offset = n_channels_per_scale * rows * cols;
254 ComplexMat_<T> result = *this;
255 for (uint i = 0; i < n_scales; ++i) {
256 for (int j = 0; j < n_channels_per_scale; ++j) {
257 auto lhs = result.p_data.begin() + (j * rows * cols) + (i * scale_offset);
258 auto rhs = mat_rhs.p_data.begin() + (j * rows * cols);
259 for (; lhs != result.p_data.begin() + ((j + 1) * rows * cols) + (i * scale_offset); ++lhs, ++rhs)
266 ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> &c_rhs)> &op) const
268 ComplexMat_<T> result = *this;
269 for (uint i = 0; i < n_channels; ++i)
270 for (auto lhs = result.p_data.begin() + i * rows * cols;
271 lhs != result.p_data.begin() + (i + 1) * rows * cols; ++lhs)
276 cv::Mat channel_to_cv_mat(int channel_id) const
278 cv::Mat result(rows, cols, CV_32FC2);
279 for (uint y = 0; y < rows; ++y) {
280 std::complex<T> *row_ptr = result.ptr<std::complex<T>>(y);
281 for (uint x = 0; x < cols; ++x) {
282 row_ptr[x] = p_data[channel_id * rows * cols + y * cols + x];
289 typedef ComplexMat_<float> ComplexMat;
291 #endif // COMPLEX_MAT_HPP_213123048309482094