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);
47 fftw_plan_with_nthreads(omp_get_max_threads());
51 plan = fftwf_plan_dft_r2c_2d(m_height, m_width,
52 reinterpret_cast<float*>(input.data),
53 reinterpret_cast<fftwf_complex*>(complex_result.data),
63 fftwf_destroy_plan(plan);
67 return ComplexMat(complex_result);
70 ComplexMat Fftw::forward_window(const std::vector<cv::Mat> &input)
72 int n_channels = input.size();
73 cv::Mat in_all(m_height * n_channels, m_width, CV_32F);
74 for (int i = 0; i < n_channels; ++i) {
75 cv::Mat in_roi(in_all, cv::Rect(0, i*m_height, m_width, m_height));
76 in_roi = input[i].mul(m_window);
78 cv::Mat complex_result(n_channels*m_height, m_width/2+1, CV_32FC2);
81 int n[] = {(int)m_height, (int)m_width};
82 int howmany = n_channels;
83 int idist = m_height*m_width, odist = m_height*(m_width/2+1);
84 int istride = 1, ostride = 1;
85 int *inembed = NULL, *onembed = NULL;
86 float *in = reinterpret_cast<float*>(in_all.data);
87 fftwf_complex *out = reinterpret_cast<fftwf_complex*>(complex_result.data);
89 std::unique_lock<std::mutex> lock(fftw_mut);
90 fftw_plan_with_nthreads(2);
93 fftw_plan_with_nthreads(omp_get_max_threads());
97 plan = fftwf_plan_many_dft_r2c(rank, n, howmany,
98 in, inembed, istride, idist,
99 out, onembed, ostride, odist,
109 fftwf_destroy_plan(plan);
114 ComplexMat result(m_height, m_width/2 + 1, n_channels);
115 for (int i = 0; i < n_channels; ++i)
116 result.set_channel(i, complex_result(cv::Rect(0, i*m_height, m_width/2+1, m_height)));
121 cv::Mat Fftw::inverse(const ComplexMat &inputf)
123 int n_channels = inputf.n_channels;
124 cv::Mat real_result(m_height, m_width, CV_32FC(n_channels));
125 cv::Mat complex_vconcat = inputf.to_vconcat_mat();
128 int n[] = {(int)m_height, (int)m_width};
129 int howmany = n_channels;
130 int idist = m_height*(m_width/2+1), odist = 1;
131 int istride = 1, ostride = n_channels;
133 int *inembed = NULL, *onembed = NULL;
137 inembed[1] = m_width/2+1, onembed[1] = m_width;
139 fftwf_complex *in = reinterpret_cast<fftwf_complex*>(complex_vconcat.data);
140 float *out = reinterpret_cast<float*>(real_result.data);
142 std::unique_lock<std::mutex> lock(fftw_mut);
143 fftw_plan_with_nthreads(2);
146 fftw_plan_with_nthreads(omp_get_max_threads());
150 plan = fftwf_plan_many_dft_c2r(rank, n, howmany,
151 in, inembed, istride, idist,
152 out, onembed, ostride, odist,
162 fftwf_destroy_plan(plan);
166 return real_result/(m_width*m_height);