Manishaaaaaaaaa
Manishaaaaaaaaa
Submitted by
Manisha Patel
Registration No. 20224094
B.Tech 6th Semester
Submitted To
Dr. Sumit Kumar Jha
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).
Noise Handling Effective for Gaussian noise Best for salt-and-pepper noise
% 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);
% 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:-