An noisy image can be denoised using several different filters. In class, we discussed both the averaging and the median filter. • Write a Matlab script to denoise the images peppersNoisel.tiff and peppersNoise2.tiff using both the median filter and the averaging filter. Examine the effect of using different filter window sizes including 3 × 3 pixels and 5 × 5 pixels. From your results, what are the advantages or disadvantages of each filter? What are the advantages or disadvantages of changing the filter size? Include your denoised images in your report. • In many cases, edges must be extracted from a noisy image. If this is necessary, it is important to denoise the image first. Write a Matlab script that generates an edgemap from the 3 × 3 averaging filtered and median filtered versions of peppersNoisel.tiff. Create the edgemap by first calculating the image’s gradient using the Sobel filters

(4) to approximate the gradient in the row and column directions, then by comparing the magnitude of the gradient to a threshold. Use the same threshold for both images when generating the edgemap. Please comment your code and append it to your report. What difference do you see between the edgemaps of each filtered image? Which filter has better edge preserving properties?

The Correct Answer and Explanation is:

Below is the correct MATLAB script for denoising the images peppersNoisel.tiff and peppersNoise2.tiff using both median and averaging filters with different window sizes (3×3 and 5×5). It also generates edgemaps after filtering peppersNoisel.tiff using Sobel filters for edge detection. The code is fully commented for clarity.

MATLAB Code:

matlabCopyEdit% Read the noisy images
image1 = imread('peppersNoisel.tiff');
image2 = imread('peppersNoise2.tiff');

% Display original noisy images
figure, imshow(image1), title('Original PeppersNoisel.tiff');
figure, imshow(image2), title('Original PeppersNoise2.tiff');

% Apply 3x3 median filter
med3_image1 = medfilt2(image1, [3 3]);
med3_image2 = medfilt2(image2, [3 3]);

% Apply 5x5 median filter
med5_image1 = medfilt2(image1, [5 5]);
med5_image2 = medfilt2(image2, [5 5]);

% Apply 3x3 averaging filter
avg3_filter = fspecial('average', [3 3]);
avg3_image1 = imfilter(image1, avg3_filter, 'replicate');
avg3_image2 = imfilter(image2, avg3_filter, 'replicate');

% Apply 5x5 averaging filter
avg5_filter = fspecial('average', [5 5]);
avg5_image1 = imfilter(image1, avg5_filter, 'replicate');
avg5_image2 = imfilter(image2, avg5_filter, 'replicate');

% Display filtered images
figure, imshow(med3_image1), title('Median Filter 3x3 - PeppersNoisel');
figure, imshow(med5_image1), title('Median Filter 5x5 - PeppersNoisel');
figure, imshow(avg3_image1), title('Averaging Filter 3x3 - PeppersNoisel');
figure, imshow(avg5_image1), title('Averaging Filter 5x5 - PeppersNoisel');

figure, imshow(med3_image2), title('Median Filter 3x3 - PeppersNoise2');
figure, imshow(med5_image2), title('Median Filter 5x5 - PeppersNoise2');
figure, imshow(avg3_image2), title('Averaging Filter 3x3 - PeppersNoise2');
figure, imshow(avg5_image2), title('Averaging Filter 5x5 - PeppersNoise2');

%% Edge Detection using Sobel filters on PeppersNoisel after filtering

% Apply 3x3 median filter
med3_image = medfilt2(image1, [3 3]);
% Apply 3x3 averaging filter
avg3_filter = fspecial('average', [3 3]);
avg3_image = imfilter(image1, avg3_filter, 'replicate');

% Compute gradients using Sobel filters
sobel_x = fspecial('sobel'); % Horizontal gradient filter
sobel_y = sobel_x';          % Vertical gradient filter

% Gradient of median filtered image
grad_x_med = imfilter(double(med3_image), sobel_x, 'replicate');
grad_y_med = imfilter(double(med3_image), sobel_y, 'replicate');
gradient_mag_med = sqrt(grad_x_med.^2 + grad_y_med.^2);

% Gradient of averaging filtered image
grad_x_avg = imfilter(double(avg3_image), sobel_x, 'replicate');
grad_y_avg = imfilter(double(avg3_image), sobel_y, 'replicate');
gradient_mag_avg = sqrt(grad_x_avg.^2 + grad_y_avg.^2);

% Threshold for edge detection
threshold = 50;

% Generate edge maps
edges_med = gradient_mag_med > threshold;
edges_avg = gradient_mag_avg > threshold;

% Display edge maps
figure, imshow(edges_med), title('Edge Map - Median Filtered Image');
figure, imshow(edges_avg), title('Edge Map - Averaging Filtered Image');

Explanation

The noisy images were denoised using two common techniques: median filtering and averaging filtering. The median filter replaces each pixel with the median value in its neighborhood, which is effective in removing salt-and-pepper noise while preserving edges. The averaging filter computes the mean of the surrounding pixels, which smoothens the image but tends to blur edges.

Both filters were applied using 3×3 and 5×5 window sizes. The 3×3 filters are less aggressive, preserving more image detail, while 5×5 filters provide stronger noise reduction at the cost of more smoothing and potential loss of fine details.

For edge detection, it is essential to reduce noise first because noise can create false edges. Sobel filters were used to compute image gradients in both horizontal and vertical directions. The magnitude of the gradient highlights regions with significant intensity changes, which typically correspond to edges. A threshold of 50 was applied to identify the edges.

The edge map from the median filtered image showed better edge preservation. Median filtering effectively suppresses noise while maintaining sharp edges, making the edges more distinct. In contrast, the averaging filter smoothed the image but also blurred the edges, making them less prominent in the edge map.

Overall, the median filter is better for edge preservation and noise removal in images with salt-and-pepper noise. The averaging filter is suitable for reducing Gaussian-like noise but may lead to loss of edge sharpness. Increasing the filter size boosts noise reduction but can degrade image detail and edges, so the filter size should be chosen based on the specific application.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *