changeset 231:04e025afb28d

New function: bwperim
author hauberg
date Sat, 13 Jan 2007 17:03:29 +0000
parents 994cc6221f5f
children 25e4c5428492
files inst/bwperim.m
diffstat 1 files changed, 68 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/inst/bwperim.m
@@ -0,0 +1,68 @@
+## Copyright (C) 2006  Søren Hauberg
+## 
+## 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 2, 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. 
+## 
+## You should have received a copy of the GNU General Public License
+## along with this file.  If not, write to the Free Software Foundation,
+## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} @var{BW2} = bwperim(@var{BW1})
+## @deftypefnx{Function File} @var{BW2} = bwperim(@var{BW1}, @var{n})
+## Find the perimeter of objects in binary images.
+##
+## A pixel is part of an object perimeter if its value is one and there
+## is at least one zero-valued pixel in its neighborhood.
+##
+## By default the neighborhood of a pixel is 4 nearest pixels, but
+## if @var{n} is set to 8 the 8 nearest pixels will be considered.
+## @end deftypefn
+
+function out = bwperim(bw, n=4)
+  ## Input checking
+  if (nargin < 1)
+    print_usage();
+  endif
+  if (!isbw(bw) || ndims(bw)!=2)
+    error("bwperim: first input argument must be a 2-dimensional binary image");
+  endif
+  if (!isscalar(n) || (n!=4 && n!=8))
+    error("bwperim: second argument must be 4 or 8");
+  endif
+  
+  ## Translate image by one pixel in all directions
+  [rows, cols] = size(bw);
+  north = [bw(2:end,:); zeros(1,cols)];
+  south = [zeros(1,cols); bw(1:end-1,:)];
+  west  = [bw(:,2:end), zeros(rows,1)];
+  east  = [zeros(rows,1), bw(:,1:end-1)];
+  if (n == 8)
+    north_east = north_west = south_east = south_west = zeros(rows, cols, 'logical'); 
+    north_east(1:end-1, 2:end)   = bw(2:end, 1:end-1);
+    north_west(1:end-1, 1:end-1) = bw(2:end, 2:end);
+    south_east(2:end, 2:end)     = bw(1:end-1, 1:end-1);
+    south_west(2:end, 1:end-1)   = bw(1:end-1, 2:end);
+  endif
+  
+  ## Do the comparing
+  if (n == 4)
+    out = bw;
+    idx = (north == bw) & (south == bw) & (west == bw) & (east == bw);
+    out(idx) = 0;
+  else # n == 8
+    out = bw;
+    idx = (north == bw) & (north_east == bw) & ...
+          (east  == bw) & (south_east == bw) & ...
+          (south == bw) & (south_west == bw) & ...
+          (west  == bw) & (north_west == bw);
+    out(idx) = 0;
+  endif
+endfunction