]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/imtophat.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / imtophat.m
1 ## Copyright (C) 2005  Carvalho-Mariel
2 ## Copyright (C) 2010  CarnĂ« Draug <carandraug+dev@gmail.com>
3 ## 
4 ## This program is free software; you can redistribute it and/or
5 ## modify it under the terms of the GNU General Public License
6 ## as published by the Free Software Foundation; either version 2
7 ## of the License, or (at your option) any later version.
8 ## 
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ## GNU General Public License for more details.
13
14 ## -*- texinfo -*-
15 ## @deftypefn {Function File} @var{B} = imtophat (@var{A}, @var{se})
16 ## @deftypefnx{Function File} @var{B} = imtophat (@var{A}, @var{se}, @var{type})
17 ## Perform morphological top hat filtering.
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 ## @var{type} defines the type of top hat transform. To perform a white, or
24 ## opening, top-hat transform its value must be @code{open} or @code{white}. To
25 ## perform a black, or closing, top-hat transform its value must be @code{close}
26 ## or @code{black}. If @var{type} is not specified, it performs a white top-hat
27 ## transform.
28 ##
29 ## @seealso{imerode, imdilate, imopen, imclose, mmgradm}
30 ## @end deftypefn
31
32 function retval = imtophat(im, se, trans)
33
34   ## Checkinput
35   if (nargin <=1 || nargin > 3)
36     print_usage();
37   elseif (nargin == 2)
38     trans = "white";
39   endif
40   if (!ismatrix(im) || !isreal(im))
41     error("imtophat: first input argument must be a real matrix");
42   elseif (!ismatrix(se) || !isreal(se))
43     error("imtophat: second input argument must be a real matrix");
44   elseif ( !strcmp(class(im), class(se)) )
45     error("imtophat: image and structuring element must have the same class");
46   endif
47
48   ## Perform filtering
49   ## Note that in case that the transform is to applied to a logical image,
50   ## subtraction must be handled in a different way (x & !y) instead of (x - y)
51   ## or it will return a double precision matrix
52   if ( strcmpi(trans, "white") || strcmpi(trans, "open") )
53     if (islogical(im))
54       retval = im & !imopen(im,se);
55     else
56       retval = im - imopen(im, se);
57     endif
58   elseif ( strcmpi(trans, "black") || strcmpi(trans, "close") )
59     if (islogical(im))
60       retval = imclose(im, se) & !im;
61     else
62       retval = imclose(im, se) - im;
63     endif
64   else
65     error ("Unexpected type of top-hat transform");
66   endif
67
68 endfunction
69
70 %!test
71 %! I = [1 1 1; 1 1 1; 1 1 1;];
72 %! se = [1 1; 0 1;];
73 %! ## class of input should be the same as the output
74 %! result = imtophat(logical(I), logical(se));
75 %! expected = 0.5 < [0 0 1; 0 0 1; 1 1 1];
76 %! assert(expected, result);
77 %! result = imtophat((I), (se));
78 %! expected = [0 0 1; 0 0 1; 1 1 1];
79 %! assert(expected, result);
80
81