function e = bweuler(a,n)
%BWEULER Euler number.
%	BWEULER(BW,N) returns the Euler number for the binary image BW
%	assuming an N-connected neighborhood.  N can be 4 or 8.  The
%	Euler number is the number of objects minus the number of
%	holes.
%
%	BWEULER(BW) uses the default 8-connected neighborhood.
%
%	See also: DILATE, ERODE, BWPERIM, BWMORPH.

%	Clay M. Thompson 12-2-92
%	Copyright (c) 1992 by The MathWorks, Inc.
%	$Revision: 1.7 $  $Date: 1993/08/10 18:59:37 $

% Reference: William Pratt, Digital Image Processing, John Wiley and Sons, 1991,
% pp. 630-634.

error(nargchk(1,2,nargin));

if nargin<2, n = 8; end

if n~=8 & n~=4, error('N must be either 4 or 8.'); end

if n==4,
  lut = [0 0.25 0.25 0 0.25 0  .5 -0.25 0.25  0.5  0 -0.25 0 -0.25 -0.25 0];
else
  lut = [0 0.25 0.25 0 0.25 0 -.5 -0.25 0.25 -0.5  0 -0.25 0 -0.25 -0.25 0];
end
h = [1 4;2 8];

e = sum(sum(lut(filter2(h,a,'full')+1)));
