]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/erode.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / erode.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{BW2} = } erode (@var{BW1},@var{SE})
18 ## @deftypefnx {Function File} {@var{BW2} = } erode (@var{BW1},@var{SE},@var{alg})
19 ## @deftypefnx {Function File} {@var{BW2} = } erode (@var{BW1},@var{SE},...,@var{n})
20 ## Perform an erosion morphological operation on a binary image.
21 ##
22 ## BW2 = erosion(BW1, SE) returns a binary image with the result of an erosion
23 ## operation on @var{BW1} using neighbour mask @var{SE}.
24 ##
25 ## For each point in @var{BW1}, erode searchs its neighbours (which are
26 ## defined by setting to 1 their in @var{SE}). If all neighbours
27 ## are on (1), then pixel is set to 1. If any is off (0) then it is set to 0.
28 ##
29 ## Center of @var{SE} is calculated using floor((size(@var{SE})+1)/2).
30 ##
31 ## Pixels outside the image are considered to be 0.
32 ##
33 ## BW2 = erode(BW1, SE, alg) returns the result of a erosion operation 
34 ## using algorithm @var{alg}. Only 'spatial' is implemented at the moment.
35 ##
36 ## BW2 = erosion(BW1, SE, ..., n) returns the result of @var{n} erosion
37 ## operations on @var{BW1}.
38 ##
39 ## @seealso{dilate}
40 ## @end deftypefn
41
42 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
43
44 function BW2 = erode(BW1, SE, a, b)
45   alg='spatial';
46   n=1;
47   if (nargin < 1 || nargin > 4)
48     usage ("BW2 = erode(BW1, SE [, alg] [, n])");
49   endif
50   if nargin ==  4
51     alg=a;
52     n=b;
53   elseif nargin == 3
54     if ischar(a)
55       alg=a;
56     else
57       n=a;
58     endif
59   endif
60
61   if !strcmp(alg, 'spatial')
62     error("erode: alg not implemented.");
63   endif
64
65   # count ones in mask
66   thr=sum(SE(:));
67
68   # "Binarize" BW1, just in case image is not [1,0]
69   BW1=BW1!=0;
70
71   for i=1:n
72     # create result matrix
73     BW1=filter2(SE,BW1) == thr;
74   endfor
75
76   BW2=BW1;
77 endfunction
78
79 %!demo
80 %! erode(ones(5,5),ones(3,3))
81 %! % creates a zeros border around ones.
82
83
84
85 %!assert(erode([0,1,0;1,1,1;0,1,0],[0,0,0;0,0,1;0,1,1])==[1,0,0;0,0,0;0,0,0]);
86 %!assert(erode([0,1,0;1,1,1;0,1,0],[0,1;1,1])==[1,0,0;0,0,0;0,0,0]);
87
88