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