1 #ifndef COMPLEX_MAT_HPP_213123048309482094
2 #define COMPLEX_MAT_HPP_213123048309482094
4 #include <opencv2/opencv.hpp>
9 template<typename T> class ComplexMat_
16 ComplexMat_() : cols(0), rows(0), n_channels(0) {}
17 ComplexMat_(int _rows, int _cols, int _n_channels) : cols(_cols), rows(_rows), n_channels(_n_channels)
19 p_data.resize(n_channels);
22 //assuming that mat has 2 channels (real, img)
23 ComplexMat_(const cv::Mat & mat) : cols(mat.cols), rows(mat.rows), n_channels(1)
25 p_data.push_back(convert(mat));
28 //assuming that mat has 2 channels (real, imag)
29 void set_channel(int idx, const cv::Mat & mat)
31 assert(idx >= 0 && idx < n_channels);
32 p_data[idx] = convert(mat);
38 for (int i = 0; i < n_channels; ++i)
39 for (auto lhs = p_data[i].begin(); lhs != p_data[i].end(); ++lhs)
40 sum_sqr_norm += lhs->real()*lhs->real() + lhs->imag()*lhs->imag();
41 //std::for_each(p_data[i].begin(), p_data[i].end(), [&sum_sqr_norm](const std::complex<T> & c) { sum_sqr_norm += c.real()*c.real() + c.imag()*c.imag(); } );
42 return sum_sqr_norm / static_cast<T>(cols*rows);
45 ComplexMat_<T> sqr_mag() const
47 return mat_const_operator( [](std::complex<T> & c) { c = c.real()*c.real() + c.imag()*c.imag(); } );
50 ComplexMat_<T> conj() const
52 return mat_const_operator( [](std::complex<T> & c) { c = std::complex<T>(c.real(), -c.imag()); } );
55 ComplexMat_<T> sum_over_channels() const
57 assert(p_data.size() > 1);
58 ComplexMat_<T> result(this->rows, this->cols, 1);
59 result.p_data[0] = p_data[0];
60 for (int i = 1; i < n_channels; ++i) {
61 std::transform(result.p_data[0].begin(), result.p_data[0].end(), p_data[i].begin(), result.p_data[0].begin(), std::plus<std::complex<T>>());
66 //return 2 channels (real, imag) for first complex channel
67 cv::Mat to_cv_mat() const
69 assert(p_data.size() >= 1);
70 return channel_to_cv_mat(0);
72 //return a vector of 2 channels (real, imag) per one complex channel
73 std::vector<cv::Mat> to_cv_mat_vector() const
75 std::vector<cv::Mat> result;
76 result.reserve(n_channels);
78 for (int i = 0; i < n_channels; ++i)
79 result.push_back(channel_to_cv_mat(i));
84 //element-wise per channel multiplication, division and addition
85 ComplexMat_<T> operator*(const ComplexMat_<T> & rhs) const
87 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
89 ComplexMat_<T> operator/(const ComplexMat_<T> & rhs) const
91 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs /= c_rhs; }, rhs);
93 ComplexMat_<T> operator+(const ComplexMat_<T> & rhs) const
95 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs += c_rhs; }, rhs);
98 //multiplying or adding constant
99 ComplexMat_<T> operator*(const T & rhs) const
101 return mat_const_operator( [&rhs](std::complex<T> & c) { c *= rhs; });
103 ComplexMat_<T> operator+(const T & rhs) const
105 return mat_const_operator( [&rhs](std::complex<T> & c) { c += rhs; });
108 //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
109 ComplexMat_<T> mul(const ComplexMat_<T> & rhs) const
111 return matn_mat1_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
115 friend std::ostream & operator<<(std::ostream & os, const ComplexMat_<T> & mat)
117 //for (int i = 0; i < mat.n_channels; ++i){
118 for (int i = 0; i < 1; ++i){
119 os << "Channel " << i << std::endl;
120 for (int j = 0; j < mat.rows; ++j) {
121 for (int k = 0; k < mat.cols-1; ++k)
122 os << mat.p_data[i][j*mat.cols + k] << ", ";
123 os << mat.p_data[i][j*mat.cols + mat.cols-1] << std::endl;
131 std::vector<std::vector<std::complex<T>>> p_data;
133 //convert 2 channel mat (real, imag) to vector row-by-row
134 std::vector<std::complex<T>> convert(const cv::Mat & mat)
136 std::vector<std::complex<T>> result;
137 result.reserve(mat.cols*mat.rows);
138 for (int y = 0; y < mat.rows; ++y) {
139 const T * row_ptr = mat.ptr<T>(y);
140 for (int x = 0; x < 2*mat.cols; x += 2){
141 result.push_back(std::complex<T>(row_ptr[x], row_ptr[x+1]));
147 ComplexMat_<T> mat_mat_operator(void (*op)(std::complex<T> & c_lhs, const std::complex<T> & c_rhs), const ComplexMat_<T> & mat_rhs) const
149 assert(mat_rhs.n_channels == n_channels && mat_rhs.cols == cols && mat_rhs.rows == rows);
151 ComplexMat_<T> result = *this;
152 for (int i = 0; i < n_channels; ++i) {
153 auto lhs = result.p_data[i].begin();
154 auto rhs = mat_rhs.p_data[i].begin();
155 for ( ; lhs != result.p_data[i].end(); ++lhs, ++rhs)
161 ComplexMat_<T> matn_mat1_operator(void (*op)(std::complex<T> & c_lhs, const std::complex<T> & c_rhs), const ComplexMat_<T> & mat_rhs) const
163 assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
165 ComplexMat_<T> result = *this;
166 for (int i = 0; i < n_channels; ++i) {
167 auto lhs = result.p_data[i].begin();
168 auto rhs = mat_rhs.p_data[0].begin();
169 for ( ; lhs != result.p_data[i].end(); ++lhs, ++rhs)
175 ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> & c_rhs)> & op) const
177 ComplexMat_<T> result = *this;
178 for (int i = 0; i < n_channels; ++i)
179 for (auto lhs = result.p_data[i].begin(); lhs != result.p_data[i].end(); ++lhs)
184 cv::Mat channel_to_cv_mat(int channel_id) const
186 cv::Mat result(rows, cols, CV_32FC2);
188 for (int y = 0; y < rows; ++y) {
189 T * row_ptr = result.ptr<T>(y);
190 for (int x = 0; x < 2*cols; x += 2){
191 row_ptr[x] = p_data[channel_id][data_id].real();
192 row_ptr[x+1] = p_data[channel_id][data_id++].imag();
200 typedef ComplexMat_<float> ComplexMat;
203 #endif //COMPLEX_MAT_HPP_213123048309482094