does imdilate and imerode "flip" neighborhoods?
Show older comments
hello i'm trying to use the above funcs to make a "stick-man" move right.
clear all
N=20;X=zeros(N);
stick=[ 0 1 1 1 0; 0 1 1 1 0; 0 0 1 0 0; 0 0 1 0 0; 1 1 1 1 1;
0 0 1 0 0; 0 0 1 0 0; 0 1 1 1 0; 1 0 0 0 1; ];
[n m]=size(stick);
X(N-n+1:N,2:m+1)=stick;
figure(1)
subplot(1,2,1);imshow(X);
title('The BEGINNING')
H1=[0 1 1 ];H2=[1 1 0 ];
for i=1:N-m-1
X=imdilate(X,H1);
X=imerode(X,H2);
figure(2)
imshow(X,'InitialMagnification','fit')
title('MOVIE');drawnow
clear ts;ts=tic;
while toc(ts)<0.5;end
end
figure(1)
subplot(1,2,2);imshow(X);
title('The END')
my code works. however i feel like the neighborhoods should be opposite. hence, my question is: does matlab "filp" the neighborhood like in regular 2-d filtering or convolution operators? thanks in advance
Amit
Answers (1)
David Young
on 14 Jan 2012
It's sometimes easiest just to look at a very simple example to see what happens. Here's one:
>> img = [0 0 1 0 0];
>> se = [0 0 1];
>> imdilate(logical(img), se)
ans =
0 0 0 1 0
To answer your question: it depends on how you describe the operation. Here are two ways to describe binary dilation (erosion is similar). Start with a binary image, a binary structuring element (SE), and an output array that is initially all zeros.
- Flip the SE. Place it somewhere on the image. If a 1 in the flipped SE coincides with a 1 in the image, write a 1 in the output at the position of the SE's centre. Repeat for every possible position of the SE.
- Do not flip the SE. Place it somewhere on the image. If there is a 1 in the image at the position of the SE's centre, write a 1 in the output at the position of every 1 in the SE. Repeat for every possible position of the SE.
You should be able to see that each of these descriptions is consistent with the simple example. If you run a few more examples, changing the pattern in the SE slightly, you should be able to convince yourself that the descriptions are equivalent to each other.
You may also be able to see that the first description is just an informal version of the mathematical definition of binary dilation given in doc imdilate.
Categories
Find more on Morphological Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!