Here's a Python code that demonstrates the transformation of the shifted fast Fourier transform for different types of ideal filters, normal filters, and returns them to the spatial domain:
import numpy as np
import cv2
from matplotlib import pyplot as plt
def shift_fft(img):
shifted = np.fft.fftshift(np.fft.fft2(img))
return shifted
def inverse_shift_fft(shifted):
inv_shifted = np.fft.ifftshift(shifted)
return inv_shifted
def ideal_lowpass(shifted, cutoff):
rows, cols = shifted.shape
crow, ccol = rows // 2, cols // 2
mask = np.zeros((rows, cols))
mask[crow - cutoff: crow + cutoff, ccol - cutoff: ccol + cutoff] = 1
filtered = np.multiply(mask, shifted)
return filtered
def ideal_highpass(shifted, cutoff):
rows, cols = shifted.shape
crow, ccol = rows // 2, cols // 2
mask = np.ones((rows, cols))
mask[crow - cutoff: crow + cutoff, ccol - cutoff: ccol + cutoff] = 0
filtered = np.multiply(mask, shifted)
return filtered
def ideal_bandpass(shifted, cutoff_low, cutoff_high):
rows, cols = shifted.shape
crow, ccol = rows // 2, cols // 2
mask = np.zeros((rows, cols))
mask[crow - cutoff_high: crow + cutoff_high, ccol - cutoff_high: ccol + cutoff_high] = 1
mask[crow - cutoff_low: crow + cutoff_low, ccol - cutoff_low: ccol + cutoff_low] = 0
filtered = np.multiply(mask, shifted)
return filtered
def gaussian_lowpass(shifted, sigma):
rows, cols = shifted.shape
crow, ccol = rows // 2, cols // 2
x = np.linspace(-0.5,0.5,cols)
y = np.linspace(-0.5,0.5,rows)
xx, yy = np.meshgrid(x, y)
d = np.sqrt(xx**2 + yy**2)
mask = np.exp(-2 * sigma**2 * (d**2))
filtered = np.multiply(mask, shifted)
return filtered
def butterworth_lowpass(shifted, cutoff, order):
rows, cols = shifted.shape
crow, ccol = rows // 2, cols // 2
x = np.linspace(-0.5,0.5,cols)
y = np.linspace(-0.5,0.5,rows)
xx, yy = np.meshgrid(x, y)
d = np.sqrt(xx**2 + yy**2)
mask = 1 / (1 + ((d * cutoff) ** (2 * order)))
filtered = np.multiply(mask, shifted)
return filtered
def laplacian_highboost(shifted, alpha):
rows, cols = shifted.shape
crow, ccol = rows // 2, cols // 2
x = np.linspace(-1, 1, cols)
y = np.linspace(-1, 1, rows)
xx, yy = np.meshgrid(x, y)
d = np.sqrt(xx**2 + yy**2)
mask = -4 * np.pi**2 * d**2
mask = mask + 1
filtered = np.multiply(mask, shifted)
filtered = alpha * shifted - filtered
return filtered
# Load the Lena image
img = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
# Perform the shifted FFT
shifted = shift_fft(img)
# Apply ideal lowpass filter
cutoff_ideal_lp = 40
filtered_ideal_lp = inverse_shift_fft(ideal_lowpass(shifted, cutoff_ideal_lp))
# Apply ideal highpass filter
cutoff_ideal_hp = 40
filtered_ideal_hp = inverse_shift_fft(ideal_highpass(shifted, cutoff_ideal_hp))
# Apply ideal bandpass filter
cutoff_low_ideal_bp = 20
cutoff_high_ideal_bp = 60
filtered_ideal_bp = inverse_shift_fft(ideal_bandpass(shifted, cutoff_low_ideal_bp, cutoff_high_ideal_bp))
# Apply Gaussian lowpass filter
sigma_gaussian = 30
filtered_gaussian_lp = inverse_shift_fft(gaussian_lowpass(shifted, sigma_gaussian))
# Apply Butterworth lowpass filter
cutoff_butterworth_lp = 40
order_butterworth_lp = 2
filtered_butterworth_lp = inverse_shift_fft(butterworth_lowpass(shifted, cutoff_butterworth_lp, order_butterworth_lp))
# Apply Laplacian highboost filter
alpha_laplacian_hb = 2
filtered_laplacian_hb = inverse_shift_fft(laplacian_highboost(shifted, alpha_laplacian_hb))
# Compare the outputs visually
plt.figure(figsize=(12, 12))
plt.subplot(2, 3, 1), plt.imshow(np.abs(filtered_ideal_lp), cmap='gray')
plt.title('Ideal Lowpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 2), plt.imshow(np.abs(filtered_ideal_hp), cmap='gray')
plt.title('Ideal Highpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 3), plt.imshow(np.abs(filtered_ideal_bp), cmap='gray')
plt.title('Ideal Bandpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 4), plt.imshow(np.abs(filtered_gaussian_lp), cmap='gray')
plt.title('Gaussian Lowpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 5), plt.imshow(np.abs(filtered_butterworth_lp), cmap='gray')
plt.title('Butterworth Lowpass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 3, 6), plt.imshow(np.abs(filtered_laplacian_hb), cmap='gray')
plt.title('Laplacian Highboost Filter'), plt.xticks([]), plt.yticks([])
plt.show()
Note: Make sure you have the 'lena.png' image file in the same directory as the Python script. You can replace 'lena.png' with the path to your own Lena image.