]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/entropyfilt.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / entropyfilt.m
1 ## Copyright (C) 2008 Søren Hauberg
2 ## 
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.
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 ## 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
16
17 ## -*- texinfo -*-
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.
22 ##
23 ## The entropy of the elements of the neighbourhood is computed as
24 ##
25 ## @example
26 ## @var{E} = -sum (@var{P} .* log2 (@var{P})
27 ## @end example
28 ##
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
32 ## are used.
33 ##
34 ## When the entropy is computed, zero-valued cells of the histogram are ignored.
35 ##
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.
39 ##
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
43 ## the image border.
44 ##
45 ## @seealso{entropy, paddarray, stdfilt}
46 ## @end deftypefn
47
48 function retval = entropyfilt (I, domain = true (9), padding = "symmetric", varargin)
49   ## Check input
50   if (nargin == 0)
51     error ("entropyfilt: not enough input arguments");
52   endif
53   
54   if (!ismatrix (I))
55     error ("entropyfilt: first input must be a matrix");
56   endif
57   
58   if (!ismatrix (domain))
59     error ("entropyfilt: second input argument must be a logical matrix");
60   endif
61   domain = (domain > 0);
62   
63   ## Get number of histogram bins
64   if (islogical (I))
65     nbins = 2;
66   else
67     nbins = 256;
68   endif
69   
70   ## Convert to 8 or 16 bit integers if needed
71   switch (class (I))
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));
77         return;
78       endif
79       I = (double (I) - min_val)./(max_val - min_val);
80       I = uint8 (255 * I);
81     case {"logical", "int8", "uint8"}
82       ## Do nothing
83     otherwise
84       error ("entropyfilt: cannot handle images of class '%s'", class (I));
85   endswitch
86   size (I)
87   ## Pad image
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));
92   for k = 1:ndims (I)
93     idx {k} = (even (k)+1):size (I, k);
94   endfor
95   I = I (idx {:});
96   size (I)
97   ## Perform filtering
98   retval = __spatial_filtering__ (I, domain, "entropy", I, nbins);
99
100 endfunction