You are on page 1of 11

SPATIAL FILTER

Average Filter Implementation


Median Filter Implementation
Gaussian Filter Implementation

1
SPATIAL FILTER- Filters

Commands used : imread( ),imshow(), subplot/figure


rgb2gray, vector generation, sum function ,ones,

2
imread() & imshow()

 Reads a jpg file stores in drive or file.


A1= imread(‘try1.jpg’)

imshow(image1)
Displays the grayscale image

imshow(image1,[])
Displays the grayscale image with max and min range
subplot() & figure()

subplot(m,n,x)
m ---No. of rows
n --- No. of columns
x --- Figure no.
subplot(3,1,2)
m=3, n= 1 x=2
figure( 2)
2: handler
Vector generation
 a= [ 1 2 3 4 ];
A1= [ 1 2 3 4 ;
2 4 6 8;
3 6 9 12;
4 8 12 16 ]
 average generation : n= [ 1 2 3 4]
sum (n) ( sums all functions row wise)
N=sum( A1)
B= ones(4)
B1= ones(4,4) %B=B1
conv2
• avgfiltmask= A1/N
• Data: double
• Convert to double
• double(avgfiltmask) double(image2)
• Convolution output is single if one input or both is single.Else
double
• conv : image ( M*N) full,same,valid
• 'full' — function gives the full 2-D convolution.( M+N-1)
• 'same' —central part of the convolution, which is the same size
as A.(M*N)
• 'valid' — convolution without zero-padded edges.
• For a filter of size (2K+1) × (2L+1), if the image size is MxN, the
filter is computed over the range:
• K=1(3×3), L=1 (3×3) M×N = 256×256
• 1≤u’ ≤ 254 (256-1-1) 1≤v’ ≤ 254 (256-1-1)
Task
 Read an image, check its size, convert to gray scale and set as
double and as the same image. Plot the image.
 Build an average filter of size( 5*5) using ones function and
using vector generation.Plot the filter.
 Convolve the filter and observe output.Plot all outputs. rgb
image,Grayscale img, convolved image 1, convolved image 2
 Plot 4 images.Verify that the last and before last outputs
( both result of convolved images) are equal.
Salt and Pepper Noise
• Form of impulse noise( sudden disturbances, sensor faults)
• Eg. MRI scan
• Certain amount of the pixels in the image are either black
or white
• Random bright (with 255 pixel value) & Random dark
(with 0 pixel value)
• Data drop noise in statistics
• Drop the original data values
Salt and Pepper Noise

• imnoise function
• A = imnoise(image1,'salt & pepper‘,val);
• val: density of noise, default value is 5% of total
pixels present in I

9
Average Filter Implementation Algorithm

 Read the colour image using imread( ).Resize image


Convert to gray using rgb2gray
 Generate the average filter mask
 Apply convolution to the gray image
Observe output using the function imshow( )
Add salt and pepper noise( 0.5) to the gray image
Apply convolution to the noisy gray image
Observe output using the function imshow( )
Average Filter Code

You might also like