Why do I get an error for my erosion code of an image?

1 view (last 30 days)
My code is having an error, but I do not know where it is coming from. Thanks in advance!
function [img_out] = myErosion(img_in, N)
if img_in >=(2*N+1)^2
for k=0:N
for i=1:img_in(1)
for j=1:img_in(2)
if img_in(i+k,j)== 0
if img_in(i-k,j)== 0
if img_in(i,j+k)== 0
if img_in(i,j-k)== 0
if img_in(i+k,j+k)== 0
if img_in(i+k,j-k)== 0
if img_in(i-k,j+k)== 0
if img_in(i-k,j-k)== 0
img_out(i,j) = 0;
end
end
end
end
end
end
end
end
end
end
end
end
end

Accepted Answer

Image Analyst
Image Analyst on 14 Mar 2014
Again, you're just not thinking this through. Erosion is a local min. So do that instead of whatever you're trying to do. Try this: (untested)
[rows, columns] = size(img_in);
% N is an odd number that is the window size
n2 = floor(N/2);
img_out = zeros(size(img_in));
for c = n2 : columns - n2
for r = n2 : rows - n2
subImage = img_in(r-n2:r+n2, c-n2 : c+n2);
img_out(r, c) = min(subImage(:));
end
end
  4 Comments
Christopher
Christopher on 15 Mar 2014
Thanks I got it to work but it's only eroding only one time through so there are still black specks on my image. How do I fix that?
function [img_out] = myErosion(img_in, N)
[rows, columns] = size(img_in);
% N is an odd number that is the window size
n2 = floor(N/2);
img_out = zeros(size(img_in));
for c = n2 : (columns - n2)
for r = n2 : (rows - n2)
subImage = img_in(((r+1)-(n2)):((r-1)+(n2)), ((c+1)-(n2)) : ((c-1)+(n2)));
img_out(r, c) = min(subImage(:));
end
end
end
Image Analyst
Image Analyst on 15 Mar 2014
You can use a larger window size. Try 9 or 19 or 51 or whatever it takes to get rid of noise.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!