0% found this document useful (0 votes)
6 views5 pages

Lab 5,6

The document details a lab report on digital image processing using MATLAB, focusing on various image filtering techniques. It includes tasks such as converting an RGB image to grayscale, applying different smoothing and weighted averaging filters, Gaussian and median filters, and adding noise to analyze the effects of these filters. The conclusion emphasizes the importance of selecting appropriate filters based on their impact on noise reduction and detail preservation.
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)
6 views5 pages

Lab 5,6

The document details a lab report on digital image processing using MATLAB, focusing on various image filtering techniques. It includes tasks such as converting an RGB image to grayscale, applying different smoothing and weighted averaging filters, Gaussian and median filters, and adding noise to analyze the effects of these filters. The conclusion emphasizes the importance of selecting appropriate filters based on their impact on noise reduction and detail preservation.
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/ 5

Course: CS-406 Digital Image Processing

Lab Report 05&6

Submitted By:

Muhammad Jahangir (21-CS-089)

Basit Ali (21-CS-104)

Muhammad Alrayan (21-CS-107)

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);

% Display the original and grayscale images


figure;
subplot(3, 4, 1); imshow(image); title('Original RGB Image');
subplot(3, 4, 2); imshow(gray_image); title('Grayscale Image');

% 1. Smoothing Averaging filter (simple averaging)


avg_filter = fspecial('average', [3, 3]);
smooth_image = imfilter(gray_image, avg_filter, 'symmetric');
subplot(3, 4, 3); imshow(smooth_image); title('3x3 Smoothing Avg Filter');

% 2. Weighted Averaging filter for different sizes (3x3, 5x5, 7x7)


weights_3x3 = fspecial('average', [3, 3]);
weights_5x5 = fspecial('average', [5, 5]);
weights_7x7 = fspecial('average', [7, 7]);

weighted_avg_3x3 = imfilter(gray_image, weights_3x3, 'symmetric');


weighted_avg_5x5 = imfilter(gray_image, weights_5x5, 'symmetric');
weighted_avg_7x7 = imfilter(gray_image, weights_7x7, 'symmetric');

subplot(3, 4, 4); imshow(weighted_avg_3x3); title('3x3 Weighted Avg Filter');


subplot(3, 4, 5); imshow(weighted_avg_5x5); title('5x5 Weighted Avg Filter');
subplot(3, 4, 6); imshow(weighted_avg_7x7); title('7x7 Weighted Avg Filter');

% 3. Gaussian filter of size 11x11


gaussian_filter = fspecial('gaussian', [11, 11], 1.5);
gaussian_image = imfilter(gray_image, gaussian_filter, 'symmetric');
subplot(3, 4, 7); imshow(gaussian_image); title('Gaussian Filter 11x11');

% 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);

med_salt_pepper = medfilt2(salt_pepper_noise, [3, 3]);


med_gaussian = medfilt2(gaussian_noise, [3, 3]);

subplot(3, 4, 8); imshow(salt_pepper_noise); title('Salt & Pepper Noise');


subplot(3, 4, 9); imshow(med_salt_pepper); title('Median Filter (Salt &
Pepper)');

subplot(3, 4, 10); imshow(gaussian_noise); title('Gaussian Noise');


subplot(3, 4, 11); imshow(med_gaussian); title('Median Filter (Gaussian
Noise)');

% 5. Max and Min filters


max_filter = ordfilt2(gray_image, 9, ones(3, 3)); % Max filter with 3x3
neighborhood
min_filter = ordfilt2(gray_image, 1, ones(3, 3)); % Min filter with 3x3
neighborhood

subplot(3, 4, 12); imshow(max_filter); title('Max Filter');


figure;
imshow(min_filter); title('Min Filter');

% End of the script

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.

You might also like