]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/complexmat.hpp
added linear kernel for filter learning and tracking
[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);
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.push_back(convert(mat));
26     }
27
28     //assuming that mat has 2 channels (real, imag)
29     void set_channel(int idx, const cv::Mat & mat)
30     {
31         assert(idx >= 0 && idx < n_channels);
32         p_data[idx] = convert(mat);
33     }
34
35     T sqr_norm() const
36     {
37         T sum_sqr_norm = 0;
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);
43     }
44
45     ComplexMat_<T> sqr_mag() const
46     {
47         return mat_const_operator( [](std::complex<T> & c) { c = c.real()*c.real() + c.imag()*c.imag(); } );
48     }
49
50     ComplexMat_<T> conj() const
51     {
52         return mat_const_operator( [](std::complex<T> & c) { c = std::complex<T>(c.real(), -c.imag()); } );
53     }
54
55     ComplexMat_<T> sum_over_channels() const
56     {
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>>());
62         }
63         return result;
64     }
65
66     //return 2 channels (real, imag) for first complex channel
67     cv::Mat to_cv_mat() const
68     {
69         assert(p_data.size() >= 1);
70         return channel_to_cv_mat(0);
71     }
72     //return a vector of 2 channels (real, imag) per one complex channel
73     std::vector<cv::Mat> to_cv_mat_vector() const
74     {
75         std::vector<cv::Mat> result;
76         result.reserve(n_channels);
77
78         for (int i = 0; i < n_channels; ++i)
79             result.push_back(channel_to_cv_mat(i));
80
81         return result;
82     }
83
84     //element-wise per channel multiplication, division and addition
85     ComplexMat_<T> operator*(const ComplexMat_<T> & rhs) const
86     {
87         return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
88     }
89     ComplexMat_<T> operator/(const ComplexMat_<T> & rhs) const
90     {
91         return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs /= c_rhs; }, rhs);
92     }
93     ComplexMat_<T> operator+(const ComplexMat_<T> & rhs) const
94     {
95         return mat_mat_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs)  { c_lhs += c_rhs; }, rhs);
96     }
97
98     //multiplying or adding constant
99     ComplexMat_<T> operator*(const T & rhs) const
100     {
101         return mat_const_operator( [&rhs](std::complex<T> & c) { c *= rhs; });
102     }
103     ComplexMat_<T> operator+(const T & rhs) const
104     {
105         return mat_const_operator( [&rhs](std::complex<T> & c) { c += rhs; });
106     }
107
108     //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
109     ComplexMat_<T> mul(const ComplexMat_<T> & rhs) const
110     {
111         return matn_mat1_operator( [](std::complex<T> & c_lhs, const std::complex<T> & c_rhs) { c_lhs *= c_rhs; }, rhs);
112     }
113
114     //text output
115     friend std::ostream & operator<<(std::ostream & os, const ComplexMat_<T> & mat)
116     {
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;
124             }
125         }
126         return os;
127     }
128
129
130 private:
131     std::vector<std::vector<std::complex<T>>> p_data;
132
133     //convert 2 channel mat (real, imag) to vector row-by-row
134     std::vector<std::complex<T>> convert(const cv::Mat & mat)
135     {
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]));
142             }
143         }
144         return result;
145     }
146
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
148     {
149         assert(mat_rhs.n_channels == n_channels && mat_rhs.cols == cols && mat_rhs.rows == rows);
150
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)
156                 op(*lhs, *rhs);
157         }
158
159         return result;
160     }
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
162     {
163         assert(mat_rhs.n_channels == 1 && 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[i].begin();
168             auto rhs = mat_rhs.p_data[0].begin();
169             for ( ; lhs != result.p_data[i].end(); ++lhs, ++rhs)
170                 op(*lhs, *rhs);
171         }
172
173         return result;
174     }
175     ComplexMat_<T> mat_const_operator(const std::function<void(std::complex<T> & c_rhs)> & op) const
176     {
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)
180                 op(*lhs);
181         return result;
182     }
183
184     cv::Mat channel_to_cv_mat(int channel_id) const
185     {
186         cv::Mat result(rows, cols, CV_32FC2);
187         int data_id = 0;
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();
193             }
194         }
195         return result;
196     }
197
198 };
199
200 typedef ComplexMat_<float> ComplexMat;
201
202
203 #endif //COMPLEX_MAT_HPP_213123048309482094