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

DIP Lab.5 L5

The document outlines a laboratory exercise on spatial filters used in digital image processing for noise reduction and image enhancement. It describes three types of filters: mean filters, median filters, and enhancement filters, detailing their functions and applications. Additionally, it provides MATLAB scripts for tasks involving the removal of noise and highlighting image details using these filters.

Uploaded by

sky818086
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)
26 views5 pages

DIP Lab.5 L5

The document outlines a laboratory exercise on spatial filters used in digital image processing for noise reduction and image enhancement. It describes three types of filters: mean filters, median filters, and enhancement filters, detailing their functions and applications. Additionally, it provides MATLAB scripts for tasks involving the removal of noise and highlighting image details using these filters.

Uploaded by

sky818086
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

Digital Image Processing Laboratory

Lab.5: Spatial Filters.


❖ Spatial filtering is typically applied for noise mitigation or to perform some type of image
Enhancement.
❖ The three types of filters are
(1) Mean filters,
(2) Median filters and
(3) Enhancement filters
❖ The first two are used primarily to deal with noise in images and add a “softer” look to an image.
❖ The enhancement filters highlight edges and details within the image.

➢ Mean filters are averaging filters that replace the center pixel with the average of neighboring pixel values, is a
linear filter.

Mask=

➢ Median filter: Sorts the pixel values in a small neighborhood and replaces the center pixel with the middle
value in the sorted list, is a nonlinear filter.
➢ Enhancement filters:
I. Laplacian Filter: Will bring out image details equally in all directions.

II. Difference Filters will enhance details in the direction specific to the mask selected.
❖ Noise is any undesired information that contaminates an image.

I(r,c) y(r,c)=I(r,c)+n(r,c)

Image noisy image

n(r,c)
noise
❖ we can add a noise to an image by using the MATLAB function:
y=imnoise(I,Type,Parameters)
where
- TYPE is a string that can have one of these values
'salt & pepper' "On and Off" pixels
'gaussian' Gaussian white noise with constant mean and variance
- Parameters:
M (mean) default to 0
'gaussian'
V (variance) default to 0.01
'salt & pepper' D (noise density) default to 0.05
 NOTES:
1- In mean filtering we will use conv2 MATLAB function
2- In median filtering we will use medfilt2 MATLAB function which has the following syntax:

B = medfilt2(A, [M N])

Where
A is the input image
[M N] is the size of window used for median filtering. 3 × 3 or 5 × 5 or 7 × 7 are typical values for the
window size.

➢ [Task 1: Using Mean and Median Filters to Remove Noise]


Write and execute the following script in MATALB, then write your observations on the result:
clc
clear
close all
I=imread('eight.tif');
Isp=imnoise(I,'salt & pepper',0.03);
Ig=imnoise(I,'gaussian',0.02);
mask=ones(3)/9;
y1=conv2(im2double(Isp),mask,'same');
y2=conv2(im2double(Ig),mask,'same');
y1=im2uint8(y1);
y2=im2uint8(y2);
y3=medfilt2(Isp,[3 3]);
y4=medfilt2(Ig,[3 3]);
subplot(2,3,2),imshow(I)
subplot(2,3,4),imshow(Isp)
subplot(2,3,6),imshow(Ig)
figure,
subplot(1,2,1),imshow(y1),title('remove salt and pepper noise using
mean filter')
subplot(1,2,2),imshow(y2),title('remove gaussian noise using mean
filter')
figure,
subplot(1,2,1),imshow(y3),title('remove salt and pepper noise using
median filter')
subplot(1,2,2),imshow(y4),title('remove gaussian noise using median
filter')

Image with salt and pepper noise image with gaussian noise
1
salt and pepper noise remove using median filter gaussian noise remove using median filter

➢ [Task 2: Using Enhancement Filters to Highlight the Edges and Details of an Image]
Write and execute the following script in MATALB, then write your observations on the result:
clc
clear
close all
I=imread('cameraman.tif');
m=[0 -1 0;-1 5 -1;0 -1 0];
y=imfilter(I,m,'conv');
m1=[0 1 0;0 1 0;0 -1 0];
m2=[0 0 0;1 1 -1;0 0 0];
m3=[1 0 0;0 1 0;0 0 -1];
m4=[0 0 1;0 1 0;-1 0 0];
y1=imfilter(I,m1,'conv');
y2=imfilter(I,m2,'conv');
y3=imfilter(I,m3,'conv');
y4=imfilter(I,m4,'conv');
imshow(I)
figure,imshow(y),title('laplacian filter')
figure,imshow(y1),title('enhanced edges in horizontal direction')
figure,imshow(y2),title('enhanced edges in vertical direction')
figure,imshow(y3),title('enhanced edges in right diagonal direction')
figure,imshow(y4),title('enhanced edges in left diagonal direction')

laplacian filter Enhanced edges in horizontal direction

Enhanced edges in vertical direction Enhanced edges in right diagonal direction

Enhanced edges in left diagonal direction

Eng./Maged Albadany

You might also like