1 ## Copyright (C) 2004 Josep Mones i Teixidor
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{eul} = } bweuler (@var{BW},@var{n})
18 ## Calculates the Euler number of a binary image.
20 ## @var{eul}=bweuler(@var{BW}, @var{n}) calculates the Euler number @var{eul} of a binary
21 ## image @var{BW}, which is a scalar whose value is the total number of
22 ## objects in an image minus the number of holes.
24 ## @var{n} can have the values:
27 ## bweuler will use 4-connected neighbourhood definition.
29 ## bweuler will use 8-connected neighbourhood definition. This is the
33 ## This function uses Bit Quads as described in "Digital Image
34 ## Processing" to calculate euler number.
37 ## W. K. Pratt, "Digital Image Processing", 3rd Edition, pp 593-595
42 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com>
44 function eul = bweuler(BW, n)
45 if(nargin<1 || nargin>2)
46 usage("eul=bweuler(BW,n)");
52 ## q1lut=makelut(inline("sum(x(:))==1","x"),2);
53 ## q3lut=makelut(inline("sum(x(:))==3","x"),2);
54 ## qdlut=makelut(inline("all((x==eye(2))(:))||all((x==fliplr(eye(2)))(:))","x"),2);
55 ## lut_4=(q1lut-q3lut+2*qdlut)/4; # everything in one lut will be quicker
56 ## lut_8=(q1lut-q3lut-2*qdlut)/4;
57 ## we precalculate this...
59 lut=[0;.25;.25;0;.25;0;-.5;-.25;.25;-.5;0;-.25;0;-.25;-.25;0];
61 lut=[0;.25;.25;0;.25;0;.5;-.25;.25;.5;0;-.25;0;-.25;-.25;0];
63 error("bweuler: n can only be 4 or 8.");
66 eul=sum(applylut(BW,lut)(:));
74 %! # Euler number (objects minus holes) is 1-2=-1 in an 8-like object
80 %! A(8,8)=0; # not a hole
82 %! assert(bweuler(A),-1);
84 %!# This will test if n=4 and n=8 behave differently
89 %! assert(bweuler(A,4),2);
90 %! assert(bweuler(A,8),1);
91 %! assert(bweuler(A),1);
94 % Revision 1.3 2007/03/23 16:14:36 adb014
95 % Update the FSF address
97 % Revision 1.2 2007/01/04 23:41:47 hauberg
98 % Minor changes in help text
100 % Revision 1.1 2006/08/20 12:59:31 hauberg
101 % Changed the structure to match the package system
103 % Revision 1.2 2005/07/03 01:10:19 pkienzle
104 % Try to correct for missing newline at the end of the file
106 % Revision 1.1 2004/08/15 19:33:20 jmones
107 % bweuler: Calculates the Euler number of a binary image