Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Boundary Extraction using Dilation

BAPUJI EDUCATIONAL ASSOCIATION ®


BAPUJI INSTITUTE OF ENGINEERING AND TECHNOLOGY DAVANAGERE – 577004
DEPARTMENT OF ELECTRONICS & COMMUNICATION
ENGINEERING 2020 – 21

ASSIGNMENT ON
DEMONSTRATION OF APPLICATIONS OF MEDIAN
FILTERING AND DILATION USING MATLAB

APPLICATIONS OF MEDIAN FILTERING

1. Median Filter for Noise Filtering

Median filtering is a kind of order-statistic (nonlinear) filters. In order to perform median filtering at a
point in an image, we first sort the values of the pixel in the neighborhood, determine their median,
and assign that value to the corresponding pixel in the filtered image. This image is best suited for salt
and pepper noise, and it can be used to remove this kind of noise present in image as demonstrated by
the below code.

The code inputs an image and adds a noise to it. Later the noise is filtered by replacing each
pixel by the median of its neighborhood

Code
clc
clear all
close all
es=imread('rice.png');
imshow(es);
title('Original Image');
es=imnoise(es,'salt & pepper',0.1);
b=es;
[r c]=size(es);
for i=2:r-1
for j=2:c-1
mat=[es(i-1,j-1),es(i-1,j),es(i-1,j+1),es(i,j-1),es(i,j),...

~1~
Assignment using Matlab Digital Image Processing [17EC72]
Boundary Extraction using Dilation

es(i,j+1),es(i+1,j-1),es(i+1,j),es(i+1,j+1)];
mat=sort(mat); b(i,j)=mat(5);
end
end
figure;imshow(es);title('Image corrupted with Salt & Pepper Noise');
figure;imshow(b);title('Image after filtering');

OUTPUTS
Original Image Image corrupted with Salt & Pepper Noise

Image after filtering

2. Median Filter for filling Holes

The same median filter can be used to fill the small holes present in the image. This is evident
because: as the pixel representing the holes will be subjected to median filtering it will be replaced by
the median of its neighborhood and therefore the hole will seem as if it is filled by the surrounding
colour.

The code inputs an grayscale image, (In our case it is of coins) and converts it to binary image
by applying thresholding function on it. Histogram of the image is used to find the threshold value for

~2~
Assignment using Matlab Digital Image Processing [17EC72]
Boundary Extraction using Dilation

the threshlding function (here we have chosen as 100), Later it applies median filter on it as a result if
there are any holes they seem to be filled by the surrounding colour.

Code
clc
clear all
close all
warning off
I=imread('coins.png');
figure;
imshow(I);title('Original Image');
figure;
%Finding threshold by analysing histogram of image
imhist(I);title('Histogram Of Image')
axis tight;
%Converting the image to binary
g=I>100;
figure;
imshow(g);title('Binary form of image')
ms=medfilt2(g,[5,5]);
figure;
imshow(ms);title('Image with no holes')
[a b]=bwlabel(ms);

~3~
Assignment using Matlab Digital Image Processing [17EC72]
Boundary Extraction using Dilation

OUTPUTS

Original Image

Histogram Of Image
4000

3500

3000

2500

2000

1500

1000

500

0 50 100 150 200 250

Binary form of image Image with no holes

~4~
Assignment using Matlab Digital Image Processing [17EC72]
Boundary Extraction using Dilation

~5~
Assignment using Matlab Digital Image Processing [17EC72]
Boundary Extraction using Dilation

APPLICATIONS OF DILATION

1. Repairing Brakes Using Dilation

Dilation is a kind of morphological image processing. It uses a structural element for its operation.
The structural element is placed as kernel over each pixel of the binary image if the if neighborhood
doesn’t contain a pixel then it adds a pixel to its neighborhood. And hence the dilation can be used to
repair brakes in the image, as the brake in the image will be filled by performing dilation.

The code accepts an binary mage and performs dilation (using inbuilt function) as a result the
brakes in the image are filled as shown in the images

Code
clc
clear all
close all
warning off
oi = imread('Sample.tif');
%Converting the image from m×n×4 to m×n×3
%by taking first 3 layers
oi = oi(:,:,1:3);
x = im2bw(oi);
imshow(x);
s = strel('disk',30);
g = imdilate(x,s);
imshow(x);
title('Original Image');
figure(2)
imshow(g);title ('Image After performing dilation');

OUTPUTS

Original Image Image After performing dilation

~6~
Assignment using Matlab Digital Image Processing [17EC72]
Boundary Extraction using Dilation

2. Boundary Extraction using Dilation

The dilation on the image can also be used to extract the boundary of the image. The concept behind
the code is like this: The binary image inputted is subjected to dilation, this adds an outer layer to the
image same as the colour of the image in its boundary. And if we subtract the original image from the
dilated image we will get the boundary of the image.

Code
clc
clear all
close all
warning off
%Reading an gray image since the image is gray
%performing rgb2gray is not reuired
x=imread('Original.tif');
se=strel('disk',5);
A=imdilate(x,se);
A=A-x;
A=imresize(A,2);
x=imresize(x,2);
imshow(x);title('Original Image');
figure (2)
imshow(A);title('Boundary of Image');

OUTPUTS

Original Image Boundary of Image

~7~
Assignment using Matlab Digital Image Processing [17EC72]

You might also like