/usr/share/octave/packages/image-2.2.2/medfilt2.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 | ## Copyright (C) 2000 Teemu Ikonen <tpikonen@pcu.helsinki.fi>
##
## 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} {} medfilt2 (@var{A})
## @deftypefnx {Function File} {} medfilt2 (@var{A}, @var{nhood})
## @deftypefnx {Function File} {} medfilt2 (@var{A}, [@var{M} @var{N}])
## @deftypefnx {Function File} {} medfilt2 (@dots{}, @var{pad})
## Two dimensional median filtering.
##
## Replaces elements of @var{A} with the median of their neighbours as defined
## by the true elements of logical matrix @var{nhood} or by a matrix of size
## @var{M} by @var{N}. The default @var{nhood} is a 3 by 3 matrix of true
## elements.
##
## @example
## @group
## ## median filtering specifying neighborhood dimensions
## medfilt2 (img) # default is [3 3]
## medfilt2 (img, [3 1]) # a 3x1 vector
## medfilt2 (img, [5 5]) # 5 element wide square
## @end group
##
## @group
## ## median filtering specifying neighborhood
## medfilt2 (img, true (5)) # same as [5 5]
## nhood = logical ([0 1 0
## 1 1 1
## 0 1 0]);
## medfilt2 (img, nhood) # 3 element wide cross
## @end group
## @end example
##
## The optional variable @var{pad} defines the padding used in augmenting
## the borders of @var{A}. See @code{padarray} for details.
## @seealso{ordfilt2, ordfiltn}
## @end deftypefn
function retval = medfilt2 (A, varargin)
if (nargin < 1 || nargin > 3)
print_usage ();
elseif (! isimage (A))
error ("medfilt2: A must be a real matrix")
endif
## defaults
padding = "zeros";
domain = true (3, 3);
for idx = 1:numel (varargin)
opt = varargin{idx};
if (ischar (opt) || isscalar (opt))
padding = opt;
elseif (isnumeric (opt) || islogical (opt))
## once we have removed the ability of selecting domain with a non logical
## matrix, we can use it to make this N dimensional domain = [3 3 3].
if (isvector (opt) && ! islogical (opt) && numel (opt) == 2)
domain = true (opt);
else
if (! islogical (opt))
persistent warned = false;
if (! warned)
warned = true;
warning ("medfilt2: to specify filter instead of dimensions use a matrix of logical class. This usage will be removed from future releases of the image package.");
endif
endif
domain = logical (opt);
endif
else
error ("medfilt2: unrecognized option of class %s", class (opt))
endif
endfor
## TODO this should probably be implemented as an option of
## __spatial_filtering__ where median() cold be called directly
n = nnz (domain);
if ((n - 2*floor(n/2)) == 0) % n even - more work
nth = floor (n/2);
a = ordfiltn (A, nth, domain, padding);
b = ordfiltn (A, nth + 1, domain, padding);
retval = a./2 + b./2; # split into two divisions to avoid overflow on integer data
else
nth = floor (n/2) + 1;
retval = ordfiltn (A, nth, domain, padding);
endif
endfunction
%!shared b, f
%! b = [ 0 1 2 3
%! 1 8 12 12
%! 4 20 24 21
%! 7 22 25 18];
%! f = [ 0 1 2 0
%! 1 4 12 3
%! 4 12 20 12
%! 0 7 20 0];
%!assert (medfilt2 (b), f);
%!
%! f = [ 0 1 2 3
%! 1 8 12 12
%! 4 20 24 18
%! 4 20 24 18];
%!assert (medfilt2 (b, true (3, 1)), f);
%!assert (medfilt2 (b, [3 1]), f);
%!
%! f = [ 1 8 10 10
%! 1 8 12 12
%! 4 20 24 18
%! 7 20 24 18];
%!assert (medfilt2 (b, [3 1], 10), f);
%!assert (medfilt2 (b, 10, [3 1]), f);
%!
%! f = [ 0.5 4.5 7.0 7.5
%! 2.5 14.0 18.0 15.0
%! 2.5 14.0 18.0 15.0
%! 2.0 10.0 12.0 9.0];
%!assert (medfilt2 (b, true (4, 1)), f);
%!assert (medfilt2 (b, [4 1]), f);
|