changeset 454:9eef309640e5

For logical images, use filter2 as in the function erode by Josep Mones i Teixidor whic is much faster
author carandraug
date Thu, 14 Apr 2011 00:45:01 +0000
parents 8ec0b45eb554
children af2072711ff1
files inst/imerode.m
diffstat 1 files changed, 33 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/inst/imerode.m
+++ b/inst/imerode.m
@@ -1,35 +1,44 @@
+## Copyright (C) 2004 Josep Mones i Teixidor  <jmones@puntbarra.com>
 ## Copyright (C) 2008 Soren Hauberg
+## Copyright (C) 2011 Carnë Draug <carandraug+dev@gmail.com>
 ## 
 ## This program is free software; you can redistribute it and/or
 ## modify it under the terms of the GNU General Public License
 ## as published by the Free Software Foundation; either version 3
 ## of the License, or (at your option) any later version.
-## 
+## 
 ## This program is distributed in the hope that it will be useful,
 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ## GNU General Public License for more details.
 
-## -*- texinfo -*-
-## @deftypefn {Function File} @var{B} = imerode (@var{A}, @var{se})
-## Perform morphological erosion on a given image.
-## The image @var{A} must be a grayscale or binary image, and @var{se} must be a
-## structuring element.
-## @seealso{imdilate, imopen, imclose}
-## @end deftypefn
-
-function retval = imerode(im, se)
-  ## Checkinput
-  if (nargin != 2)
-    print_usage();
-  endif
-  if (!ismatrix(im) || !isreal(im))
-    error("imerode: first input argument must be a real matrix");
-  endif
-  if (!ismatrix(se) ||  !isreal(se))
-    error("imerode: second input argument must be a real matrix");
-  endif
-
-  ## Perform filtering
-  retval = ordfiltn(im, 1, se, 0);
-endfunction
+## -*- texinfo -*-
+## @deftypefn {Function File} @var{B} = imerode (@var{A}, @var{se})
+## Perform morphological erosion on a given image.
+## The image @var{A} must be a grayscale or binary image, and @var{se} must be a
+## structuring element.
+## @seealso{imdilate, imopen, imclose}
+## @end deftypefn
+
+function retval = imerode(im, se)
+  ## Checkinput
+  if (nargin != 2)
+    print_usage();
+  endif
+  if (!ismatrix(im) || !isreal(im))
+    error("imerode: first input argument must be a real matrix");
+  endif
+  if (!ismatrix(se) ||  !isreal(se))
+    error("imerode: second input argument must be a real matrix");
+  endif
+
+  ## Perform filtering
+  ## If image is binary/logical, try to use filter2 (much faster)
+  if (islogical(im))
+    thr     = sum(se(:));
+    retval  = filter2(se,im) == thr;
+  else
+    retval  = ordfiltn(im, 1, se, 0);
+  endif
+
+endfunction