]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/complexmat.hpp
Merge branch 'master' of github.com:Shanigen/kcf
[hercules2020/kcf.git] / src / complexmat.hpp
1 #ifndef COMPLEX_MAT_HPP_213123048309482094
2 #define COMPLEX_MAT_HPP_213123048309482094
3
4 #include <opencv2/opencv.hpp>
5 #include <vector>
6 #include <algorithm>
7 #include <functional>
8
9 template<typename T> class ComplexMat_
10 {
11 public:
12     int cols;
13     int rows;
14     int n_channels;
15
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)
18     {
19         p_data.resize(n_channels*cols*rows);
20     }
21
22     //assuming that mat has 2 channels (real, img)
23     ComplexMat_(const cv::Mat & mat) : cols(mat.cols), rows(mat.rows), n_channels(1)
24     {
25         p_data = convert(mat);
26     }
27
28     // cv::Mat API compatibility
29     cv::Size size() { return cv::Size(cols, rows); }
30     int channels() { return n_channels; }
31
32     //assuming that mat has 2 channels (real, imag)
33     void set_channel(int idx, const cv::Mat & mat)
34     {
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];
40         }
41     }
42
43     T sqr_norm() const
44     {
45         T sum_sqr_norm = 0;
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();
49
50         return sum_sqr_norm / static_cast<T>(cols*rows);
51     }
52
53     ComplexMat_<T> sqr_mag() const
54     {
55         return mat_const_operator( [](std::complex<T> & c) { c = c.real()*c.real() + c.imag()*c.imag(); } );
56     }
57
58     ComplexMat_<T> conj() const
59     {
60         return mat_const_operator( [](std::complex<T> & c) { c = std::complex<T>(c.real(), -c.imag()); } );
61     }
62
63     ComplexMat_<T> sum_over_channels() const
64     {
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>>());
71         }
72         return result;
73     }
74
75     //return 2 channels (real, imag) for first complex channel
76     cv::Mat to_cv_mat() const
77     {
78         assert(p_data.size() >= 1);
79         return channel_to_cv_mat(0);
80     }
81     // return a vector of 2 channels (real, imag) per one complex channel
82     std::vector<cv::Mat> to_cv_mat_vector() const
83     {
84         std::vector<cv::Mat> result;
85         result.reserve(n_channels);
86
87         for (int i = 0; i < n_channels; ++i)
88             result.push_back(channel_to_cv_mat(i));
89
90         return result;
91     }
92
93     std::vector<std::complex<T>> get_p_data() const
94     {
95         return p_data;
96     }
97
98     //element-wise per channel multiplication, division and addition
99     ComplexMat_<T> operator*(const ComplexMat_<T> & rhs) const
100     {
101         return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
102     }
103     ComplexMat_<T> operator/(const ComplexMat_<T> & rhs) const
104     {
105         return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs /= c_rhs; }, rhs);
106     }
107     ComplexMat_<T> operator+(const ComplexMat_<T> & rhs) const
108     {
109         return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs)  { c_lhs += c_rhs; }, rhs);
110     }
111
112     //multiplying or adding constant
113     ComplexMat_<T> operator*(const T & rhs) const
114     {
115         return mat_const_operator( [&rhs](std::complex<T> & c) { c *= rhs; });
116     }
117     ComplexMat_<T> operator+(const T & rhs) const
118     {
119         return mat_const_operator( [&rhs](std::complex<T> & c) { c += rhs; });
120     }
121
122     //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
123     ComplexMat_<T> mul(const ComplexMat_<T> & rhs) const
124     {
125         return matn_mat1_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
126     }
127
128     //text output
129     friend std::ostream & operator<<(std::ostream & os, const ComplexMat_<T> & mat)
130     {
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;
138             }
139         }
140         return os;
141     }
142
143
144 private:
145     std::vector<std::complex<T>> p_data;
146
147     //convert 2 channel mat (real, imag) to vector row-by-row
148     std::vector<std::complex<T>> convert(const cv::Mat & mat)
149     {
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]));
156             }
157         }
158         return result;
159     }
160
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
162     {
163         assert(mat_rhs.n_channels == n_channels && mat_rhs.cols == cols && mat_rhs.rows == rows);
164
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)
170                 op(*lhs, *rhs);
171         }
172
173         return result;
174     }
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
176     {
177         assert(mat_rhs.n_channels == 1 && mat_rhs.cols == cols && mat_rhs.rows == rows);
178
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)
184                 op(*lhs, *rhs);
185         }
186
187         return result;
188     }
189     ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> & c_rhs)> & op) const
190     {
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)
194                 op(*lhs);
195         return result;
196     }
197
198     cv::Mat channel_to_cv_mat(int channel_id) const
199     {
200         cv::Mat result(rows, cols, CV_32FC2);
201         int data_id = 0;
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];
206             }
207         }
208         return result;
209     }
210
211 };
212
213 typedef ComplexMat_<float> ComplexMat;
214
215
216 #endif //COMPLEX_MAT_HPP_213123048309482094