]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/fft_opencv.cpp
Introduce FFT abstraction and move implementation to separate files
[hercules2020/kcf.git] / src / fft_opencv.cpp
1
2 #include "fft_opencv.h"
3
4 void FftOpencv::init(unsigned width, unsigned height)
5 {
6     (void)width;
7     (void)height;
8     std::cout << "FFT: OpenCV" << std::endl;
9 }
10
11 void FftOpencv::set_window(const cv::Mat &window)
12 {
13      m_window = window;
14 }
15
16 ComplexMat FftOpencv::forward(const cv::Mat &input)
17 {
18     cv::Mat complex_result;
19     cv::dft(input, complex_result, cv::DFT_COMPLEX_OUTPUT);
20     return ComplexMat(complex_result);
21 }
22
23 ComplexMat FftOpencv::forward_window(const std::vector<cv::Mat> &input)
24 {
25     int n_channels = input.size();
26     ComplexMat result(input[0].rows, input[0].cols, n_channels);
27
28     for (int i = 0; i < n_channels; ++i) {
29         cv::Mat complex_result;
30         cv::dft(input[i].mul(m_window), complex_result, cv::DFT_COMPLEX_OUTPUT);
31         result.set_channel(i, complex_result);
32     }
33     return result;
34 }
35
36 cv::Mat FftOpencv::inverse(const ComplexMat &inputf)
37 {
38     cv::Mat real_result;
39     if (inputf.n_channels == 1) {
40         cv::dft(inputf.to_cv_mat(), real_result, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT | cv::DFT_SCALE);
41     } else {
42         std::vector<cv::Mat> mat_channels = inputf.to_cv_mat_vector();
43         std::vector<cv::Mat> ifft_mats(inputf.n_channels);
44         for (int i = 0; i < inputf.n_channels; ++i) {
45             cv::dft(mat_channels[i], ifft_mats[i], cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT | cv::DFT_SCALE);
46         }
47         cv::merge(ifft_mats, real_result);
48     }
49     return real_result;
50 }
51
52 FftOpencv::~FftOpencv()
53 {
54
55 }