1 ## Copyright (C) 2008 Søren Hauberg
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or
6 ## (at your option) any later version.
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.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ## @deftypefn {Function File} {@var{E} =} entropyfilt (@var{im})
19 ## @deftypefnx{Function File} {@var{E} =} entropyfilt (@var{im}, @var{domain})
20 ## @deftypefnx{Function File} {@var{E} =} entropyfilt (@var{im}, @var{domain}, @var{padding}, ...)
21 ## Computes the local entropy in a neighbourhood around each pixel in an image.
23 ## The entropy of the elements of the neighbourhood is computed as
26 ## @var{E} = -sum (@var{P} .* log2 (@var{P})
29 ## where @var{P} is the distribution of the elements of @var{im}. The distribution
30 ## is approximated using a histogram with @var{nbins} cells. If @var{im} is
31 ## @code{logical} then two cells are used. For other classes 256 cells
34 ## When the entropy is computed, zero-valued cells of the histogram are ignored.
36 ## The neighbourhood is defined by the @var{domain} binary mask. Elements of the
37 ## mask with a non-zero value are considered part of the neighbourhood. By default
38 ## a 9 by 9 matrix containing only non-zero values is used.
40 ## At the border of the image, extrapolation is used. By default symmetric
41 ## extrapolation is used, but any method supported by the @code{padarray} function
42 ## can be used. Since extrapolation is used, one can expect a lower entropy near
45 ## @seealso{entropy, paddarray, stdfilt}
48 function retval = entropyfilt (I, domain = true (9), padding = "symmetric", varargin)
51 error ("entropyfilt: not enough input arguments");
55 error ("entropyfilt: first input must be a matrix");
58 if (!ismatrix (domain))
59 error ("entropyfilt: second input argument must be a logical matrix");
61 domain = (domain > 0);
63 ## Get number of histogram bins
70 ## Convert to 8 or 16 bit integers if needed
72 case {"double", "single", "int16", "int32", "int64", "uint16", "uint32", "uint64"}
73 min_val = double (min (I (:)));
74 max_val = double (max (I (:)));
75 if (min_val == max_val)
76 retval = zeros (size (I));
79 I = (double (I) - min_val)./(max_val - min_val);
81 case {"logical", "int8", "uint8"}
84 error ("entropyfilt: cannot handle images of class '%s'", class (I));
88 pad = floor (size (domain)/2);
89 I = padarray (I, pad, padding, varargin {:});
90 even = (round (size (domain)/2) == size (domain)/2);
91 idx = cell (1, ndims (I));
93 idx {k} = (even (k)+1):size (I, k);
98 retval = __spatial_filtering__ (I, domain, "entropy", I, nbins);