changeset 1651:4325ee30d7e6 octave-forge

Functions used to code bwmorph
author jmones
date Mon, 16 Aug 2004 14:42:02 +0000
parents a2992fcefa7b
children ff3126024104
files main/image/devel/__bridge_lut_fun__.m main/image/devel/__conditional_mark_patterns_lut_fun__.m main/image/devel/__diagonal_fill_lut_fun__.m main/image/devel/__unconditional_mark_patterns_lut_fun__.m
diffstat 4 files changed, 447 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/image/devel/__bridge_lut_fun__.m	Mon Aug 16 14:42:02 2004 +0000
@@ -0,0 +1,77 @@
+## Copyright (C) 2004 Josep Mones i Teixidor
+##
+## 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 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.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{g} = } __bridge_lut_fun__ (@var{X})
+## Calculates hit patterns for bridge operation
+##
+## g=__bridge_lut_fun__(X) evaluates a 3-by-3 BW matrix
+## neighbourhood according to rules in Pratt's book for bridge
+## morphological operation to create a LUT using makelut.
+##
+## @var{X} contains a 3-by-3 matrix to be evaluated. Returns 1 if is a
+## "hit" and 0 otherwise.
+##
+## This function is needed by bwmorph, although it just contains the
+## result matrix as a literal inside the code.
+##
+## This function probably never be needed by itself, but it's useful to
+## know how bwmorph was coded.
+##
+## References:
+## W. K. Pratt, "Digital Image Processing"
+## @end deftypefn
+## @seealso{bwmorph}
+
+## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
+
+## Note: no intention has been made to make this quick. The only focus
+## is on being clear and similar to Pratt's specification.
+
+function g=__bridge_lut_fun__(X)
+  x=X(2,2);
+  x0=X(2,3);
+  x1=X(1,3);
+  x2=X(1,2);
+  x3=X(1,1);
+  x4=X(2,1);
+  x5=X(3,1);
+  x6=X(3,2);
+  x7=X(3,3);
+
+  l4=!x&&!x0&&x1&&!x2&&!x3&&!x4&&!x5&&!x6&&x7;
+  l3=!x&&!x0&&!x1&&!x2&&!x3&&!x4&&x5&&!x6&&x7;
+  l2=!x&&!x0&&!x1&&!x2&&x3&&!x4&&x5&&!x6&&!x7;
+  l1=!x&&!x0&&x1&&!x2&&x3&&!x4&&!x5&&!x6&&!x7;
+  pq=l1||l2||l3||l4;
+  p6=!x4&&!x6&&x5&&(x0||x1||x2);
+  p5=!x2&&!x4&&x3&&(x0||x6||x7);
+  p4=!x0&&!x2&&x1&&(x4||x5||x6);
+  p3=!x0&&!x6&&x7&&(x2||x3||x4);
+  p2=!x0&&!x4&&(x1||x2||x3)&&(x5||x6||x7)&&!pq;
+  p1=!x2&&!x6&&(x3||x4||x5)&&(x0||x1||x7)&&!pq;
+  g=x||p1||p2||p3||p4||p5||p6;
+endfunction
+
+%!assert(sum(makelut("bridge_lut_fun",3)),256+119);
+
+
+%
+% $Log$
+% Revision 1.1  2004/08/16 14:42:02  jmones
+% Functions used to code bwmorph
+%
+%
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/image/devel/__conditional_mark_patterns_lut_fun__.m	Mon Aug 16 14:42:02 2004 +0000
@@ -0,0 +1,115 @@
+## Copyright (C) 2004 Josep Mones i Teixidor
+##
+## 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 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.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{g} = } __conditional_mark_patterns_lut_fun__ (@var{X},@var{op})
+## Calculates conditional mark patterns for shrink, skel, thin and thicken
+##
+## g=__conditional_mark_patterns_lut_fun__(X, op) evaluates a 3-by-3 BW matrix
+## neighbourhood according to rules in Pratt's book as a stage for
+## shrink, skel, thin and thicken morphological operations to create a
+## LUT using makelut.
+##
+## @var{X} contains a 3-by-3 matrix to be evaluated, and @var{op} can be
+## "S", "T" or "K" as defined in Pratt's book.
+##
+## Returns 1 if is a "hit" and 0 otherwise.
+##
+## This function is needed by bwmorph, although it just contains the
+## result matrix as a literal inside the code.
+##
+## This function probably never be needed by itself, but it's useful to
+## know how bwmorph was coded.
+##
+## References:
+## W. K. Pratt, "Digital Image Processing", 3rd Edition, pp 413-414
+## @end deftypefn
+## @seealso{bwmorph}
+
+## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
+
+## Note: no intention has been made to make this quick. The only focus
+## is on being clear and similar to Pratt's specification.
+
+function m=__conditional_mark_patterns_lut_fun__(X, op)
+  x=X(2,2);
+  x0=X(2,3);
+  x1=X(1,3);
+  x2=X(1,2);
+  x3=X(1,1);
+  x4=X(2,1);
+  x5=X(3,1);
+  x6=X(3,2);
+  x7=X(3,3);
+
+  sx=sum(X(:));
+
+  m=0;
+
+  ## all cases need x==1
+  if(x==0)
+    return;
+  endif
+
+  if(any(op=='K'))
+    m|=(sx==8)&&( !x5 || !x7 || !x1 || !x3 ); ## bond 11
+  endif
+
+  if(any(op=='S'))
+    m|=(sx==2)&&(x1||x3||x5||x7); ## bond 1
+    m|=(sx==2)&&(x0||x2||x4||x6); ## bond 2
+    m|=(sx==3)&&((x0&&(x1||x7))||(x2&&(x1||x3))||(x4&&(x3||x5))||(x6&&(x5||x7))); ## bond 3
+  endif
+
+  if(any(op=='ST'))
+    m|=(sx==4)&&((x0&&x2&&x3)||(x0&&x2&&x7)||(x1&&x2&&x4)||(x0&&x1&&x6)); ## bond 5
+    m|=(sx==4)&&((x0&&x1&&x2)||(x2&&x3&&x4)||(x4&&x5&&x6)||(x6&&x7&&x0)); ## bond 5
+    m|=(sx==5)&&((x0&&x2&&x3&&x7)||(x1&&x2&&x4&&x5)); ## bond 6	
+  endif
+
+  if(any(op=='STK'))
+    m|=(sx==4)&&(all(X(:,3))||all(X(1,:))||all(X(:,1))||all(X(3,:))); ## bond 4
+    m|=(sx==5)&&( \ ## bond 6
+           (all(X(1,:))&&(x0||x4)) || \
+           (all(X(:,3))&&(x2||x6)) || \
+           (all(X(:,1))&&(x2||x6)) || \
+   	   (all(X(3,:))&&(x0||x4)) );
+    m|=(sx==6)&&( !(x4||x5||x6) || !(x6||x7||x0) || !(x0||x1||x2) || \
+		 !(x2||x3||x4) ); ## bond 7
+    m|=(sx==6)&&( !any(X(:,1)) || !any(X(3,:)) || !any(X(:,3)) || \
+		 !any(X(1,:)) ); ## bond 8
+    m|=(sx==7)&&( !(x4||(x3&&x5)) || !(x6||(x5&&x7)) || \
+		 !(x0||(x7&&x1)) || !(x2||(x1&&x3)) ); ## bond 9
+    m|=(sx==8)&&( !x0 || !x2 || !x4 || !x6 ); ## bond 10
+  endif
+
+  if(any(op=='TK'))
+    ##bond 4
+    m|=(sx==3)&&( (x0&&x2) || (x2&&x4) || (x4&&x6) || (x6&&x0) );
+  endif
+
+endfunction
+
+
+%!# We'll only check if number of hits is ok.
+%!assert(sum(makelut("__conditional_mark_patterns_lut_fun__",3,"S")), 58);
+
+%
+% $Log$
+% Revision 1.1  2004/08/16 14:42:02  jmones
+% Functions used to code bwmorph
+%
+%
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/image/devel/__diagonal_fill_lut_fun__.m	Mon Aug 16 14:42:02 2004 +0000
@@ -0,0 +1,69 @@
+## Copyright (C) 2004 Josep Mones i Teixidor
+##
+## 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 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.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{g} = } __diagonal_fill_lut_fun__ (@var{X})
+## Calculates hit patterns for diagonal fill operation
+##
+## g=__diagonal_fill_lut_fun__(X) evaluates a 3-by-3 BW matrix
+## neighbourhood according to rules in Pratt's book for diagonal fill
+## morphological operation to create a LUT using makelut.
+##
+## @var{X} contains a 3-by-3 matrix to be evaluated. Returns 1 if is a
+## "hit" and 0 otherwise.
+##
+## This function is needed by bwmorph, although it just contains the
+## result matrix as a literal inside the code.
+##
+## This function probably never be needed by itself, but it's useful to
+## know how bwmorph was coded.
+##
+## References:
+## W. K. Pratt, "Digital Image Processing"
+## @end deftypefn
+## @seealso{bwmorph}
+
+## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
+
+## Note: no intention has been made to make this quick. The only focus
+## is on being clear and similar to Pratt's specification.
+
+function g=__diagonal_fill_lut_fun__(X)
+  x=X(2,2);
+  x0=X(2,3);
+  x1=X(1,3);
+  x2=X(1,2);
+  x3=X(1,1);
+  x4=X(2,1);
+  x5=X(3,1);
+  x6=X(3,2);
+  x7=X(3,3);
+
+
+  p4=!x&&x6&&!x7&&x0;
+  p3=!x&&x4&&!x5&&x6;
+  p2=!x&&x2&&!x3&&x4;
+  p1=!x&&x0&&!x1&&x2;
+  g=x||p1||p2||p3||p4;
+endfunction
+
+
+%
+% $Log$
+% Revision 1.1  2004/08/16 14:42:02  jmones
+% Functions used to code bwmorph
+%
+%
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/image/devel/__unconditional_mark_patterns_lut_fun__.m	Mon Aug 16 14:42:02 2004 +0000
@@ -0,0 +1,186 @@
+## Copyright (C) 2004 Josep Mones i Teixidor
+##
+## 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 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.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{g} = } __unconditional_mark_patterns_lut_fun__ (@var{X},@var{op})
+## Calculates unconditional mark patterns for shrink and thin
+##
+## g=__conditional_mark_patterns_lut_fun__(X, op) evaluates a 3-by-3 BW matrix
+## neighbourhood according to rules in Pratt's book as a stage for
+## shrink and thin morphological operations to create a LUT using makelut.
+##
+## @var{X} contains a 3-by-3 matrix to be evaluated, and @var{op} can be
+## "S" (shrink), "T" (thin) or "K" (skel).
+##
+## Returns 1 if is a "hit" and 0 otherwise.
+##
+## This function is needed by bwmorph, although it just contains the
+## result matrix as a literal inside the code.
+##
+## This function probably never be needed by itself, but it's useful to
+## know how bwmorph was coded.
+##
+## References:
+## W. K. Pratt, "Digital Image Processing", 3rd Edition, pp 415,420
+## @end deftypefn
+## @seealso{bwmorph}
+
+## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
+
+function m=__unconditional_mark_patterns_lut_fun__(X, op)
+  if(nargin!=2)
+    usage("m=__unconditional_mark_patterns_lut_fun__(X, op)");
+  endif
+  if(!strcmp(op,"S") && !strcmp(op,"T") && !strcmp(op,"K"))
+    error("op can only be 'S', 'T' or 'K'.")
+  endif
+  x=X(2,2);
+  x0=X(2,3);
+  x1=X(1,3);
+  x2=X(1,2);
+  x3=X(1,1);
+  x4=X(2,1);
+  x5=X(3,1);
+  x6=X(3,2);
+  x7=X(3,3);
+
+  sx=sum(X(:));
+
+  m=0;
+
+  ## all cases need x==1
+  if(x==0)
+    return;
+  endif
+
+  if (any(op=='ST'))
+    ## spur
+    m|=all((X==[0,0,1;0,1,0;0,0,0])(:));
+    m|=all((X==[1,0,0;0,1,0;0,0,0])(:));
+  
+    ## single 4-connection
+    m|=all((X==[0,0,0;0,1,0;0,1,0])(:));
+    m|=all((X==[0,0,0;0,1,1;0,0,0])(:));
+
+    if (op=='T')
+      ## L cluster
+      m|=all((X==[0,0,1;0,1,1;0,0,0])(:));
+      m|=all((X==[0,1,1;0,1,0;0,0,0])(:));
+      m|=all((X==[1,1,0;0,1,0;0,0,0])(:));
+      m|=all((X==[1,0,0;1,1,0;0,0,0])(:));
+      m|=all((X==[0,0,0;1,1,0;1,0,0])(:));
+      m|=all((X==[0,0,0;0,1,0;1,1,0])(:));
+      m|=all((X==[0,0,0;0,1,0;0,1,1])(:));
+      m|=all((X==[0,0,0;0,1,1;0,0,1])(:));
+    endif
+
+    ## 4-connected offset
+    m|=all((X==[0,1,1;1,1,0;0,0,0])(:));
+    m|=all((X==[1,1,0;0,1,1;0,0,0])(:));
+    m|=all((X==[0,1,0;0,1,1;0,0,1])(:));
+    m|=all((X==[0,0,1;0,1,1;0,1,0])(:));
+
+    ## spur corner cluster
+    m|=all(((X&[1,0,1;1,1,0;1,1,1])==fliplr(eye(3)))(:))&&(x0||x2);
+    m|=all(((X&[1,0,1;0,1,1;1,1,1])==eye(3))(:))&&(x2||x4);
+    m|=all(((X&[1,1,1;0,1,1;1,0,1])==fliplr(eye(3)))(:))&&(x4||x6);
+    m|=all(((X&[1,1,1;1,1,0;1,0,1])==eye(3))(:))&&(x0||x6);
+
+    ## corner cluster
+    m|=all((X(1:2,1:2)==ones(2,2))(:));
+    
+    ## tee branch
+    m|=all(((X&[0,1,1;1,1,1;0,1,1])==[0,1,0;1,1,1;0,0,0])(:));
+    m|=all(((X&[1,1,0;1,1,1;1,1,0])==[0,1,0;1,1,1;0,0,0])(:));
+    m|=all(((X&[1,1,0;1,1,1;1,1,0])==[0,0,0;1,1,1;0,1,0])(:));
+    m|=all(((X&[0,1,1;1,1,1;0,1,1])==[0,0,0;1,1,1;0,1,0])(:));
+    m|=all(((X&[0,1,0;1,1,1;1,1,1])==[0,1,0;1,1,0;0,1,0])(:));
+    m|=all(((X&[1,1,1;1,1,1;0,1,0])==[0,1,0;1,1,0;0,1,0])(:));
+    m|=all(((X&[1,1,1;1,1,1;0,1,0])==[0,1,0;0,1,1;0,1,0])(:));
+    m|=all(((X&[0,1,0;1,1,1;1,1,1])==[0,1,0;0,1,1;0,1,0])(:));
+
+    ## vee branch
+    m|=all(((X(1:2,1:3)&[1,0,1;0,1,0])==[1,0,1;0,1,0])(:))&&any(X(3,:));
+    m|=all(((X(1:3,1:2)&[1,0;0,1;1,0])==[1,0;0,1;1,0])(:))&&any(X(:,3));
+    m|=all(((X(2:3,1:3)&[0,1,0;1,0,1])==[0,1,0;1,0,1])(:))&&any(X(1,:));
+    m|=all(((X(1:3,2:3)&[0,1;1,0;0,1])==[0,1;1,0;0,1])(:))&&any(X(:,1));
+
+    ## diagonal branch
+    m|=all(((X&[0,1,1;1,1,1;1,1,0])==[0,1,0;0,1,1;1,0,0])(:));
+    m|=all(((X&[1,1,0;1,1,1;0,1,1])==[0,1,0;1,1,0;0,0,1])(:));
+    m|=all(((X&[0,1,1;1,1,1;1,1,0])==[0,0,1;1,1,0;0,1,0])(:));
+    m|=all(((X&[1,1,0;1,1,1;0,1,1])==[1,0,0;0,1,1;0,1,0])(:));
+
+  elseif(any(op=='K'))
+    ## spur
+    m|=all((X==[0,0,0;0,1,0;0,0,1])(:));
+    m|=all((X==[0,0,0;0,1,0;1,0,0])(:));
+    m|=all((X==[0,0,1;0,1,0;0,0,0])(:));
+    m|=all((X==[1,0,0;0,1,0;0,0,0])(:));
+
+    ## single 4-connection
+    m|=all((X==[0,0,0;0,1,0;0,1,0])(:));
+    m|=all((X==[0,0,0;0,1,1;0,0,0])(:));
+    m|=all((X==[0,0,0;1,1,0;0,0,0])(:));
+    m|=all((X==[0,1,0;0,1,0;0,0,0])(:));
+
+    ## L corner
+    m|=all((X==[0,1,0;0,1,1;0,0,0])(:));
+    m|=all((X==[0,1,0;1,1,0;0,0,0])(:));
+    m|=all((X==[0,0,0;0,1,1;0,1,0])(:));
+    m|=all((X==[0,0,0;1,1,0;0,1,0])(:));
+
+    ## corner cluster
+    m|=all((X(1:2,2:3)==ones(2,2))(:));
+    m|=all((X(2:3,1:2)==ones(2,2))(:));
+    m|=all((X(1:2,1:2)==ones(2,2))(:));
+    m|=all((X(2:3,2:3)==ones(2,2))(:));
+
+    ## tee branch
+    m|=all(((X&[0,1,0;1,1,1;0,1,1])==[0,1,0;1,1,1;0,0,0])(:));
+    m|=all(((X&[0,1,0;1,1,0;0,1,0])==[0,1,0;1,1,0;0,1,0])(:));
+    m|=all(((X&[0,0,0;1,1,1;0,1,0])==[0,0,0;1,1,1;0,1,0])(:));
+    m|=all(((X&[0,1,0;0,1,1;0,1,0])==[0,1,0;0,1,1;0,1,0])(:));
+
+    ## vee branch (equal to ST version)
+    m|=all(((X(1:2,1:3)&[1,0,1;0,1,0])==[1,0,1;0,1,0])(:))&&any(X(3,:));
+    m|=all(((X(1:3,1:2)&[1,0;0,1;1,0])==[1,0;0,1;1,0])(:))&&any(X(:,3));
+    m|=all(((X(2:3,1:3)&[0,1,0;1,0,1])==[0,1,0;1,0,1])(:))&&any(X(1,:));
+    m|=all(((X(1:3,2:3)&[0,1;1,0;0,1])==[0,1;1,0;0,1])(:))&&any(X(:,1));
+
+    ## diagonal branch (equal to ST version)
+    m|=all(((X&[0,1,1;1,1,1;1,1,0])==[0,1,0;0,1,1;1,0,0])(:));
+    m|=all(((X&[1,1,0;1,1,1;0,1,1])==[0,1,0;1,1,0;0,0,1])(:));
+    m|=all(((X&[0,1,1;1,1,1;1,1,0])==[0,0,1;1,1,0;0,1,0])(:));
+    m|=all(((X&[1,1,0;1,1,1;0,1,1])==[1,0,0;0,1,1;0,1,0])(:));
+    
+  endif
+    
+endfunction
+
+
+## Pratt's book says there are 157 patterns from spur corner cluster to
+## the end (page 411). spur, single 4-connection and 4-connected offset
+## have no duplicate cases with other patterns. So:
+%!assert(sum(makelut("__unconditional_mark_patterns_lut_fun__",3,'S')),157+8);
+
+
+%
+% $Log$
+% Revision 1.1  2004/08/16 14:42:02  jmones
+% Functions used to code bwmorph
+%
+%
\ No newline at end of file