Skip to content

Instantly share code, notes, and snippets.

@pourmand1376
Last active May 25, 2021 11:45
Show Gist options
  • Save pourmand1376/c9235b0cbaa43451c10d4f41ddd39d94 to your computer and use it in GitHub Desktop.
Save pourmand1376/c9235b0cbaa43451c10d4f41ddd39d94 to your computer and use it in GitHub Desktop.
Fast Fourier Transform Implementation with numpy
import numpy as np
import matplotlib.pyplot as plt
from skimage import color
def fft(length_x,width_y,v,y,image,inner=True):
expVector=np.exp(-2j*np.pi/width_y*(v@y)) if inner else np.exp(-2j*np.pi/length_x*(v@y))
if inner:
return image @ expVector
else:
return expVector @ image
def fft2d(grayImage):
length_x = grayImage.shape[0]
width_y= grayImage.shape[1]
y=np.arange(width_y).reshape(1,-1)
v = np.arange(width_y).reshape(-1,1)
firstLevel=fft(length_x,width_y,v,y,grayImage,True)
u = np.arange(length_x).reshape(-1,1)
x = np.arange(length_x).reshape(1,-1)
return fft(length_x,width_y,u,x,firstLevel,False)
image=plt.imread('filename')
grayImage=color.rgb2gray(image)
transformedImage=fft2d(grayImage)
magnitude = np.log(np.abs(fourierimage)+1))
angle = np.angle(fourierimage))
f,axxr = plt.subplots(nrows=1,ncols=2,figsize=(15,15))
axxr[0].imshow(magnitude),plt.title('Magnitude')
axxr[1].imshow(angle),plt.title('Angle')
@pourmand1376
Copy link
Author

This implementation only works with grayscale 2d images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment