18 void Fftw::init(unsigned width, unsigned height)
23 #if defined(ASYNC) || defined(OPENMP)
28 std::cout << "FFT: FFTW" << std::endl;
30 std::cout << "FFT: cuFFTW" << std::endl;
34 void Fftw::set_window(const cv::Mat &window)
39 ComplexMat Fftw::forward(const cv::Mat &input)
41 cv::Mat complex_result(m_height, m_width / 2 + 1, CV_32FC2);
43 std::unique_lock<std::mutex> lock(fftw_mut);
44 fftw_plan_with_nthreads(2);
46 fftwf_plan plan = fftwf_plan_dft_r2c_2d(m_height, m_width,
47 reinterpret_cast<float*>(input.data),
48 reinterpret_cast<fftwf_complex*>(complex_result.data),
57 fftwf_destroy_plan(plan);
61 return ComplexMat(complex_result);
64 ComplexMat Fftw::forward_window(const std::vector<cv::Mat> &input)
66 int n_channels = input.size();
67 cv::Mat in_all(m_height * n_channels, m_width, CV_32F);
68 for (int i = 0; i < n_channels; ++i) {
69 cv::Mat in_roi(in_all, cv::Rect(0, i*m_height, m_width, m_height));
70 in_roi = input[i].mul(m_window);
72 cv::Mat complex_result(n_channels*m_height, m_width/2+1, CV_32FC2);
75 int n[] = {(int)m_height, (int)m_width};
76 int howmany = n_channels;
77 int idist = m_height*m_width, odist = m_height*(m_width/2+1);
78 int istride = 1, ostride = 1;
79 int *inembed = NULL, *onembed = NULL;
80 float *in = reinterpret_cast<float*>(in_all.data);
81 fftwf_complex *out = reinterpret_cast<fftwf_complex*>(complex_result.data);
83 std::unique_lock<std::mutex> lock(fftw_mut);
84 fftw_plan_with_nthreads(2);
86 fftwf_plan plan = fftwf_plan_many_dft_r2c(rank, n, howmany,
87 in, inembed, istride, idist,
88 out, onembed, ostride, odist,
97 fftwf_destroy_plan(plan);
102 ComplexMat result(m_height, m_width/2 + 1, n_channels);
103 for (int i = 0; i < n_channels; ++i)
104 result.set_channel(i, complex_result(cv::Rect(0, i*m_height, m_width/2+1, m_height)));
109 cv::Mat Fftw::inverse(const ComplexMat &inputf)
111 int n_channels = inputf.n_channels;
112 cv::Mat real_result(m_height, m_width, CV_32FC(n_channels));
113 cv::Mat complex_vconcat = inputf.to_vconcat_mat();
116 int n[] = {(int)m_height, (int)m_width};
117 int howmany = n_channels;
118 int idist = m_height*(m_width/2+1), odist = 1;
119 int istride = 1, ostride = n_channels;
120 int inembed[] = {(int)m_height, (int)m_width/2+1}, *onembed = n;
121 fftwf_complex *in = reinterpret_cast<fftwf_complex*>(complex_vconcat.data);
122 float *out = reinterpret_cast<float*>(real_result.data);
123 #if defined(ASYNC) || defined(OPENMP)
124 std::unique_lock<std::mutex> lock(fftw_mut);
125 fftw_plan_with_nthreads(2);
127 fftwf_plan plan = fftwf_plan_many_dft_c2r(rank, n, howmany,
128 in, inembed, istride, idist,
129 out, onembed, ostride, odist,
138 fftwf_destroy_plan(plan);
142 return real_result/(m_width*m_height);