]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/dilate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / dilate.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} = } dilate (@var{BW1},@var{SE})
18 ## @deftypefnx {Function File} {@var{BW2} = } dilate (@var{BW1},@var{SE},@var{alg})
19 ## @deftypefnx {Function File} {@var{BW2} = } dilate (@var{BW1},@var{SE},...,@var{n})
20 ## Perform a dilation morphological operation on a binary image.
21 ##
22 ## BW2 = dilate(BW1, SE) returns a binary image with the result of a dilation
23 ## operation on @var{BW1} using neighbour mask @var{SE}.
24 ##
25 ## For each point in @var{BW1}, dilate search its neighbours (which are
26 ## defined by setting to 1 their in @var{SE}). If any of its neighbours
27 ## is on (1), then pixel is set to 1. If all are 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 = dilate(BW1, SE, alg) returns the result of a dilation operation 
34 ## using algorithm @var{alg}. Only 'spatial' is implemented at the moment.
35 ##
36 ## BW2 = dilate(BW1, SE, ..., n) returns the result of @var{n} dilation
37 ## operations on @var{BW1}.
38 ##
39 ## @seealso{erode}
40 ## @end deftypefn
41
42 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
43
44 function BW2 = dilate(BW1, SE, a, b)
45   alg='spatial';
46   n=1;
47   if (nargin < 1 || nargin > 4)
48     usage ("BW2 = dilate(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("dilate: alg not implemented.");
63   endif
64
65   # "Binarize" BW1, just in case image is not [1,0]
66   BW1=BW1!=0;
67
68   ## Filtering must be done with the reflection of the structuring element (they
69   ## are not always symmetrical)
70   SE = imrotate(SE, 180);
71
72   for i=1:n
73     # create result matrix
74     BW1=filter2(SE,BW1)>0;
75   endfor
76
77   BW2=BW1;
78 endfunction
79
80 %!demo
81 %! dilate(eye(5),ones(2,2))
82 %! % returns a thick diagonal.
83
84
85
86 %!assert(dilate(eye(3),[1])==eye(3));                     # using [1] as a mask returns the same value
87 %!assert(dilate(eye(3),[1,0,0])==[0,0,0;1,0,0;0,1,0]);    # check if it works with non-symmetric SE
88 ## TODO the next assertion is commented since we do not know how the right answer for the calculation
89 %!##assert(dilate(eye(3),[1,0,0,0])==XXX); # test if center is correctly calculated on even masks
90