]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/imdilate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / imdilate.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor  <jmones@puntbarra.com>
2 ## Copyright (C) 2008 Soren Hauberg
3 ## Copyright (C) 2010 CarnĂ« Draug <carandraug+dev@gmail.com>
4 ## 
5 ## This program is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU General Public License
7 ## as published by the Free Software Foundation; either version 3
8 ## of the License, or (at your option) any later version.
9 ## 
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14
15 ## -*- texinfo -*-
16 ## @deftypefn {Function File} @var{B} = imdilate (@var{A}, @var{se})
17 ## Perform morphological dilation on a given image.
18 ##
19 ## The image @var{A} must be a grayscale or binary image, and @var{se} must be a
20 ## structuring element. Both must have the same class, e.g., if @var{A} is a
21 ## logical matrix, @var{se} must also be logical.
22 ##
23 ## @seealso{imerode, imopen, imclose}
24 ## @end deftypefn
25
26 function retval = imdilate(im, se)
27   ## Checkinput
28   if (nargin != 2)
29     print_usage();
30   endif
31   if (!ismatrix(im) || !isreal(im))
32     error("imdilate: first input argument must be a real matrix");
33   elseif (!ismatrix(se) || !isreal(se))
34     error("imdilate: second input argument must be a real matrix");
35   elseif ( !strcmp(class(im), class(se)) )
36     error("imdilate: image and structuring element must have the same class");
37   endif
38
39   ## Perform filtering
40   ## Filtering must be done with the reflection of the structuring element (they
41   ## are not always symmetrical)
42   se      = imrotate(se, 180);
43
44   ## If image is binary/logical, try to use filter2 (much faster)
45   if (islogical(im))
46     retval  = filter2(se,im)>0;
47   else
48     retval  = ordfiltn(im, sum(se(:)!=0), se, 0);
49   endif
50
51 endfunction