]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/bweuler.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / bweuler.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor
2 ##
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.
7 ##
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.
12 ##
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/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{eul} = } bweuler (@var{BW},@var{n})
18 ## Calculates the Euler number of a binary image.
19 ##
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.
23 ##
24 ## @var{n} can have the values:
25 ## @table @code
26 ## @item 4
27 ## bweuler will use 4-connected neighbourhood definition.
28 ## @item 8
29 ## bweuler will use 8-connected neighbourhood definition. This is the
30 ## default value.
31 ## @end table
32 ##
33 ## This function uses Bit Quads as described in "Digital Image
34 ## Processing" to calculate euler number.
35 ##
36 ## References:
37 ## W. K. Pratt, "Digital Image Processing", 3rd Edition, pp 593-595
38 ##
39 ## @seealso{qtgetblk}
40 ## @end deftypefn
41
42 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
43
44 function eul = bweuler(BW, n)
45   if(nargin<1 || nargin>2)
46     usage("eul=bweuler(BW,n)");
47   endif
48   if(nargin<2)
49     n=8;
50   endif
51
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...
58   if(n==8)
59     lut=[0;.25;.25;0;.25;0;-.5;-.25;.25;-.5;0;-.25;0;-.25;-.25;0];
60   elseif(n==4)
61     lut=[0;.25;.25;0;.25;0;.5;-.25;.25;.5;0;-.25;0;-.25;-.25;0];
62   else
63     error("bweuler: n can only be 4 or 8.");
64   endif
65   
66   eul=sum(applylut(BW,lut)(:));
67 endfunction
68
69 %!demo
70 %! A=zeros(9,10);
71 %! A([2,5,8],2:9)=1;
72 %! A(2:8,[2,9])=1
73 %! bweuler(A)
74 %! # Euler number (objects minus holes) is 1-2=-1 in an 8-like object
75
76 %!test
77 %! A=zeros(10,10);
78 %! A(2:9,3:8)=1;
79 %! A(4,4)=0;
80 %! A(8,8)=0; # not a hole
81 %! A(6,6)=0;
82 %! assert(bweuler(A),-1);
83
84 %!# This will test if n=4 and n=8 behave differently
85 %!test
86 %! A=zeros(10,10);
87 %! A(2:4,2:4)=1;
88 %! A(5:8,5:8)=1;
89 %! assert(bweuler(A,4),2);
90 %! assert(bweuler(A,8),1);
91 %! assert(bweuler(A),1);
92
93 % $Log$
94 % Revision 1.3  2007/03/23 16:14:36  adb014
95 % Update the FSF address
96 %
97 % Revision 1.2  2007/01/04 23:41:47  hauberg
98 % Minor changes in help text
99 %
100 % Revision 1.1  2006/08/20 12:59:31  hauberg
101 % Changed the structure to match the package system
102 %
103 % Revision 1.2  2005/07/03 01:10:19  pkienzle
104 % Try to correct for missing newline at the end of the file
105 %
106 % Revision 1.1  2004/08/15 19:33:20  jmones
107 % bweuler: Calculates the Euler number of a binary image