Lab 5,6
Lab 5,6
Submitted By:
Section B
Submitted To:
Ms. Syeda Aimal Fatima
Department of Computer Science HITEC University Taxila
Lab Task
❖ Create a MATLAB program on script/.m file that takes an RGB image, convert into gray
❖ scale image and apply
❖ Smoothing Averaging filter and note the effects on given images.
❖ Weighted Averaging filter of size 3X3, 5X5 7x7 for both parts a and b
❖ Compare all the results with proper subploting
❖ Apply Gaussian filter of size 11 X 11.
❖ Apply median filter by using medfilt3 (image) command.
❖ Add salt & pepper noise
❖ Add Gaussian noise
❖ Apply max and min filter and plot their results.
Code
% Load an RGB image
image = imread('C:\Users\Students\Desktop\a\j.png'); % Replace
'your_image.jpg' with the actual image path
% Convert to grayscale
gray_image = rgb2gray(image);
% 4. Median filter (with salt & pepper noise and Gaussian noise)
salt_pepper_noise = imnoise(gray_image, 'salt & pepper', 0.02);
gaussian_noise = imnoise(gray_image, 'gaussian', 0, 0.01);
Output
Explanation
1. Grayscale Conversion:
o The script starts by converting an RGB image into grayscale to simplify the
analysis, reducing color channels to a single channel for intensity, making it easier
to apply spatial filters.
2. Smoothing Averaging Filter:
o A simple 3x3 averaging filter is applied to smooth the grayscale image, which
reduces noise by averaging the intensity of neighboring pixels.
3. Weighted Averaging Filter:
o Filters of different sizes (3x3, 5x5, 7x7) are applied. These weighted filters blur the
image, with larger kernels producing a more intense smoothing effect, showing how
filter size impacts the smoothness and detail retention.
4. Gaussian Filter:
o An 11x11 Gaussian filter, which uses a Gaussian distribution for pixel weighting,
provides more natural blurring than simple averaging. It maintains the image's
edges better while softening it.
5. Median Filter:
6. To address specific noise types, salt-and-pepper and Gaussian noise are added separately
to the grayscale image. The median filter is effective for removing salt-and-pepper noise
by replacing each pixel’s value with the median of the neighboring pixels, thus preserving
edges better than averaging filters.
7. Max and Min Filters:
8. The max filter enhances the brighter regions by replacing each pixel with the maximum
value in its neighborhood, while the min filter enhances darker areas by using the minimum
value. These filters are often used to highlight bright or dark details in an image.
Conclusion
This MATLAB script demonstrates various filtering techniques, each having unique effects on
image smoothness and detail preservation. Smoothing and weighted averaging filters reduce noise
but may blur edges, while the Gaussian filter provides smoother blurring with edge preservation.
Median filtering is particularly effective for removing salt-and-pepper noise while preserving
details. The max and min filters are useful for enhancing specific brightness features but may
distort the original structure if not used carefully. By comparing these techniques, one can choose
the appropriate filter based on the desired balance between noise reduction and detail preservation.