]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/ordfilt2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / ordfilt2.m
1 ## Copyright (C) 2000 Teemu Ikonen
2 ##
3 ## This program is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU General Public License
5 ## as published by the Free Software Foundation; either version 2
6 ## of the License, or (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## 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, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} ordfilt2(@var{A}, @var{nth}, @var{domain}, [@var{S}, @var{padding}])
18 ## Two dimensional ordered filtering.
19 ##
20 ## Ordered filter replaces an element of @var{A} with the @var{nth} 
21 ## element of the sorted set of neighbours defined by the logical 
22 ## (boolean) matrix @var{domain}.
23 ## Neighbour elements are selected to the sort if the corresponding 
24 ## element in the @var{domain} matrix is true.
25 ## 
26 ## The optional variable @var{S} is a matrix of size(@var{domain}). 
27 ## Values of @var{S} corresponding to nonzero values of domain are 
28 ## added to values obtained from @var{A} when doing the sorting.
29 ##
30 ## Optional variable @var{padding} determines how the matrix @var{A} 
31 ## is padded from the edges. See impad for details.
32 ## 
33 ## @seealso{medfilt2}
34 ## @end deftypefn
35
36
37 ## Author: Teemu Ikonen <tpikonen@pcu.helsinki.fi>
38 ## Created: 5.5.2000
39 ## Keywords: image processing filtering
40
41 function retval = ordfilt2(A, nth, domain, varargin)
42
43 S = zeros(size(domain));
44 padding = "zeros";
45 for i=1:length(varargin)
46   a = varargin{:};
47   if(ischar(a))
48     padding = a;
49   elseif(ismatrix(a) && size(a) == size(domain))
50     S = a;
51   endif
52 endfor
53
54 domain = logical(domain);
55
56 xpad(1) = floor((size(domain, 2)+1)/2) - 1;
57 xpad(2) = size(domain,2) - xpad(1) - 1;
58 ypad(1) = floor((size(domain, 1)+1)/2) - 1;
59 ypad(2) = size(domain,1) - ypad(1) - 1;
60
61 if(ypad(1) >= size(A,1) || xpad(1) >= size(A,2))
62   error("domain matrix too large");
63 endif;
64
65 A = impad(A, xpad, ypad, padding);
66 retval = __spatial_filtering__ (A, domain, "ordered", S, nth);
67
68 endfunction