Improving Bandpass Filter for Image Processing in Python

75 views Asked by At

I am currently working on an image processing task in Python where I am trying to apply a bandpass filter to an image using the Fourier Transform. My goal is to convert the original image: Original Image

To this output: Cleaned output

Here is the code I have tried so far:

import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread('path-to-image', cv2.IMREAD_GRAYSCALE)

# Apply Fourier Transform
f_transform = np.fft.fft2(image)
f_transform_shifted = np.fft.fftshift(f_transform)

# Create a bandpass filter
rows, cols = image.shape
crow, ccol = rows // 2, cols // 2
inner_radius = 0.1 * crow
outer_radius = 0.8 * crow

# Create a meshgrid for the frequency coordinates
x = np.arange(-ccol, ccol)
y = np.arange(-crow, crow)
x, y = np.meshgrid(x, y)

# Create the bandpass filter
mask = ((x**2 + y**2 >= inner_radius**2) & (x**2 + y**2 <= outer_radius**2))

# Apply the mask to the shifted Fourier transform
f_transform_shifted_filtered = f_transform_shifted * mask

# Inverse Fourier Transform to get the image back
f_inverse_shifted = np.fft.ifftshift(f_transform_shifted_filtered)
image_filtered = np.fft.ifft2(f_inverse_shifted)
image_filtered = np.abs(image_filtered)

# Convert back to uint8 and normalize the values
image_filtered = np.uint8(image_filtered)
image_filtered = cv2.normalize(image_filtered, None, 0, 255, cv2.NORM_MINMAX)

The problem I am facing is that the output image does not match my desired output. I believe the issue lies in the way I am defining and applying the bandpass filter, but I am not sure how to adjust it to achieve the desired result.

Any suggestions on how I can improve this code to get closer to my desired output would be greatly appreciated.

0

There are 0 answers