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_
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)
21 p_data.resize(n_channels*cols*rows);
25 //assuming that mat has 2 channels (real, img)
26 ComplexMat_(const cv::Mat & mat) : cols(mat.cols), rows(mat.rows), n_channels(1)
28 p_data = convert(mat);
31 void create(int _rows, int _cols, int _n_channels)
35 n_channels = _n_channels;
36 p_data.resize(n_channels*cols*rows);
39 void create(int _rows, int _cols, int _n_channels, int _n_scales)
43 n_channels = _n_channels;
45 p_data.resize(n_channels*cols*rows);
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; }
52 //assuming that mat has 2 channels (real, imag)
53 void set_channel(int idx, const cv::Mat & mat)
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];
64 void sqr_norm(T *sums_sqr_norms) const
66 int n_channels_per_scale = n_channels/n_scales;
67 int scale_offset = n_channels_per_scale*rows*cols;
69 for (int scale = 0; scale < n_scales; ++scale) {
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);
79 ComplexMat_<T> sqr_mag() const
81 return mat_const_operator( [](std::complex<T> & c) { c = c.real()*c.real() + c.imag()*c.imag(); } );
84 ComplexMat_<T> conj() const
86 return mat_const_operator( [](std::complex<T> & c) { c = std::complex<T>(c.real(), -c.imag()); } );
89 ComplexMat_<T> sum_over_channels() const
91 assert(p_data.size() > 1);
93 int n_channels_per_scale = n_channels/n_scales;
94 int scale_offset = n_channels_per_scale*rows*cols;
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>>());
107 //return 2 channels (real, imag) for first complex channel
108 cv::Mat to_cv_mat() const
110 assert(p_data.size() >= 1);
111 return channel_to_cv_mat(0);
113 // return a vector of 2 channels (real, imag) per one complex channel
114 std::vector<cv::Mat> to_cv_mat_vector() const
116 std::vector<cv::Mat> result;
117 result.reserve(n_channels);
119 for (int i = 0; i < n_channels; ++i)
120 result.push_back(channel_to_cv_mat(i));
125 std::complex<T>* get_p_data() const
127 return p_data.data();
130 //element-wise per channel multiplication, division and addition
131 ComplexMat_<T> operator*(const ComplexMat_<T> & rhs) const
133 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
135 ComplexMat_<T> operator/(const ComplexMat_<T> & rhs) const
137 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs /= c_rhs; }, rhs);
139 ComplexMat_<T> operator+(const ComplexMat_<T> & rhs) const
141 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs += c_rhs; }, rhs);
144 //multiplying or adding constant
145 ComplexMat_<T> operator*(const T & rhs) const
147 return mat_const_operator( [&rhs](std::complex<T> & c) { c *= rhs; });
149 ComplexMat_<T> operator+(const T & rhs) const
151 return mat_const_operator( [&rhs](std::complex<T> & c) { c += rhs; });
154 //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
155 ComplexMat_<T> mul(const ComplexMat_<T> & rhs) const
157 return matn_mat1_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
160 //multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
161 ComplexMat_<T> mul2(const ComplexMat_<T> & rhs) const
163 return matn_mat2_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
167 friend std::ostream & operator<<(std::ostream & os, const ComplexMat_<T> & mat)
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;
183 mutable std::vector<std::complex<T>> p_data;
185 //convert 2 channel mat (real, imag) to vector row-by-row
186 std::vector<std::complex<T>> convert(const cv::Mat & mat)
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]));
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
201 assert(mat_rhs.n_channels == n_channels && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
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
215 assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
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
229 assert(mat_rhs.n_channels == n_channels/n_scales && mat_rhs.cols == cols && mat_rhs.rows == rows);
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)
245 ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> & c_rhs)> & op) const
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)
254 cv::Mat channel_to_cv_mat(int channel_id) const
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];
268 typedef ComplexMat_<float> ComplexMat;
279 ComplexMat(int _rows, int _cols, int _n_channels);
280 ComplexMat(const cv::Mat & mat);
282 void create(int _rows, int _cols, int _n_channels);
284 void create(int _rows, int _cols, int _n_channels, int _n_scales);
285 // cv::Mat API compatibility
288 int channels() const;
290 //assuming that mat has 2 channels (real, imag)
291 void set_channel(int idx, const cv::Mat & mat);
294 void sqr_norm(float *sums_sqr_norms) const;
296 ComplexMat sqr_mag() const;
298 ComplexMat conj() const;
300 ComplexMat sum_over_channels() const;
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;
307 std::complex<float>* get_p_data() const;
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;
314 //multiplying or adding constant
315 ComplexMat operator*(const float & rhs) const;
316 ComplexMat operator+(const float & rhs) const;
318 //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
319 ComplexMat mul(const ComplexMat & rhs) const;
321 //multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
322 ComplexMat mul2(const ComplexMat & rhs) const;
325 friend std::ostream & operator<<(std::ostream & os, const ComplexMat & mat)
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;
341 mutable std::vector<std::complex<float>> p_data;
343 //convert 2 channel mat (real, imag) to vector row-by-row
344 std::vector<std::complex<float>> convert(const cv::Mat & mat);
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;
351 cv::Mat channel_to_cv_mat(int channel_id) const;
356 #endif //COMPLEX_MAT_HPP_213123048309482094