% Dilation and erosion fig8_1
clear
BW=[1 1 1 0 0 1] % input
BWE=[0 BW 0]
SE = strel([1 1 0]) % structuring element
S =[1 1 0]
for k=2:1+length(BW)
    BW2(k-1)=S(3)&BWE(k-1)|S(2)&BWE(k)|S(1)&BWE(k+1);
end
BW2
BW2 = imdilate(BW,SE)
BWE=[1 BW 1]
for k=2:1+length(BW)
    BW3(k-1)=(S(1)&BWE(k-1))&(S(2)&BWE(k));
end
BW3
BW3 = imerode(BW,SE)