clear all;
close all;
warning off;


B = [ 9 9 9 8 9 8 9 8 8 8 8 9 9 9 9 
      8 8 9 8 8 8 8 8 7 8 8 8 9 8 9 
      7 7 7 6 6 7 7 7 7 6 6 6 7 8 8 
      6 7 6 6 2 1 1 1 1 1 1 1 7 8 8 
      7 7 6 4 3 1 0 0 0 1 2 2 6 7 8 
      7 6 5 4 2 0 0 0 0 1 2 5 6 7 8 
      7 7 6 6 7 6 0 0 1 6 7 7 7 8 7 
      7 7 7 7 6 6 0 0 1 6 7 7 8 7 8 
      7 5 8 7 6 6 0 1 1 6 7 7 7 7 8 
      7 4 7 8 7 6 1 1 2 6 7 7 7 8 7 
      6 6 6 7 8 7 2 2 3 6 7 6 7 7 7 
      6 6 4 6 7 6 2 3 5 7 6 6 6 7 7 
      6 6 3 4 6 6 6 7 7 6 6 6 6 7 7 
      4 4 6 4 4 6 6 6 7 7 6 7 7 5 8 
      2 3 6 6 6 4 6 7 6 7 6 6 6 7 4];
  
[l c] = size(B);
A = reshape(B, 1, l*c);
h = hist(A,16);

C = zeros(size(B));
for y = 1:l
    for x = 1:c
        if ( B(y,x) <= 5 )
            C(y,x) = 1;
        else
            C(y,x) = 0;
        end
    end
end


se4 = [0 1 0
      1 1 1 
      0 1 0];

I2 = erode(C,se4);


se8 = [1 1 1
      1 1 1 
      1 1 1];

I3 = erode(C,se8);


I4 = dilate(I2, se4);


I5 = dilate(I3, se8);


figure, imshow(~I5), title('Dilatacao I3 B8')
figure, imshow(~I4), title('Dilatacao I2 B4')
figure, imshow(~I3), title('Erosao I1 B8')
figure, imshow(~I2), title('Erosao I1 B4')
figure,imshow(~C), title('Binaria S=5')
figure,hist(A,16); grid; title('Histograma')
figure,imshow(B,[0 9]), title('Imagem Original')

