Fourier transform (low-pass filter and high-pass filter)

Fourier transform

The world we see usually runs through time, and the trend of stocks, the height of people, and the trajectory of cars will all change over time. This method of observing the dynamic world with time as a reference is called temporal analysis.
But if you use another method to observe the world, you will find that the world is eternal, and this static world is called the frequency domain.

One of the methods that can penetrate the time domain and frequency domain is Fourier analysis. Fourier analysis can be divided into Fourier series (Fourier Serie) and Fourier transform (Fourier Transformation). The essence of the Fourier series is to decompose a periodic signal into an infinite number of separate (discrete) sine waves. The Fourier series is a periodic and continuous function in the time domain, while it is a non-periodic discrete function in the frequency domain. The Fourier transform converts a non-periodic continuous signal in the time domain into a non-periodic continuous signal in the frequency domain.

Fourier tells us that any periodic function can be regarded as a superposition of sine waves with different amplitudes and different phases.

The role of the Fourier transform - converting the time domain to the frequency domain

  • High frequency: grayscale components that change drastically, such as boundaries
  • Low frequency: slowly changing grayscale components, such as a sea

filtering

  • 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

The functions involving Fourier transform in OpenCV are cv2.dft() and cv2.idft(), and the input image needs to be converted to np.float32 first.
The part with a frequency of 0 in the obtained result will be in the upper left corner, and usually needs to be converted to the center position, which can be achieved by shift transformation.
The result returned by cv2.dft() is dual-channel (real part and imaginary part), and usually needs to be converted into an image format to display ([0, 255]).

Low pass filter to achieve image blurring:

import cv2
import matplotlib.pyplot as plt
import numpy as np

# DFT
img = cv2.imread(r'F:/aixin.jpg', 0)  # img.dtype为uint8
img_float32 = np.float32(img)  # 将uint8格式的图像转换成float32格式
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)  # 傅里叶变换,得到频谱图,有两个通道,低频部分往往在频谱图的左上角
dft_shift = np.fft.fftshift(dft)  # 将频谱图左上角低频部分转换到中心位置

height, width = img.shape
centerh, centerw = int(height/2), int(width/2)  # 获取图像中心位置

# 低通滤波器
mask = np.zeros((height, width, 2), np.uint8)
mask[centerh-30:centerh+30, centerw-30:centerw+30] = 1

# IDFT
fshift = dft_shift * mask  # dft_shift只保留了中心部分的低频信息
f_ishift = np.fft.ifftshift(fshift)  # 将中心位置的低频转回到左上角
img_back = cv2.idft(f_ishift)  # 将频谱图还原成图像,此时的图像还无法展示,因为包含双通道(实部和虚部)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])  # 使得图像能够展示

plt.subplot(1, 2, 1)
plt.title('source')
plt.imshow(img, 'gray')

plt.subplot(1, 2, 2)
plt.title('low frequency')
plt.imshow(img_back, 'gray')

plt.show()

insert image description here

High-pass filter to achieve image detail enhancement:

import cv2
import matplotlib.pyplot as plt
import numpy as np

# DFT
img = cv2.imread(r'F:/aixin.jpg', 0)  # img.dtype为uint8
img_float32 = np.float32(img)  # 将uint8格式的图像转换成float32格式
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)  # 傅里叶变换,得到频谱图,有两个通道,低频部分往往在频谱图的左上角
dft_shift = np.fft.fftshift(dft)  # 将频谱图左上角低频部分转换到中心位置

height, width = img.shape
centerh, centerw = int(height/2), int(width/2)  # 获取图像中心位置

# 高通滤波器
mask = np.ones((height, width, 2), np.uint8)
mask[centerh-30:centerh+30, centerw-30:centerw+30] = 0

# IDFT
fshift = dft_shift * mask  # dft_shift只保留了中心部分的低频信息
f_ishift = np.fft.ifftshift(fshift)  # 将中心位置的低频转回到左上角
img_back = cv2.idft(f_ishift)  # 将频谱图还原成图像,此时的图像还无法展示,因为包含双通道(实部和虚部)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])  # 使得图像能够展示

plt.subplot(1, 2, 1)
plt.title('source')
plt.imshow(img, 'gray')

plt.subplot(1, 2, 2)
plt.title('high frequency')
plt.imshow(img_back, 'gray')

plt.show()

insert image description here

Guess you like

Origin blog.csdn.net/weixin_48158964/article/details/131749837