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{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.
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}.
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.
29 ## Center of @var{SE} is calculated using floor((size(@var{SE})+1)/2).
31 ## Pixels outside the image are considered to be 0.
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.
36 ## BW2 = dilate(BW1, SE, ..., n) returns the result of @var{n} dilation
37 ## operations on @var{BW1}.
42 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com>
44 function BW2 = dilate(BW1, SE, a, b)
47 if (nargin < 1 || nargin > 4)
48 usage ("BW2 = dilate(BW1, SE [, alg] [, n])");
61 if !strcmp(alg, 'spatial')
62 error("dilate: alg not implemented.");
65 # "Binarize" BW1, just in case image is not [1,0]
68 ## Filtering must be done with the reflection of the structuring element (they
69 ## are not always symmetrical)
70 SE = imrotate(SE, 180);
73 # create result matrix
74 BW1=filter2(SE,BW1)>0;
81 %! dilate(eye(5),ones(2,2))
82 %! % returns a thick diagonal.
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