A Fourier transform image opencv

We live in a world of time, up at 7:00 in the morning to eat breakfast, 8:00 to go to crowded subway, 9:00 start to work. . . Time-domain analysis is the time reference. But everything is still in the frequency domain of!

Frequency: drastic changes in the gray component, for example, the boundary

Low: slow changes the gray component, a sea e.g.

Low Pass Filter: retaining only a low frequency, so that the image will be blurred

High-pass filter: retaining only a high frequency, such that image detail enhancement will

In opencv mainly cv2.dft () and cv2.idft (), the input image need to be converted into a format np.float32.
The results obtained in the frequency will be 0 in the upper left portion, typically convert to the center position, it can be realized by shift conversion.
cv2.dft () returns the result of the two-channel (real part, imaginary part), usually you need to be converted into display image format (0,255).

Correlation function as follows

1. cv2.dft(img, cv2.DFT_COMPLEX_OUTPUT) 进行傅里叶变化

参数说明: img表示输入的图片, cv2.DFT_COMPLEX_OUTPUT表示进行傅里叶变化的方法

2. np.fft.fftshift(img)  将图像中的低频部分移动到图像的中心

参数说明:img表示输入的图片

3. cv2.magnitude(x, y) 将sqrt(x^2 + y^2) 计算矩阵维度的平方根

参数说明:需要进行x和y平方的数

4.np.fft.ifftshift(img) # 进图像的低频和高频部分移动到图像原来的位置

参数说明:img表示输入的图片

5.cv2.idft(img) # 进行傅里叶的逆变化

参数说明:img表示经过傅里叶变化后的图片

First, the frequency domain information image drawing

Step One: Load images

Step 2: Use np.float32 format conversion

The third step: Use cv2.dft Fourier changes

The fourth step: using the low frequency np.fft.shiftfft transferred to an intermediate position

Step 5: cv2.magnitude the real and imaginary parts of a projector to a spatial domain

Step Six: plotted operation

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


# 第一步读取图片
img = cv2.imread('lena.jpg', 0)

# 第二步:进行float32形式转换
float32_img = np.float32(img)

# 第三步: 使用cv2.dft进行傅里叶变化
dft_img = cv2.dft(float32_img, flags=cv2.DFT_COMPLEX_OUTPUT)

# 第四步:使用np.fft.shiftfft()将变化后的图像的低频转移到中心位置
dft_img_ce = np.fft.fftshift(dft_img)

# 第五步:使用cv2.magnitude将实部和虚部转换为实部,乘以20是为了使得结果更大
img_dft = 20 * np.log(cv2.magnitude(dft_img_ce[:, :, 0], dft_img_ce[:, :, 1]))

# 第六步:进行画图操作
plt.subplot(121)
plt.imshow(img, cmap='gray')
plt.subplot(122)
plt.imshow(img_dft, cmap='gray')
plt.show()

Here Insert Picture Description

Second, low-pass filtering

The first step: read the image

Step two: np.float32 type conversion

The third step: Use cv2.dft Fourier changes

Step Four: Use np.fft.fftshift converting the low frequency part to the center of the image

Step Five: mask structure, so that the center position of the mask is 1, the edge position of 0

Step Six: image combined with the Fourier transform of the mask, leaving only the central portion of the low-frequency position

Step 7: Use np.fft.ifftshift the low-frequency part of the transfer back to the original position of the image

Step eight: Use cv2.idft Fourier reverse conversion

Step 9: Use cv2.magnitude image converting real and imaginary portions of the spatial domain

Step 10: Operation plotted

# 第一步读入图片
img = cv2.imread('lena.jpg', 0)
# 第二步:进行数据类型转换
img_float = np.float32(img)
# 第三步:使用cv2.dft进行傅里叶变化
dft = cv2.dft(img_float, flags=cv2.DFT_COMPLEX_OUTPUT)
# 第四步:使用np.fft.fftshift将低频转移到图像中心
dft_center = np.fft.fftshift(dft)
# 第五步:定义掩模:生成的掩模中间为1周围为0
crow, ccol = int(img.shape[0] / 2), int(img.shape[1] / 2) # 求得图像的中心点位置
mask = np.zeros((img.shape[0], img.shape[1], 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1

# 第六步:将掩模与傅里叶变化后图像相乘,保留中间部分
mask_img = dft_center * mask

# 第七步:使用np.fft.ifftshift()将低频移动到原来的位置
img_idf = np.fft.ifftshift(mask_img)

# 第八步:使用cv2.idft进行傅里叶的反变化
img_idf = cv2.idft(img_idf)

# 第九步:使用cv2.magnitude转化为空间域内
img_idf = cv2.magnitude(img_idf[:, :, 0], img_idf[:, :, 1])

# 第十步:进行绘图操作
plt.subplot(121)
plt.imshow(img, cmap='gray')
plt.subplot(122)
plt.imshow(img_idf, cmap='gray')
plt.show()

Here Insert Picture Description

Third, high-pass filtering

Process as above, except for the configuration of the intermediate mask is 0, 1 edges, then combined with the Fourier transform image, to retain the high frequency part, remove low frequency portion

# 第一步读入图片
img = cv2.imread('lena.jpg', 0)
# 第二步:进行数据类型转换
img_float = np.float32(img)
# 第三步:使用cv2.dft进行傅里叶变化
dft = cv2.dft(img_float, flags=cv2.DFT_COMPLEX_OUTPUT)
# 第四步:使用np.fft.fftshift将低频转移到图像中心
dft_center = np.fft.fftshift(dft)
# 第五步:定义掩模:生成的掩模中间为0周围为1
crow, ccol = int(img.shape[0] / 2), int(img.shape[1] / 2) # 求得图像的中心点位置
mask = np.ones((img.shape[0], img.shape[1], 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0

# 第六步:将掩模与傅里叶变化后图像相乘,去除中心部分,保留周围部分
mask_img = dft_center * mask

# 第七步:使用np.fft.ifftshift()将低频移动到原来的位置
img_idf = np.fft.ifftshift(mask_img)

# 第八步:使用cv2.idft进行傅里叶的反变化
img_idf = cv2.idft(img_idf)

# 第九步:使用cv2.magnitude转化为空间域内
img_idf = cv2.magnitude(img_idf[:, :, 0], img_idf[:, :, 1])

# 第十步:进行绘图操作
plt.subplot(121)
plt.imshow(img, cmap='gray')
plt.subplot(122)
plt.imshow(img_idf, cmap='gray')
plt.show()

Here Insert Picture Description

Published 27 original articles · won praise 20 · views 1546

Guess you like

Origin blog.csdn.net/qq_39507748/article/details/104551726