Digital Image Processing--Fourier (Inverse) Transform

Digital Image Processing – Fourier (Inverse) Transform

main content

(1) Scale an image, display the original image and the zoomed image, perform Fourier transform on them respectively, and display the transformed result; (2)
Rotate an image, display the original image and the rotated image Image, perform Fourier transform on it respectively, and display the transformed result;

source code

import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import torchvision.transforms.functional as TF

rose = cv2.imread('2.png', 0)  # 转为灰度图
rose_shrink = cv2.resize(rose,(int(rose.shape[0]/2),int(rose.shape[1]/2)))

rose_rotate = Image.fromarray(np.uint8(rose))
rose_rotate = TF.rotate(rose_rotate, 45)
rose_rotate = np.asarray(rose_rotate)

def FFT(img):
    dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
    dftShift = np.fft.fftshift(dft)     # 将图像中的低频部分移动到图像的中心
    result = 20 * np.log(cv2.magnitude(dftShift[:, :, 0], dftShift[:, :, 1]))   # 将实部和虚部转换为实部,乘以20是为了使得结果更大

    ishift = np.fft.ifftshift(dftShift)     # 低频部分从图像中心移开
    iImg = cv2.idft(ishift)                 # 傅里叶反变换
    iImg = cv2.magnitude(iImg[:, :, 0], iImg[:, :, 1])      # 转化为空间域

    return result,iImg

#自定义傅里叶变换函数
def dft(img):
    H,W,channel=img.shape
    #定义频域图、复数矩阵
    F = np.zeros((H, W,channel), dtype=np.complex)
    #准备与原始图像位置相对应的处理索引
    x = np.tile(np.arange(W), (H, 1))
    y = np.arange(H).repeat(W).reshape(H, -1)
    #遍历
    for c in range(channel):
        for u in range(H):
            for v in range(W):
                F[u, v, c] = np.sum(img[..., c] * np.exp(-2j * np.pi * (x * u / W + y * v / H))) / np.sqrt(H * W)
    fshift = np.fft.fftshift(F)
    #将复数转为浮点数进行傅里叶频谱图显示
    fimg = np.log(np.abs(fshift))
    return fimg,F
#自定义傅里叶反变换
def idft(G):
    H, W, channel = G.shape
    #定义空白时域图像
    out = np.zeros((H, W, channel), dtype=np.float32)
    # 准备与原始图像位置相对应的处理索引
    x = np.tile(np.arange(W), (H, 1))
    y = np.arange(H).repeat(W).reshape(H, -1)
    #遍历
    for c in range(channel):
        for u in range(H):
            for v in range(W):
                out[u, v, c] = np.abs(np.sum(G[..., c] * np.exp(2j * np.pi * (x * u / W + y * v / H)))) / np.sqrt(W * H)
    #剪裁
    out = np.clip(out, 0, 255)
    out = out.astype(np.uint8)
    return out

img=cv2.imread("4.png")
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#傅里叶变换
rose_color_fft,rose_color_F=dft(img)
#傅里叶逆变换
rose_color_ifft=idft(rose_color_F)

#原图
rose_fft,rose_ifft = FFT(rose)
#缩小图
rose_shrink_fft,rose_shrink_ifft = FFT(rose_shrink)
#旋转图
rose_rotate_fft,rose_rotate_ifft = FFT(rose_rotate)

plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False

plt.subplot(131), plt.imshow(rose, cmap='gray'),plt.title('原图'),plt.axis('off')
plt.subplot(132), plt.imshow(rose_fft, cmap='gray'),plt.title('傅里叶变换'),plt.axis('off')
plt.subplot(133), plt.imshow(rose_ifft, cmap='gray'),plt.title('傅里叶逆变换'),plt.axis('off')  #关闭坐标轴

plt.figure(2)
plt.subplot(131), plt.imshow(rose_shrink, cmap='gray'),plt.title('原图'),plt.axis('off')
plt.subplot(132), plt.imshow(rose_shrink_fft, cmap='gray'),plt.title('傅里叶变换'),plt.axis('off')
plt.subplot(133), plt.imshow(rose_shrink_ifft, cmap='gray'),plt.title('傅里叶逆变换'),plt.axis('off')  #关闭坐标轴

plt.figure(3)
plt.subplot(131), plt.imshow(rose_rotate, cmap='gray'),plt.title('原图'),plt.axis('off')
plt.subplot(132), plt.imshow(rose_rotate_fft, cmap='gray'),plt.title('傅里叶变换'),plt.axis('off')
plt.subplot(133), plt.imshow(rose_rotate_ifft, cmap='gray'),plt.title('傅里叶逆变换'),plt.axis('off')

plt.figure(4)
plt.subplot(131), plt.imshow(img), plt.title('原图'),plt.axis('off')
plt.subplot(132), plt.imshow(rose_color_fft), plt.title('傅里叶变换'),plt.axis('off')
plt.subplot(133), plt.imshow(rose_color_ifft), plt.title('傅里叶逆变换'),plt.axis('off')

plt.show()

achieve results

In this experiment, the selected original image size is 1200 675, and Fourier transform and inverse Fourier transform are performed on the image. The spectrum obtained by Fourier transform and centering, and the image obtained after inverse transformation are shown in the figure below.
insert image description here
After reducing the length and width of the image by half, the obtained image, the spectrum obtained by Fourier transform and centering, and the image obtained after inverse transformation are shown in the figure below.
insert image description here
After rotating the original image by 45°, the obtained image, the spectrum obtained after Fourier transform and centering, and the image obtained after inverse transformation are shown in the figure below.
insert image description here
In this experiment, the Fourier transform function and the inverse Fourier transform function are customized to realize the Fourier transform and inverse transform of the color image. In this operation, considering the time consumed by the transformation operation, the original (1200
675) color image is reduced to (100*100) size, and this operation is completed. After Fourier transform, the spectrogram and the image obtained after Fourier inverse transform are obtained. As shown below.
insert image description here
Xiaobai one! ! !

Guess you like

Origin blog.csdn.net/MZYYZT/article/details/128160992