数字图像处理--傅里叶(逆)变换

数字图像处理–傅里叶(逆)变换

主要内容

(1)对一副图像进行缩放,显示原始图像和缩放后的图像,分别对其进行傅里叶变换,显示变换后结果;
(2)对一副图像进行旋转,显示原始图像和旋转后的图像,分别对其进行傅里叶变换,显示变换后结果;

源代码

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()

实现结果

本实验中,选择的原始图像尺寸为1200675,对该图像进行傅里叶变换和傅里叶逆变换。其经过傅里叶变换并进行中心化得到的谱,以及逆变换后得到的图像,如下图所示。
在这里插入图片描述
将图像长、宽均缩减二分之一后,得到的图像,以及经过傅里叶变换并进行中心化得到的谱,和逆变换后得到的图像,如下图所示。
在这里插入图片描述
将原图像进行旋转45°后,得到的图像,以及经过傅里叶变换并进行中心化得到的谱,和逆变换后得到的图像,如下图所示。
在这里插入图片描述
本实验中,自定义了傅里叶变换函数和傅里叶逆变换函数,实现了对彩色图像的傅里叶变换和逆变换。在此操作中考虑到变换操作所耗费的时间,故对原来(1200
675)的彩色图像进行了缩减至(100*100)大小,并完成了本次操作。经过傅里叶变换得到频谱图以及傅里叶逆变换后得到的图像。如下图所示。
在这里插入图片描述
小白一枚!!!

猜你喜欢

转载自blog.csdn.net/MZYYZT/article/details/128160992