/usr/share/octave/packages/image-2.2.2/ordfiltn.m is in octave-image 2.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | ## Copyright (C) 2008 Søren Hauberg <soren@hauberg.org>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {} ordfiltn (@var{A}, @var{nth}, @var{domain})
## @deftypefnx {Function File} {} ordfiltn (@var{A}, @var{nth}, @var{domain}, @var{S})
## @deftypefnx {Function File} {} ordfiltn (@dots{}, @var{padding})
## N dimensional ordered filtering.
##
## Ordered filter replaces an element of @var{A} with the @var{nth} element
## element of the sorted set of neighbours defined by the logical
## (boolean) matrix @var{domain}.
## Neighbour elements are selected to the sort if the corresponding
## element in the @var{domain} matrix is true.
##
## The optional variable @var{S} is a matrix of size(@var{domain}).
## Values of @var{S} corresponding to nonzero values of domain are
## added to values obtained from @var{A} when doing the sorting.
##
## Optional variable @var{padding} determines how the matrix @var{A}
## is padded from the edges. See @code{padarray} for details.
##
## @seealso{medfilt2, padarray, ordfilt2}
## @end deftypefn
## This function is based on 'ordfilt2' by Teemu Ikonen <tpikonen@pcu.helsinki.fi>
## which is released under GPLv2 or later.
function retval = ordfiltn (A, nth, domain, varargin)
## Check input
if (nargin < 3)
print_usage ();
elseif (! ismatrix (A))
error ("ordfiltn: first input must be an array");
elseif (! isscalar (nth) || nth <= 0 || fix (nth) != nth)
error ("ordfiltn: second input argument must be a positive integer");
elseif (! ismatrix (domain) && ! isscalar (domain))
error ("ordfiltn: third input argument must be an array or a scalar");
elseif (isscalar (domain) && (domain <= 0 || fix (domain) != domain))
error ("ordfiltn: third input argument must be a positive integer, when it is a scalar");
endif
if (isscalar (domain))
domain = true (repmat (domain, 1, ndims (A)));
endif
if (ndims (A) != ndims (domain))
error ("ordfiltn: first and second argument must have same dimensionality");
elseif (any (size (A) < size (domain)))
error ("ordfiltn: domain array cannot be larger than the data array");
endif
## Parse varargin
S = zeros (size (domain));
padding = 0;
for idx = 1:length(varargin)
opt = varargin{idx};
if (ischar (opt) || isscalar (opt))
padding = opt;
elseif (ismatrix (opt) && size_equal (opt, domain))
S = opt;
else
error ("ordfiltn: unrecognized option from input argument #%i and class %s", 3 + idx, class (opt));
endif
endfor
A = pad_for_sliding_filter (A, size (domain), padding);
## Perform the filtering
retval = __spatial_filtering__ (A, logical (domain), "ordered", S, nth);
endfunction
%!shared b, f, s
%! b = [ 0 1 2 3
%! 1 8 12 12
%! 4 20 24 21
%! 7 22 25 18];
%!
%! f = [ 8 12 12 12
%! 20 24 24 24
%! 22 25 25 25
%! 22 25 25 25];
%!assert (ordfiltn (b, 9, true (3)), f);
%!
%! f = [ 1 8 12 12
%! 8 20 21 21
%! 20 24 24 24
%! 20 24 24 24];
%!assert (ordfiltn (b, 8, true (3)), f);
%!
%! f = [ 1 2 8 12
%! 4 12 20 21
%! 8 22 22 21
%! 20 24 24 24];
%!assert (ordfiltn (b, 7, true (3), "symmetric"), f);
%!
%! f = [ 1 8 12 12
%! 4 20 24 21
%! 7 22 25 21
%! 7 22 25 21];
%!assert (ordfiltn (b, 3, true (3, 1)), f);
%!
%! f = [ 1 8 12 12
%! 4 20 24 18
%! 4 20 24 18
%! 4 20 24 18];
%!assert (ordfiltn (b, 3, true (4, 1)), f);
%!
%! f = [ 4 20 24 21
%! 7 22 25 21
%! 7 22 25 21
%! 7 22 25 21];
%!assert (ordfiltn (b, 4, true (4, 1)), f);
%!
%! s = [0 0 1
%! 0 0 1
%! 0 0 1];
%! f = [ 2 8 12 12
%! 9 20 22 21
%! 21 25 24 24
%! 21 25 24 24];
%!assert (ordfiltn (b, 8, true (3), s), f);
%!
%! b(:,:,2) = b(:,:,1) - 1;
%! b(:,:,3) = b(:,:,2) - 1;
%! f(:,:,1) = [ 1 8 11 11
%! 8 20 21 21
%! 20 24 24 24
%! 20 24 24 24];
%! f(:,:,2) = [ 6 10 11 11
%! 18 22 22 22
%! 20 24 24 24
%! 20 24 24 24];
%! f(:,:,3) = [ 0 7 10 10
%! 7 19 20 20
%! 19 23 23 23
%! 19 23 23 23];
%!assert (ordfiltn (b, 25, true (3, 3, 3)), f);
|