1 ## Copyright (C) 2005 Carvalho-Mariel
2 ## Copyright (C) 2010 Carnë Draug <carandraug+dev@gmail.com>
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.
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.
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.
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.
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
29 ## @seealso{imerode, imdilate, imopen, imclose, mmgradm}
32 function retval = imtophat(im, se, trans)
35 if (nargin <=1 || nargin > 3)
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");
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") )
54 retval = im & !imopen(im,se);
56 retval = im - imopen(im, se);
58 elseif ( strcmpi(trans, "black") || strcmpi(trans, "close") )
60 retval = imclose(im, se) & !im;
62 retval = imclose(im, se) - im;
65 error ("Unexpected type of top-hat transform");
71 %! I = [1 1 1; 1 1 1; 1 1 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);