]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/imerode.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / imerode.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor  <jmones@puntbarra.com>
2 ## Copyright (C) 2008 Soren Hauberg
3 ## Copyright (C) 2011 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} = imerode (@var{A}, @var{se})
17 ## Perform morphological erosion 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{imdilate, imopen, imclose}
24 ## @end deftypefn
25
26 function retval = imerode(im, se)
27   ## Checkinput
28   if (nargin != 2)
29     print_usage();
30   endif
31   if (!ismatrix(im) || !isreal(im))
32     error("imerode: first input argument must be a real matrix");
33   elseif (!ismatrix(se) || !isreal(se))
34     error("imerode: second input argument must be a real matrix");
35   elseif ( !strcmp(class(im), class(se)) )
36     error("imerode: image and structuring element must have the same class");
37   endif
38
39   ## Perform filtering
40   ## If image is binary/logical, try to use filter2 (much faster)
41   if (islogical(im))
42     thr     = sum(se(:));
43     retval  = filter2(se,im) == thr;
44   else
45     retval  = ordfiltn(im, 1, se, 0);
46   endif
47
48 endfunction