|
|
| dilate.m |
% Below is the code for DILATION
% Initially color image is converted to grayscale image using rgb2gray()
% After that the obtained image is thresholded
% Next the DILATION operation is applied
%NOTE THAT THE CODE MAY BE LONG BUT THERE IS NEED FOR USING LOOPS TO OBTAIN
%DILATION IN THE DILATION STEP
clc; %Just clears the screen
x=imread('dil.jpg'); %Reads the image u can replace 'dil.jpg' to your own image
imshow(x,[]); %Displays the image
x=rgb2gray(x); %Converts COLOR image to GRAYSCALE image
x=double(x);
[a,b]=size(x); %As 'x' is matrix it assigns a=row_size & b=column_size
%Initialising y,dia arrays IT IS OPTIONAL
for i=1:a
for j=1:b
y(i,j)=0;
dia(i,j)=0;
end
end
%THRESHOLDING the GRAYSCALE image to BINARY image
tot=0;
for i=1:a
for j=1:b
tot=tot+x(i,j);
end
end
thr=tot/(a*b);
for i=1:a
for j=1:b
if(x(i,j)<thr)
y(i,j)=0;
else
y(i,j)=1;
end
end
end
figure;
imshow(y,[]);
%DILATION CODE
se=[1 1 ;1 1]; %This is the structuring element I used of size 2X2 U can change it to any size U want.
for i=1:a-1 %As I used 2X2 structuring element 'se', I used 'a-1' if U use structuring of mXn size use 'a-(m-1)'
for j=1:b-1 %Same for 'b-(n-1)' if mXn matrix is used
if(y(i,j)==1)
for i1=0:1
for j1=0:1
if(dia(i+i1,j+j1)~=1)
dia(i+i1,j+j1)=se(i1+1,j1+1);
end
end
end
end
end
end
figure;
imshow(dia,[]); %Displaying the dilated image
|
|
Contact us at files@mathworks.com