Why is my dilation not working when N is greater than 1?

12 views (last 30 days)
I have been working on the dilation problem, but I cannot see why the dilation only works for when N= 1.
function [img_out] = myDilation(img_in, N)
M=size(img_in);
for k= 0:N
for i = 1:M(1)
for j = 1:M(2)
if (i+k)<= M(1)
if img_in(i+k,j,1:3)==0
img_out(i,j,1:3)=0;
end
end
if (i-k)>0
if img_in(i-k,j,1:3)==0
img_out(i,j,1:3)=0;
end
end
if (j+k)<= M(2)
if img_in(i,j+k,1:3)==0
img_out(i,j,1:3)= 0;
end
end
if (j-k)>0
if img_in(i,j-k,1:3)==0
img_out(i,j,1:3)= 0;
end
end
if (i+k)<= M(1) && (j+k)<= M(2)
if img_in(i+k,j+k,1:3)==0
img_out(i,j,1:3)= 0;
end
end
if (i+k)<= M(1) && (j-k)>0
if img_in(i+k,j-k,1:3)==0
img_out(i,j,1:3)= 0;
end
end
if (i-k)>0 && (j+k)<= M(2)
if img_in(i-k,j+k,1:3)==0
img_out(i,j,1:3)= 0;
end
end
if (i-k)>0 && (j-k)>0
if img_in(i-k,j-k,1:3)==0
img_out(i,j,1:3)= 0;
end
end
if img_in(i,j,1:3)==1
img_out(i,j,1:3) = 1;
end
end
end
end
end

Answers (1)

Image Analyst
Image Analyst on 16 Mar 2014
Christopher, did you not learn anything when I rewrote your erosion code for you? This dilation code is the same mess that you had for erosion. A dilation is a local max, so program it up that way. Just abandon that whole approach above, which is wrong, and do the much shorter, simpler, and correct way I showed you in the erosion example.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!