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*cols*rows);
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 = convert(mat);
28 // cv::Mat API compatibility
29 cv::Size size() { return cv::Size(cols, rows); }
30 int channels() { return n_channels; }
32 //assuming that mat has 2 channels (real, imag)
33 void set_channel(int idx, const cv::Mat & mat)
35 assert(idx >= 0 && idx < n_channels);
36 for (int i = 0; i < rows; ++i){
37 const std::complex<T> *row = mat.ptr<std::complex<T>>(i);
38 for (int j = 0; j < cols; ++j)
39 p_data[idx*rows*cols+i*cols+j]=row[j];
46 for (int i = 0; i < n_channels; ++i)
47 for (auto lhs = p_data.begin()+i*rows*cols; lhs != p_data.begin()+(i+1)*rows*cols; ++lhs)
48 sum_sqr_norm += lhs->real()*lhs->real() + lhs->imag()*lhs->imag();
50 return sum_sqr_norm / static_cast<T>(cols*rows);
53 ComplexMat_<T> sqr_mag() const
55 return mat_const_operator( [](std::complex<T> & c) { c = c.real()*c.real() + c.imag()*c.imag(); } );
58 ComplexMat_<T> conj() const
60 return mat_const_operator( [](std::complex<T> & c) { c = std::complex<T>(c.real(), -c.imag()); } );
63 ComplexMat_<T> sum_over_channels() const
65 assert(p_data.size() > 1);
66 ComplexMat_<T> result(this->rows, this->cols, 1);
67 std::copy(p_data.begin(),p_data.begin()+rows*cols, result.p_data.begin());
68 std::cout << "TEST\n"<< *this << std::endl;
69 for (int i = 1; i < n_channels; ++i) {
70 std::transform(result.p_data.begin(), result.p_data.end(), p_data.begin()+i*rows*cols, result.p_data.begin(), std::plus<std::complex<T>>());
75 //return 2 channels (real, imag) for first complex channel
76 cv::Mat to_cv_mat() const
78 assert(p_data.size() >= 1);
79 return channel_to_cv_mat(0);
81 // return a vector of 2 channels (real, imag) per one complex channel
82 std::vector<cv::Mat> to_cv_mat_vector() const
84 std::vector<cv::Mat> result;
85 result.reserve(n_channels);
87 for (int i = 0; i < n_channels; ++i)
88 result.push_back(channel_to_cv_mat(i));
93 std::vector<std::complex<T>> get_p_data() const
98 //element-wise per channel multiplication, division and addition
99 ComplexMat_<T> operator*(const ComplexMat_<T> & rhs) const
101 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
103 ComplexMat_<T> operator/(const ComplexMat_<T> & rhs) const
105 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs /= c_rhs; }, rhs);
107 ComplexMat_<T> operator+(const ComplexMat_<T> & rhs) const
109 return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs += c_rhs; }, rhs);
112 //multiplying or adding constant
113 ComplexMat_<T> operator*(const T & rhs) const
115 return mat_const_operator( [&rhs](std::complex<T> & c) { c *= rhs; });
117 ComplexMat_<T> operator+(const T & rhs) const
119 return mat_const_operator( [&rhs](std::complex<T> & c) { c += rhs; });
122 //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
123 ComplexMat_<T> mul(const ComplexMat_<T> & rhs) const
125 return matn_mat1_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
129 friend std::ostream & operator<<(std::ostream & os, const ComplexMat_<T> & mat)
131 //for (int i = 0; i < mat.n_channels; ++i){
132 for (int i = 0; i < 1; ++i){
133 os << "Channel " << i << std::endl;
134 for (int j = 0; j < mat.rows; ++j) {
135 for (int k = 0; k < mat.cols-1; ++k)
136 os << mat.p_data[j*mat.cols + k] << ", ";
137 os << mat.p_data[j*mat.cols + mat.cols-1] << std::endl;
145 std::vector<std::complex<T>> p_data;
147 //convert 2 channel mat (real, imag) to vector row-by-row
148 std::vector<std::complex<T>> convert(const cv::Mat & mat)
150 std::vector<std::complex<T>> result;
151 result.reserve(mat.cols*mat.rows);
152 for (int y = 0; y < mat.rows; ++y) {
153 const T * row_ptr = mat.ptr<T>(y);
154 for (int x = 0; x < 2*mat.cols; x += 2){
155 result.push_back(std::complex<T>(row_ptr[x], row_ptr[x+1]));
161 ComplexMat_<T> mat_mat_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 == n_channels && 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.begin()+i*rows*cols;
168 auto rhs = mat_rhs.p_data.begin()+i*rows*cols;
169 for ( ; lhs != result.p_data.begin()+(i+1)*rows*cols; ++lhs, ++rhs)
175 ComplexMat_<T> matn_mat1_operator(void (*op)(std::complex<T> & c_lhs, const std::complex<T> & c_rhs), const ComplexMat_<T> & mat_rhs) const
177 assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
179 ComplexMat_<T> result = *this;
180 for (int i = 0; i < n_channels; ++i) {
181 auto lhs = result.p_data.begin()+i*rows*cols;
182 auto rhs = mat_rhs.p_data.begin();
183 for ( ; lhs != result.p_data.begin()+(i+1)*rows*cols; ++lhs, ++rhs)
189 ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> & c_rhs)> & op) const
191 ComplexMat_<T> result = *this;
192 for (int i = 0; i < n_channels; ++i)
193 for (auto lhs = result.p_data.begin()+i*rows*cols; lhs != result.p_data.begin()+(i+1)*rows*cols; ++lhs)
198 cv::Mat channel_to_cv_mat(int channel_id) const
200 cv::Mat result(rows, cols, CV_32FC2);
202 for (int y = 0; y < rows; ++y) {
203 std::complex<T> * row_ptr = result.ptr<std::complex<T>>(y);
204 for (int x = 0; x < cols; ++x){
205 row_ptr[x] = p_data[channel_id*rows*cols+y*cols+x];
213 typedef ComplexMat_<float> ComplexMat;
216 #endif //COMPLEX_MAT_HPP_213123048309482094