Article Directory
1. Overview of Fourier transform
Fourier transform means that a certain function that satisfies certain conditions can be expressed as a linear combination of trigonometric functions (sine and/or cosine functions) or their integrals. The places that change drastically in the image (such as the boundary) are equivalent to high frequencies after Fourier transformation, and the places that change slowly are low frequencies. The Fourier transform can transform the image into the frequency domain, and the inverse Fourier transform transforms the frequency domain into the spatial domain. For details, you can see the Fourier analysis written by this big guy .
Second, the role of Fourier transform
High frequency: grayscale components that change drastically, such as boundaries
Low frequency: slowly changing grayscale components, such as a sea
Low-pass filter: only retains low frequencies, which will blur the image
High-pass filter: Only high frequencies are retained, which will enhance image details
Opencv mainly includes Fourier transform cv2.dft() and inverse Fourier transform cv2.idft(). The input image needs to be converted into np.float32 format first. The part with frequency 0 in the obtained result will be in the upper left corner. For the convenience of analysis, it is usually converted to the center position.
After processing with this function through dst=numpy.ffr.fftshift(src), the zero frequency component in the image spectrum will be Move to the center position of the frequency domain image. The result returned by cv2.dft() is dual-channel (real part, imaginary part), and usually needs to be converted into an image format to display (0,255).
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('D:/cat2.jpg',0) #0表示灰度图
img_float32 = np.float32(img)#转换格式
#傅里叶变化
dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
#将图像频谱中的零频率分量会被移到频域图像的中心位置
dft_shift = np.fft.fftshift(dft)
#得到灰度图能表示的形式
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
The center point in the picture is the low frequency part.
1. Low-pass filtering
Low-pass filtering is to keep the low-frequency part of the image and remove the high-frequency part of the image, and the place where the image changes drastically is equivalent to the high-frequency part. After removing the boundary, the entire image will become blurred. Covering the central part with a mask mask is equivalent to removing the central part.
code:
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('D:/cat2.jpg',0) #0表示灰度图
img_float32 = np.float32(img)#转换格式
#傅里叶变化
dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
#将图像频谱中的零频率分量会被移到频域图像的中心位置
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape
crow, ccol = int(rows/2) , int(cols/2) # 中心位置
# 低通滤波
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
# IDFT傅里叶逆变化
fshift = dft_shift*mask#保留中间部分
f_ishift = np.fft.ifftshift(fshift)#将零频率移回原来位置
img_back = cv2.idft(f_ishift)# IDFT傅里叶逆变化
#得到灰度图能表示的形式
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('Result'), plt.xticks([]), plt.yticks([])
plt.show()
Effect:
2. High-pass filtering
High-pass filtering removes the low-frequency part of the image, and only keeps the high-frequency part of the image (that is, the boundary). High-pass filtering and low-pass filtering only need to modify the low-pass code mask = np.ones((rows,
cols , 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0
Code:
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('D:/cat2.jpg',0) #0表示灰度图
img_float32 = np.float32(img)#转换格式
#傅里叶变化
dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
#将图像频谱中的零频率分量会被移到频域图像的中心位置
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape
crow, ccol = int(rows/2) , int(cols/2) # 中心位置
# 高通滤波
mask = np.ones((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0
# IDFT傅里叶逆变化
fshift = dft_shift*mask#保留中间部分
f_ishift = np.fft.ifftshift(fshift)#将零频率移回原来位置
img_back = cv2.idft(f_ishift)# IDFT傅里叶逆变化
#得到灰度图能表示的形式
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('Result'), plt.xticks([]), plt.yticks([])
plt.show()
Effect: