0% found this document useful (0 votes)
9 views6 pages

Manishaaaaaaaaa

The document outlines a lab experiment focused on constructing 5x5 Gaussian and Median filters to reduce salt-and-pepper noise in images using MATLAB. It explains the theory behind image processing, the characteristics of both filters, and provides MATLAB code for implementation. The results indicate successful execution of the code, demonstrating the effectiveness of both filters in noise reduction.

Uploaded by

Ananya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Manishaaaaaaaaa

The document outlines a lab experiment focused on constructing 5x5 Gaussian and Median filters to reduce salt-and-pepper noise in images using MATLAB. It explains the theory behind image processing, the characteristics of both filters, and provides MATLAB code for implementation. The results indicate successful execution of the code, demonstrating the effectiveness of both filters in noise reduction.

Uploaded by

Ananya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Digital Image Processing Lab (ECN-16102)

Submitted by
Manisha Patel
Registration No. 20224094
B.Tech 6th Semester

Submitted To
Dr. Sumit Kumar Jha

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING


MOTILAL NEHRU NATIONAL INSTITUTE OF TECHNOLOGY, ALLAHABAD
PRAYAGRAJ- 211004, INDIA
Date:15/01/2025 EXPERIMENT 3
AIM: Write a program to construct 5*5 Gaussian and Median filter and apply on an image corrupted
by salt and pepper noise.

Tools Used: MATLAB Software 2023b


Theory:
Image processing is a fundamental technique used to enhance, analyze, and extract meaningful
information from digital images. Real-world images often suffer from noise, which degrades image
quality. Salt-and-pepper noise is a type of impulse noise that appears as random black and white
pixels in an image. To reduce such noise, filters like Gaussian and Median filters are commonly used.

Noise in Images
• Salt-and-Pepper Noise: This type of noise occurs due to sharp and sudden disturbances in
image signals. It appears as randomly scattered white (salt) and black (pepper) pixels in the
image.
• Cause: It often arises due to transmission errors, faulty sensors, or environmental factors
during image acquisition.

Gaussian Filter
• Concept: The Gaussian filter is a linear smoothing filter used to reduce noise and blur an
image. It is based on the Gaussian function, which assigns weights to neighboring pixels
according to their distance from the center.
• Characteristics:
o Smooths noise while preserving edges to some extent.
o The kernel is symmetric and isotropic (same in all directions).
o Size of the filter (e.g., 5×5) determines the level of smoothing.

Median Filter
• Concept: The Median filter is a non-linear filter used to effectively remove salt-and-pepper
noise. It works by replacing each pixel's value with the median value of the intensities in its
neighborhood.
• Steps in Median Filtering:
1. Define a square filter kernel (e.g., 5×5) centered on the target pixel.
2. Extract all pixel values within the kernel.
3. Sort the pixel values and select the median.
4. Replace the target pixel's value with the median.
• Advantages:
o Excellent at removing impulse noise like salt-and-pepper noise.
o Preserves edges better than linear filters (e.g., Gaussian or mean filters).

Comparison: Gaussian vs. Median Filters

Aspect Gaussian Filter Median Filter

Type Linear Filter Non-linear Filter

Noise Handling Effective for Gaussian noise Best for salt-and-pepper noise

Edge Preservation Partial preservation Better edge preservation

Complexity Computationally simpler Relatively more complex

Applications of Gaussian and Median Filters


1. Medical Imaging:
o Reducing noise in MRI or CT scan images.
2. Photography:
o Smoothing and enhancing images captured in noisy environments.
3. Computer Vision:
o Preprocessing images for tasks like object detection and recognition.
4. Remote Sensing:
o Reducing noise in satellite imagery.
MATLAB Code:
image = imread('pics/e01.png');
if size(image, 3) == 3
image = rgb2gray(image);
end

% salt-and-pepper noise
noisyImage = imnoise(image, 'salt & pepper', 0.02);

figure;
subplot(2, 2, 1);
imshow(image);
title('Original Image');
subplot(2, 2, 2);
imshow(noisyImage);
title('Noisy Image');

filterSize = 5;
halfSize = floor(filterSize / 2);

% 5x5 Gaussian filter


sigma = 1.0;
[x, y] = meshgrid(-halfSize:halfSize, -halfSize:halfSize);
gaussianFilter = exp(-(x.^2 + y.^2) / (2 * sigma^2));
gaussianFilter = gaussianFilter / sum(gaussianFilter(:));

[rows, cols] = size(noisyImage);


gaussianFiltered = zeros(size(noisyImage));
noisyImage = double(noisyImage);

for i = 1 + halfSize : rows - halfSize


for j = 1 + halfSize : cols - halfSize
patch = noisyImage(i-halfSize:i+halfSize, j-halfSize:j+halfSize);
gaussianFiltered(i, j) = sum(sum(patch .* gaussianFilter));
end
end
gaussianFiltered = uint8(gaussianFiltered);

% Median filter
medianFiltered = zeros(size(noisyImage));
for i = 1 + halfSize : rows - halfSize
for j = 1 + halfSize : cols - halfSize
patch = noisyImage(i-halfSize:i+halfSize, j-halfSize:j+halfSize);
medianFiltered(i, j) = median(patch(:));
end
end
medianFiltered = uint8(medianFiltered);

subplot(2, 2, 3);
imshow(gaussianFiltered);
title('Gaussian Filtered Image');

subplot(2, 2, 4);
imshow(medianFiltered);
title('Median Filtered Image');
Commands used:
1. size(image, 3)
• Description: Determines the size of the third dimension of the image. If it equals 3, the image
is in RGB format (i.e., it has three color channels: Red, Green, and Blue).
2. rgb2gray(image)
• Description: Converts an RGB (color) image into a grayscale image. This is necessary
because most image processing techniques work on single-channel grayscale images.
3. imnoise(image, 'salt & pepper', 0.02)
• Description: Adds salt-and-pepper noise to the image with a noise density of 0.02. This
simulates the presence of impulse noise, where random black and white pixels appear in the
image.
4. meshgrid(-halfSize:halfSize, -halfSize:halfSize)
• Description: Generates two 2D grids, x and y, for the specified range. These grids are used to
calculate the Gaussian kernel values based on distance from the center.
5. exp(-(x.^2 + y.^2) / (2 * sigma^2))
• Description: Computes the Gaussian function for each position in the 2D grid. The result is a
5×5 Gaussian filter for smoothing the image.
6. sum(gaussianFilter(:))
• Description: Computes the sum of all elements in the Gaussian filter to normalize it, ensuring
the filter coefficients sum to 1.
7. for Loops (for i and for j)
• Description: Iterates over the rows and columns of the noisy image, applying the Gaussian
and Median filters to each pixel's neighborhood (5×5 patch).
8. patch .* gaussianFilter
• Description: Multiplies the 5×5 patch of the image by the Gaussian filter element-wise to
compute the weighted sum for the Gaussian filter.
9. median(patch(:))
• Description: Finds the median value of all pixel intensities in the 5×5 patch for the Median
filter. The median value is used to replace the central pixel.
10. uint8(array)
• Description: Converts the filtered image arrays back to 8-bit unsigned integers for proper
display and storage as images.
Observation:-

Result: The above code is successfully run on MATLAB.

You might also like