From 4bab354a95582b43f74f0d1beef00bdd31e1e91a Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Tue, 14 May 2024 00:24:02 +0200 Subject: [PATCH] Clean `fft.py` --- datasets/raise/fft/fft.py | 45 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/datasets/raise/fft/fft.py b/datasets/raise/fft/fft.py index 22da269..e9161c5 100644 --- a/datasets/raise/fft/fft.py +++ b/datasets/raise/fft/fft.py @@ -2,35 +2,40 @@ from scipy import fftpack import numpy as np import imageio from PIL import Image, ImageDraw +import matplotlib.pyplot as plt -image1 = imageio.imread('image.jpg',as_gray=True) +image = imageio.imread('image.jpg', mode = 'L') +#plt.imshow(image, cmap = 'grey') +#plt.show() -#convert image to numpy array -image1_np=np.array(image1) +# convert image to numpy array +imageNp = np.array(image) -#fft of image -fft1 = fftpack.fftshift(fftpack.fft2(image1_np)) +# fft of image +fft1 = fftpack.fftshift(fftpack.fft2(imageNp)) -#Create a low pass filter image -x,y = image1_np.shape[0],image1_np.shape[1] -#size of circle -e_x,e_y=50,50 -#create a box -bbox=((x/2)-(e_x/2),(y/2)-(e_y/2),(x/2)+(e_x/2),(y/2)+(e_y/2)) +# create a low pass filter image +x, y = imageNp.shape +# size of circle +CIRCLE_RADIUS = 50 +eX, eY = CIRCLE_RADIUS, CIRCLE_RADIUS +# create a box +middleX, middleY = x / 2, y / 2 +bbox = (middleX - eX, middleY - eY, middleX + eX, middleY + eY) -low_pass=Image.new("L",(image1_np.shape[0],image1_np.shape[1]),color=0) +lowPass = Image.new('L', imageNp.shape, color = 0) -draw1=ImageDraw.Draw(low_pass) -draw1.ellipse(bbox, fill=1) +draw = ImageDraw.Draw(lowPass) +draw.ellipse(bbox, fill = 1) -low_pass_np=np.array(low_pass) +lowPassNp = np.array(lowPass) -#multiply both the images -filtered=np.multiply(fft1,low_pass_np) +# multiply both the images +filtered = np.multiply(fft1, lowPassNp.T) -#inverse fft +# inverse fft ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(filtered))) ifft2 = np.maximum(0, np.minimum(ifft2, 255)) -#save the image -imageio.imsave('fft-then-ifft.png', ifft2.astype(np .uint8)) \ No newline at end of file +# save the image +imageio.imsave('fft-then-ifft.png', ifft2.astype(np.uint8)) \ No newline at end of file