I have been assigned to implement the morphological dilation operation without using MATLAB's imdilate, and I came up with the following solution:
% -------------------------------------------------------------------------
% -> Morphological Operations
% -> Image Dilation
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% - [Pre-Operation Code: Getting Everything Ready] -
% -------------------------------------------------------------------------
% Starting With A Clean (Workspace) And (Command Window)
clear;
clc;
% Creating A Matrix Consisting Of 0s And 1s Representing A Binary Image.
binaryImage = [ ...
0 0 0 0 0 0
0 0 1 1 0 0
0 0 1 1 0 0
0 0 0 0 0 0];
% Creating A Matrix Representing Our Structuring Element
structuringElement = [ ...
0 1 0
1 1 1
0 1 0];
% Getting The Number Of Rows (Height) And The Number Of Columns (Width) Of The Binary Image
[imageRows, imageColumns] = size(binaryImage);
% Getting The Number Of Rows (Height) And The Number Of Columns (Width) Of The Structuring Element
[structuringRows, structuringColumns] = size(structuringElement);
% Creating An Empty Matrix That Will Be Used To Store The Final Processed Image
dilatedImage = zeros(imageRows, imageColumns);
ref = imdilate(binaryImage, structuringElement);
% -------------------------------------------------------------------------
% - [Dilation Operation] -
% -------------------------------------------------------------------------
% Going Over Each Row In The Binary Image
for i = 1:imageRows
% Going Over Each Column In The Binary Image
for j = 1:imageColumns
% If The Current Pixel Is A Foreground Pixel (1)
if (binaryImage(i, j) == 1)
% Going Over Each Row In The Structuring Element
for k = 1:structuringRows
% Going Over Each Column In The Structuring Element
for l = 1:structuringColumns
% If The Current Pixel In The Structuring Element Is A Foreground Pixel (1)
if (structuringElement(k, l) == 1)
dilatedImage(i + k - 2, j + l - 2) = 1;
end
end
end
end
end
end
subplot(1, 3, 1), imshow(binaryImage), title('Original Image');
subplot(1, 3, 2), imshow(dilatedImage), title('Dilated Image');
subplot(1, 3, 3), imshow(ref), title('MATLAB imdilate');
I am open to any suggestions! Thank you in advance.