Average Filter
% Read the image
I = imread('[Link]');
% Create a 3x3 averaging kernel (all ones divided by 9)
kernel = ones(3, 3) / 9;
% Apply the filter using imfilter
I_avg_filtered = imfilter(I, kernel);
% Display the results
figure, imshow(I);
title('Original Image');
figure, imshow(I_avg_filtered);
title('Filtered Image with imfilter');
Gaussian Filter
% Read the image
I = imread('[Link]');
% Apply a Gaussian filter with a standard deviation of 2
% The standard deviation (sigma) controls the amount of blurring
I_gaussian_filtered = imgaussfilt(I, 2);
% Display the original and filtered images
figure, montage({I, I_gaussian_filtered});
title('Original Image (left) vs. Gaussian Filtered Image (right)');
Median Filter
% Create a sample image and add salt-and-pepper noise for demonstration
I = imread('[Link]');
I_noisy = imnoise(I, 'salt & pepper', 0.05); % Add 5% noise
% Apply a 3x3 median filter
I_median_filtered = medfilt2(I_noisy, [3 3]);
% Display the original, noisy, and filtered images%
figure;
subplot(1,3,1), imshow(I), title('Original');
subplot(1,3,2), imshow(I_noisy), title('Noisy Image');
subplot(1,3,3), imshow(I_median_filtered), title('Median Filtered');
TYPES OF SPATIAL FILTER
LINEAR SPATIAL FILTER
Smoothing Filters (Low-pass filters):
These filters blur the image and are used to reduce noise and remove fine details.
Examples include the average filter (all weights are equal) and the Gaussian filter
(weights are distributed according to a Gaussian curve).
Sharpening Filters (High-pass filters):
These filters enhance edges and fine details by highlighting areas of sharp
intensity changes. They typically use kernels with a central positive value and
surrounding negative values.
Non-linear Filters
Non-linear filters perform an operation on the neighborhood pixels that is not a
simple weighted sum.
Median Filter:
This filter is highly effective at removing "salt-and-pepper" noise. It replaces the
center pixel's value with the median (middle) value of all the pixels in the
neighborhood.
Min/Max Filters:
These filters replace the pixel with the minimum or maximum value in the
neighborhood, often used for finding the darkest or brightest points in an image.