function b = boundary(im)
%
% boundary: produces value of 1 at segment boundary (defined as any non-zero
%           pixel which neighbors a zero valued pixel)
% On input:
%     im: 2D array
% On output:
%     b: boundary elements in im
% Call:
%     b = boundary(im)
%

[m,n] = size(im);

b = zeros(m,n);

for i1=1:m
  for j1 = 1:n
    if (im(i1,j1) > 0) & (i1==1 | i1==m | j1==1 | j1==n)
      b(i1,j1) = 1;
    end
    if (im(i1,j1) > 0) & i1~=1 & i1~=m & j1~=1 & j1~=n
      if im(i1-1,j1-1)==0
        b(i1,j1) = 1;
      end
      if im(i1-1,j1)==0
        b(i1,j1) = 1;
      end
      if im(i1-1,j1+1)==0
        b(i1,j1) = 1;
      end
      if im(i1,j1-1)==0
        b(i1,j1) = 1;
      end
      if im(i1,j1+1)==0
        b(i1,j1) = 1;
      end
      if im(i1+1,j1-1)==0
        b(i1,j1) = 1;
      end
      if im(i1+1,j1)==0
        b(i1,j1) = 1;
      end
      if im(i1+1,j1+1)==0
        b(i1,j1) = 1;
      end
    end
  end
end

       