]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/bwhitmiss.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / bwhitmiss.m
1 ## Copyright (C) 2008 Soren Hauberg
2 ## 
3 ## This program is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU General Public License
5 ## as published by the Free Software Foundation; either version 2
6 ## of the License, or (at your option) any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12
13 ## -*- texinfo -*-
14 ## @deftypefn {Function File} @var{bw2} = bwhitmiss (@var{bw1}, @var{se1}, @var{se1})
15 ## @deftypefnx{Function File} @var{bw2} = bwhitmiss (@var{bw1}, @var{interval})
16 ## Perform the binary hit-miss operation.
17 ##
18 ## If two structuring elements @var{se1} and @var{se1} are given, the hit-miss
19 ## operation is defined as
20 ## @example
21 ## bw2 = erode(bw1, se1) & erode(!bw1, se2);
22 ## @end example
23 ## If instead an 'interval' array is given, two structuring elements are computed
24 ## as
25 ## @example
26 ## se1 = (interval ==  1)
27 ## se2 = (interval == -1)
28 ## @end example
29 ## and then the operation is defined as previously.
30 ## @seealso{bwmorph}
31 ## @end deftypefn
32
33 function bw = bwhitmiss(im, varargin)
34   ## Checkinput
35   if (nargin != 2 && nargin != 3)
36     print_usage();
37   endif
38   if (!ismatrix(im) || !isreal(im))
39     error("bwhitmiss: first input argument must be a real matrix");
40   endif
41
42   ## Get structuring elements
43   if (nargin == 2) # bwhitmiss (im, interval)
44     interval = varargin{1};
45     if (!isreal(interval))
46       error("bwhitmiss: second input argument must be a real matrix");
47     endif
48     if (!all( (interval(:) == 1) | (interval(:) == 0) | (interval(:) == -1) ))
49       error("bwhitmiss: second input argument can only contain the values -1, 0, and 1");
50     endif
51     se1 = (interval ==  1);
52     se2 = (interval == -1);
53   else # bwhitmiss (im, se1, se2)
54     se1 = varargin{1};
55     se2 = varargin{2};
56     if (!all((se1(:) == 1) | (se1(:) == 0)) || !all((se2(:) == 1) | (se2(:) == 0)))
57       error("bwhitmiss: structuring elements can only contain zeros and ones.");
58     endif
59   endif
60   
61   ## Perform filtering
62   bw = erode(im, se1) & erode(!im, se2);
63
64 endfunction