使用Python执行傅里叶分析进行图像重建

问题描述 投票:0回答:1

我正在尝试对使用 Python 生成的一些形状进行傅里叶分析。我设法在图像上获得二维傅里叶变换并应用高斯滤波器,但是使用高斯滤波器的图像的逆图像正在移动,我不知道如何解决它。我试图采用真实的值只是希望它能解决这个问题,但没有任何改变。我在下面演示我的代码,有人对我能做什么有任何想法吗?

#importing necessary libraries 
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import gaussian_filter


#function to apply Gaussian filter to the Fourier transform
def apply_gaussian_filter(fourier_transform, sigma):
    return gaussian_filter(fourier_transform, sigma)

##code to produce an image of a box and perform Fourier analysis on it

#defining the size bounds of the box
w, h = 65, 65
box = [(50, 29), (w-50, h-30)]

#creating the image with a white background
im = Image.new("I", (w,h))
img = ImageDraw.Draw(im)

#producing the box shape
img.rectangle(box, fill = "white", outline = "white")

#convering image to numpy library
im_array = np.array(im)

#taking the 2D Fourier transform
fourier_box = np.fft.fft2(im_array)

#shifting the zero frequency component to the center
fourier_box_shifted = np.fft.fftshift(fourier_box)

#applying Gaussian filter to the Fourier transform of the box
sigma_box = 5
filtered_fourier_box = apply_gaussian_filter(np.abs(fourier_box_shifted), sigma_box)

##code to produce an image of a circle and perform Fourier analysis on it

#defining the size bounds of the shape
w_1, h_1 = 65, 65
circle = [(10, 10), (w_1-10, h_1-10)]

#creating the image with a white background
im_1 = Image.new("I", (w_1,h_1))
img_1 = ImageDraw.Draw(im_1)

#producing the circle shape
img_1.ellipse(circle, fill = "white", outline = "white")

#convering image to numpy library
im_1_array = np.array(im_1)

#taking the 2D Fourier transform
fourier_circle = np.fft.fft2(im_1_array)

#shifting the zero frequency component to the center
fourier_circle_shifted = np.fft.fftshift(fourier_circle)

#applying Gaussian filter to the Fourier transform of the circle
sigma_circle = 5
filtered_fourier_circle = apply_gaussian_filter(np.abs(fourier_circle_shifted), sigma_circle)

#applying inverse Fourier transform to the filtered Fourier data
inverse_filtered_box = np.fft.ifft2(np.fft.ifftshift(filtered_fourier_box)).real
inverse_filtered_circle = np.fft.ifft2(np.fft.ifftshift(filtered_fourier_circle)).real

#adjusting size of the graphs 
plt.figure(figsize=(13, 8))

#displaying the Fourier transform of the box 
plt.subplot(231)  
plt.imshow(np.log(np.abs(fourier_box_shifted) + 1))
plt.title('2D Fourier Transform Box')

#displaying the Fourier transform of the box with Gaussian filter
plt.subplot(232) 
plt.imshow(np.log(filtered_fourier_box + 1))
plt.title('Gaussian Filtered 2D Fourier Transform Box')

#displaying the inverse of the filtered Fourier box
plt.subplot(233)  
plt.imshow(np.abs(np.real(inverse_filtered_box)))
plt.title('Inverse Filtered Box')

#displaying the Fourier transform of the circle
plt.subplot(234)
plt.imshow(np.log(np.abs(fourier_circle_shifted) + 1))
plt.title('2D Fourier Transform Circle')

#displaying the Fourier transform of the circle with Gaussian filter
plt.subplot(235)
plt.imshow(np.log(filtered_fourier_circle + 1))
plt.title('Gaussian Filtered 2D Fourier Transform Circle')

#displaying the inverse of the filtered Fourier circle
plt.subplot(236)  
plt.imshow(np.abs(np.real(inverse_filtered_circle)))
plt.title('Inverse Filtered Circle')

enter image description here

我所附的图片是我获得的结果,问题与逆图有关。

python image-processing fft analysis
1个回答
3
投票

阅读您的代码并尝试比较每个步骤后,我设法发现您的代码存在问题:

apply_gaussian_filter(np.abs(fourier_circle_shifted), sigma_circle)
。对
fourier_circle_shifted
的绝对值应用高斯将使您丢失相位信息,因此重建将不起作用。以下是在整个 fft 或其绝对值上应用高斯的两个函数:

def filterAndReconstruct_Abs(im, sigma = 1):
    fftOfIm = np.fft.fft2(im) # fft2 on image
    fftOfIm_Shifted = np.fft.fftshift(fftOfIm) # shift
    fftOfIm_Filtered = gaussian_filter(np.abs(fftOfIm_Shifted), sigma) # gaussian on absolute!!
    imReconstructed = np.fft.ifft2(np.fft.ifftshift(fftOfIm_Filtered)).real # reconstruct
    return imReconstructed
def filterAndReconstruct_WithPhase(im, sigma = 1):
    fftOfIm = np.fft.fft2(im) # fft2 on image
    fftOfIm_Shifted = np.fft.fftshift(fftOfIm) # shift
    fftOfIm_Filtered = gaussian_filter(fftOfIm_Shifted, sigma) # gaussian on fft with phase!!
    imReconstructed = np.fft.ifft2(np.fft.ifftshift(fftOfIm_Filtered)).real # reconstruct
    return imReconstructed

现在让我们举一个矩形的例子:

im = np.zeros((1000, 1000)) # create empty array
im[200:800, 200:800] = 255 # draw a rectangle
sigma = 0.2 # define sigma
fig, axs = plt.subplots(ncols = 3, nrows = 1, sharex = True, sharey = True)
axs[0].imshow(im)
axs[1].imshow(filterAndReconstruct_Abs(im,sigma)+1, norm=LogNorm())
axs[2].imshow(filterAndReconstruct_WithPhase(im,sigma)+1, norm=LogNorm())
for ax in axs:
    ax.axis("off")

结果会是这样的:

如您所见,FFT 重建是正确的变化。玩你的例子,如果你使用

apply_gaussian_filter(fourier_circle_shifted, sigma_circle)
和较低的西格玛,你也会得到一些不错的结果。

希望这对您有进一步帮助,还有我的示例的导入:

%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import gaussian_filter
from matplotlib.colors import LogNorm
© www.soinside.com 2019 - 2024. All rights reserved.