计算机视觉 (五) -- 图片FFT

FFT(Fast Fourier Transformation)是离散傅氏变换(DFT)的快速算法。即为快速傅氏变换。它是根据离散傅氏变换的奇、偶、虚、实等特性,对离散傅立叶变换的算法进行改进获得的。

我们使用FFT 对图像的高频和低频信号处理,可以找出信号在哪个方向变化快。在变化快方向就有可能存在边界。


import numpy as np
import matplotlib.pyplot as plt
import cv2
image = cv2.imread("./imgs/1.jpg")
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
f = np.fft.fft2(image/255.0)#use 2 dim fft
f_shift = np.fft.fftshift(f)
'''
#Shift the zero-frequency component to the center of the spectrum.
>>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3)
>>> freqs
array([[ 0., 1., 2.],
[ 3., 4., -4.],
[-3., -2., -1.]])
>>> np.fft.fftshift(freqs, axes=(1,))
array([[ 2., 0., 1.],
[-4., 3., 4.],
[-1., -3., -2.]])
'''
frequency_tx = 60*np.log(np.abs(f_shift))
f,(a1,a2) = plt.subplots(1,2,figsize=(200,200))
a1.set_title("original image")
a1.imshow(image,cmap='gray')
a2.set_title("requency transform image")
a2.imshow(frequency_tx,cmap='gray')
plt.show()

计算机视觉 (五) -- 图片FFT