Image Smoothing Filters: Implementation and Analysis
Overview
Image smoothing represents a fundamental technique in digital image processing, employed to reduce noise and suppress unwanted details. This experiment explores spatial domain filtering methods, specifically focusing on mean filtering and median filtering operations.
Theoretical Background
Spatial Domain Smoothing
Spatial domain filtering operates directly on image pixels, modifying values based on their local neighborhood. The core principle involves computing a representative value from surrounding pixels to replace the current pixel intensity.
Mean Filter Operation
The mean filter (also termed arithmetic mean filter) computes the average intensity within a sliding window. For each pixel position, the filter calculates:
output(x,y) = (1/MN) × Σ f(i,j)
where M×N represents the kernel dimensions, and the summation covers all pixels within the neighborhood.
Large kernel sizes produce progressively smoother output but sacrifice detail preservation. The trade-off between noise reduction and blur remains a fundamental consideration in filter design.
Noise Characteristics
Salt and Pepper Noise
This impulse noise manifests as random black and white pixels scattered across the image. Such noise typically originates from sensor malfunctions or transmission errors.
The median filter proves particularly effective against impulse noise. Unlike mean filtering, median filtering selects the middle value from sorted neighborhood intensities:
output(x,y) = median{f(i,j) for (i,j) in neighborhood}
This non-linear approach preserves edges while eliminating isolated noise points.
Implementation
Core Processing Functions
Mean Kernel Generation
function kernel = createMeanKernel(windowSize)
kernel = ones(windowSize) / (windowSize * windowSize);
end
Convolution Operation
function result = applyConvolution(image, kernel)
[imgH, imgW] = size(image);
[kH, kW] = size(kernel);
padY = floor(kH / 2);
padX = floor(kW / 2);
extended = padarray(image, [padY, padX], 0, 'both');
result = zeros(imgH, imgW);
for y = 1:imgH
for x = 1:imgW
region = extended(y:y+kH-1, x:x+kW-1);
result(y, x) = sum(region(:) .* kernel(:));
end
end
result = uint8(result);
end
Median Filtering
function output = performMedianFilter(image)
[height, width] = size(image);
output = image;
for y = 2:height-1
for x = 2:width-1
neighborhood = image(y-1:y+1, x-1:x+1);
sortedValues = sort(neighborhood(:));
output(y, x) = sortedValues(5);
end
end
end
Main Processsing Script
clear; close all;
originalImage = imread('lena.jpg');
grayImage = rgb2gray(originalImage);
figure;
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image');
windowDimensions = [3, 7, 25];
for idx = 1:numel(windowDimensions)
dim = windowDimensions(idx);
kernel = createMeanKernel(dim);
processed = applyConvolution(grayImage, kernel);
subplot(2, 2, idx + 1);
imshow(processed);
title(sprintf('%dx%d Mean Filter', dim, dim));
end
figure;
distortedImage = imnoise(grayImage, 'salt & pepper', 0.1);
subplot(2, 2, 1);
imshow(grayImage);
title('Original');
subplot(2, 2, 2);
imshow(distortedImage);
title('Noisy Image (Salt Pepper)');
meanProcessed = applyConvolution(distortedImage, createMeanKernel(3));
subplot(2, 2, 3);
imshow(meanProcessed);
title('3×3 Mean Filter Result');
medianProcessed = performMedianFilter(distortedImage);
subplot(2, 2, 4);
imshow(medianProcessed);
title('3×3 Median Filter Result');
Analysis
Mean Filter Behavior
The experimental results demonstrate that mean filtering effectively reduces random noise through intensity averaging. However, increased kernel dimensions produce excessive blur, progressively degrading image structure. A 25×25 kernel renders the image unrecognizable despite maximal noise suppression.
Median Filter Performance
When porcessing salt-and-pepper contaminated images, significant differences emerge between linear and non-linear approaches. Mean filtering reduces noise visibility but introduces considerable blur, compromising both noise removal and detail preservation.
Median filtering achieves superior results, effectively eliminating impulse noise while maintaining edge integrity and preserving fine image details. This characteristic makes median filtering the preferred choice for impulse noise applications.