]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/stdfilt.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / stdfilt.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{S} =} stdfilt (@var{im})
19 ## @deftypefnx{Function File} {@var{S} =} stdfilt (@var{im}, @var{domain})
20 ## @deftypefnx{Function File} {@var{S} =} stdfilt (@var{im}, @var{domain}, @var{padding}, ...)
21 ## Computes the local standard deviation in a neighbourhood around each pixel in
22 ## an image.
23 ##
24 ## The standard deviation of the pixels of a neighbourhood is computed as
25 ##
26 ## @example
27 ## @var{S} = sqrt ((sum (@var{x} - @var{mu}).^2)/(@var{N}-1))
28 ## @end example
29 ##
30 ## where @var{mu} is the mean value of the pixels in the neighbourhood,
31 ## @var{N} is the number of pixels in the neighbourhood. So, an unbiased estimator
32 ## is used.
33 ##
34 ## The neighbourhood is defined by the @var{domain} binary mask. Elements of the
35 ## mask with a non-zero value are considered part of the neighbourhood. By default
36 ## a 3 by 3 matrix containing only non-zero values is used.
37 ##
38 ## At the border of the image, extrapolation is used. By default symmetric
39 ## extrapolation is used, but any method supported by the @code{padarray} function
40 ## can be used. Since extrapolation is used, one can expect a lower deviation near
41 ## the image border.
42 ##
43 ## @seealso{std2, paddarray, entropyfilt}
44 ## @end deftypefn
45
46 function retval = stdfilt (I, domain = true (3), padding = "symmetric", varargin)
47   ## Check input
48   if (nargin == 0)
49     error ("stdfilt: not enough input arguments");
50   endif
51   
52   if (!ismatrix (I))
53     error ("stdfilt: first input must be a matrix");
54   endif
55   
56   if (!ismatrix (domain))
57     error ("stdfilt: second input argument must be a logical matrix");
58   endif
59   domain = (domain > 0);
60   
61   ## Pad image
62   pad = floor (size (domain)/2);
63   I = padarray (I, pad, padding, varargin {:});
64   even = (round (size (domain)/2) == size (domain)/2);
65   idx = cell (1, ndims (I));
66   for k = 1:ndims (I)
67     idx {k} = (even (k)+1):size (I, k);
68   endfor
69   I = I (idx {:});
70
71   ## Perform filtering
72   retval = __spatial_filtering__ (I, domain, "std", I, 0);
73
74 endfunction