# 频率域滤波基础

## 参考资料

OpenCV 关于 Fourier Transform 应用的示例

## 原理概述

For a sinusoidal signal, $x(t) = A \sin(2 \pi ft)$, we can say $f$ is the frequency of signal, and if its frequency domain is taken, we can see a spike at $f$. If signal is sampled to form a discrete signal, we get the same frequency domain, but is periodic in the range $[- \pi, \pi]$ or $[0,2\pi]$ (or $[0,N]$ for N-point DFT). You can consider an image as a signal which is sampled in two directions. So taking fourier transform in both X and Y directions gives you the frequency representation of image

## 使用 Numpy 实现 DFT

• numpy.fft.fft2(a, s=None, axes=(-2, -1), norm=None) 提供了计算二维 DFT 的方法，参数描述：
• a : array_like 输入数组，可以为复数，灰度图。
• s : sequence of ints, optional 决定输出数组的大小，如果输入图像的大小大于输入图像的大小，则在计算 FFT 之前用零填充输入图像。如果它小于输入图像，输入图像将被裁剪。
• axes : sequence of ints, optional 没看懂，原始这么写的：Axes over which to compute the FFT. If not given, the last two axes are used. A repeated index in axes means the transform over that axis is performed multiple times. A one-element sequence means that a one-dimensional FFT is performed.
• norm : {None, "ortho"}, optional 1.10.0.新加入，归一化方式，默认为 None。

• numpy.fft.fftshift(x, axes=None) 提供了将零频率分量移到频谱的中心的方法，对于二维输入，交换第一和第三象限，第二和第四象限，参数描述：
• x : array_like 输入数组
• axes : int or shape tuple, optional 要移动的轴。 默认值为 None

## 使用 OpenCV 实现 DFT

OpenCV 也提供的工具用于实现傅里叶变换，分别是cv2.dft()cv2.idft()函数。返回有两个通道，第一个通道是结果的实部，第二个通道是结果的虚部。所以在计算其幅度的时候需要先手动转化一次。

Note: As usual, OpenCV functions cv2.dft() and cv2.idft() are faster than Numpy counterparts. But Numpy functions are more user-friendly. For more details about performance issues, see below section.

## 性能测试

• 测试使用 OpenCV 进行 DFT 变换

测试结果：10.3 ms ± 105 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

• 测试使用 Numpy 进行 DFT 变换

测试结果：100 ms ± 1.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)