1 ## Copyright (C) 2008 Soren Hauberg
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 3
6 ## of the License, or (at your option) any later version.
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.
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/>.
16 ## This function is based on 'ordfilt2' by Teemu Ikonen which is released under
20 ## @deftypefn {Function File} {} ordfiltn(@var{A}, @var{nth}, @var{domain}, [@var{S}, @var{padding}])
21 ## Two dimensional ordered filtering.
23 ## Ordered filter replaces an element of @var{A} with the @var{nth}
24 ## element of the sorted set of neighbours defined by the logical
25 ## (boolean) matrix @var{domain}.
26 ## Neighbour elements are selected to the sort if the corresponding
27 ## element in the @var{domain} matrix is true.
29 ## The optional variable @var{S} is a matrix of size(@var{domain}).
30 ## Values of @var{S} corresponding to nonzero values of domain are
31 ## added to values obtained from @var{A} when doing the sorting.
33 ## Optional variable @var{padding} determines how the matrix @var{A}
34 ## is padded from the edges. See @code{padarray} for details.
36 ## @seealso{ordfilt2, padarray}
40 ## Author: Teemu Ikonen <tpikonen@pcu.helsinki.fi>
42 ## Keywords: image processing filtering
44 function retval = ordfiltn(A, nth, domain, varargin)
47 error("ordfiltn: not enough input arguments");
50 error("ordfiltn: first input must be an array");
52 if (!isscalar(nth) || nth <= 0 || nth != round(nth))
53 error("ordfiltn: second input argument must be a positive integer");
55 if (!ismatrix(domain) && !isscalar(domain))
56 error("ordfiltn: third input argument must be an array or a scalar");
58 if (isscalar(domain) && (domain <= 0 || domain != round(domain)))
59 error("ordfiltn: third input argument must be a positive integer, when it is a scalar");
62 domain = ones(repmat(domain, 1, ndims(A)), "logical");
65 if (ndims(A) != ndims(domain))
66 error("ordfiltn: first and second argument must have same dimensionality");
68 if (any(size(A) < size(domain)))
69 error("ordfiltn: domain array cannot be larger than the data array");
73 S = zeros(size(domain));
75 for i=1:length(varargin)
77 if (ischar(a) || isscalar(a))
79 elseif (ismatrix(a) && size_equal(a, domain))
84 ## Make sure 'domain' is logical. The C++ code assumes this.
85 domain = logical(domain);
88 pad = floor(size(domain)/2);
89 A = padarray(A, pad, padding);
90 even = ( round(size(domain)/2) == size(domain)/2 );
91 idx = cell(1, ndims(A));
93 idx{k} = (even(k)+1):size(A,k);
97 ## Perform the filtering
98 retval = __spatial_filtering__ (A, domain, "ordered", S, nth);