Experiment 6 - Removing Noise From Images and Applying Inverse Filtering
Experiment 6 - Removing Noise From Images and Applying Inverse Filtering
Objectives:
Prerequisites:
Steps:
1. Install and Import Libraries: We'll need OpenCV for image processing, Matplotlib for
plotting, and NumPy for matrix operations.
2. User-Uploaded Image Functionality in Google Colab: Google Colab allows you to
upload images directly using a file upload function.
3. Noise Removal: Use Gaussian filters to remove noise in the spatial domain.
4. Inverse Filtering: Apply inverse filtering in the frequency domain using Fourier
Transforms.
Code Implementation:
noisy_img = add_gaussian_noise(img)
plt.subplot(1, 2, 2)
plt.title('Noisy Image')
plt.imshow(noisy_img, cmap='gray')
plt.show()
filtered_img = gaussian_filter(noisy_img)
# Step 8.3: Apply the filter (multiply DFT with Gaussian mask)
filtered_dft = dft_shifted * mask
inverse_filtered_img = inverse_filtering(noisy_img)
Explanation:
Results:
This experiment showcases how noise can be reduced using both spatial and frequency domain
filtering techniques.
Explanation of program
The line !pip install opencv- -headless ensures that OpenCV is installed, even if it's
not available in your environment.
uploaded = files.upload()
● files.upload(): Opens a file selector dialog in Google Colab, allowing the user to upload
an image.
● After uploading, the image is saved in the uploaded dictionary where the key is the
filename.
def add_gaussian_noise(image):
row, col = image.shape
mean = 0
sigma = 25
gauss = np.random.normal(mean, sigma, (row, col)) # Generate
Gaussian noise
noisy_image = image + gauss # Add noise to the image
noisy_image = np.clip(noisy_image, 0, 255) # Ensure pixel values
are within valid range (0-255)
return noisy_image
noisy_img = add_gaussian_noise(img)
● add_gaussian_noise():
○ Generates random Gaussian noise using np.random.normal() with a mean
of 0 and standard deviation sigma=25.
○ This noise is added to the grayscale image to simulate a noisy image.
○ The np.clip() function ensures that the noisy image pixel values remain
between 0 (black) and 255 (white) to avoid overflows.
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(img, cmap='gray')
plt.subplot(1, 2, 2)
plt.title('Noisy Image')
plt.imshow(noisy_img, cmap='gray')
plt.show()
● plt.subplot(): Displays two images side-by-side. The first subplot shows the original
image, and the second shows the noisy image.
● plt.imshow(): Renders the images in grayscale (cmap='gray').
filtered_img = gaussian_filter(noisy_img)
plt.figure(figsize=(5, 5))
plt.title('Filtered Image (Spatial Domain)')
plt.imshow(filtered_img, cmap='gray')
plt.show()
● Filtered Image: The result of Gaussian filtering is displayed here. The noise should
appear less pronounced due to the smoothing effect of the Gaussian filter.
def inverse_filtering(image):
# Apply Fourier Transform to convert the image to the frequency
domain
dft = np.fft.fft2(image)
dft_shifted = np.fft.fftshift(dft)
● np.fft.fft2(): Computes the 2D Fast Fourier Transform (FFT) of the image, converting it
from the spatial domain to the frequency domain.
● np.fft.fftshift(): Shifts the zero-frequency component to the center of the frequency
spectrum, making it easier to apply filters.
dft_inverse_shifted = np.fft.ifftshift(filtered_dft)
img_back = np.fft.ifft2(dft_inverse_shifted)
img_back = np.abs(img_back)
return img_back
plt.figure(figsize=(5, 5))
plt.title('Inverse Filtered Image (Frequency Domain)')
plt.imshow(inverse_filtered_img, cmap='gray')
plt.show()
● Inverse Filtered Image: The result of inverse filtering is displayed, showing an image
that has been filtered to remove noise in the frequency domain.