How does the Fourier transform look in 2D?
Gustavo Sánchez Hurtado
Award-Winning Engineer, Researcher & Educator | Digital Transformation: Control Systems, IoT, and Machine Learning | PLC/SCADA programmer | Python/MATLAB | Node Red | Global Speaker, Author & Podcaster
The Fourier transform can be difficult to understand, especially for those who are not familiar with advanced mathematics, since it involves abstract concepts such as frequency domain representation,
In this regard,?visual examples can be helpful in providing a more intuitive understanding. For instance, plotting filtered images can help to illustrate the effect of filtering operations, in both the original and transformed space.
In the figure above, you can see the effect of applying a low-pass filtering (blurring): it is easy to see the removal of high-frequency components in the image's spectrum.
Here below the code:
领英推荐
import numpy as n
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
from scipy import ndimage
# Create a new image with the random values as grayscale pixels
img = Image.open(r"path to your image")?
# Define the low pass filter kernel
B = 20 # Pass-band variable
kernel = np.ones((B,B)) / (B*B)
# Define the high-pass filter kernel
# k = 0.5 # Filter strength
# kernel = np.array([[-k, -k, -k], [-k, 8*k+1, -k], [-k, -k, -k]])
# Apply the low pass filter to the image
img_filtered = ndimage.convolve(img, kernel)
# load the pixel data
img_data = img_filtered
# load the pixel data
#img_data = img.load()
# create a NumPy array from the pixel data
M = np.array(img_data)
# display the image using matplotlib
#plt.imshow(M, cmap='gray')
# calculate the 2D Fourier transform of the image
f = np.fft.fft2(M)
# shift the zero-frequency component to the center of the spectrum
f_shifted = np.fft.fftshift(f)
# calculate the amplitude of the transformed image
amplitude = np.abs(f_shifted)
amplitude = np.clip(amplitude, a_min=0, a_max=50000)
# Rescale the array to be between 0 and 255
arr_rescaled = (amplitude - amplitude.min()) * (255 / (amplitude.max() - amplitude.min()))
# Convert the rescaled array to integers
arr_rescaled = arr_rescaled.astype('uint8')
# display the amplitude of the transformed image using matplotlib
plt.imshow(arr_rescaled , cmap='gray')
plt.show()
Feel free to leave your comments here below, I would be happy to answer.
At?MYWAI?we promote agile, explainable, reliable and affordable ML at the edge.