Mube
Mube

Reputation: 21

What is the result of dilation of this image?

I have this following matrix:

 0     0     0     0     0     0     0
 0     0     0     0     0     0     0
 0     0     0     0     0     0     0
 0     0     0     0     0     0     0
 1     1     0     0     0     0     0
 1     0     0     0     0     1     1
 1     0     0     0     0     1     1

And I want to dilate it with the following structuring element:

 1     0     0
 1     1     1
 1     1     1

I already did dilation on Matlab but the result does not match with the one I did by hand. So I guess I missing something here. As far as I know if any '1' of the structuring element touches any of the '1' in the matrix, then it means it is a hit and the center of the current window should be set as 1. If I make dilation in such fashion I would get following (without considering edges):

 0     0     0     0     0     0     0
 0     0     0     0     0     0     0
 0     0     0     0     0     0     0
 0     1     1     0     0     0     0
 1     1     1     0     1     1     0
 1     1     1     0     1     1     1
 1     0     0     0     0     1     1

But Matlab gives the following result:

 0     0     0     0     0     0     0
 0     0     0     0     0     0     0
 0     0     0     0     0     0     0
 1     **0**     **0**     0     0     0     0
 1     1     1     0     1     1     0
 1     1     1     0     1     1     1
 1     1     0     0     1     1     1

Without considering edges it almost looks like my result but pixels styled with bold are '1' in my result but '0' in Matlab's result. What am I doing wrong? Pixel just below those '0's are '1's in the original image and the structuring element has '1' at that space when the center of the window is on those '0's so it is a hit and the center must be set '1' but Matlab doesn't do this. Can anyone explain me why? Am I missing something essential here?

Upvotes: 2

Views: 301

Answers (1)

Somaye
Somaye

Reputation: 114

Suppose matrix A is your image and matrix B is structuring element. You should pad the matrix A with zeroes on all sides. So let's say you have matrix C. Then you should perform Logical AND operation of C and B. The result matrix called D will be a dilated matrix. For further information, please see here. Here is the code of Matlab which dilate image without 'imdilate' function:

clc;clearvars;close all;
%Image Dilation without using 'imdilate' function
% Matrix A is our image
A=[0 0 0 0 0 0 0;
    0 0 0 0 0 0 0;
    0 0 0 0 0 0 0 ;
    0 0 0 0 0 0 0;
    1 1 0 0 0 0 0;
    1 0 0 0 0 1 1 ;
    1 0 0 0 0 1 1 ];
%Structuring element
B=[1 0 0; 1 1 1; 1 1 1];
%Pad zeros on all the sides
C=padarray(A,[1 1]);
%Intialize a matrix of matrix size A with zeros
D=false(size(A));
for i=1:size(C,1)-2
    for j=1:size(C,2)-2
        %Perform logical AND operation
        D(i,j)=sum(sum(B&C(i:i+2,j:j+2)));
    end
end

display(D);

And this is my output, dilated matrix D:

enter image description here

Upvotes: 1

Related Questions