/tags/R2002-04-20/octave-forge/main/image/ordfilt2.m

# · Objective C · 74 lines · 64 code · 10 blank · 0 comment · 10 complexity · d9877bb49c7cbeac095c229239faa5e2 MD5 · raw file

  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, write to the Free Software
  15. ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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. ## @end deftypefn
  34. ## @seealso{medfilt2}
  35. ## Author: Teemu Ikonen <tpikonen@pcu.helsinki.fi>
  36. ## Created: 5.5.2000
  37. ## Keywords: image processing filtering
  38. function retval = ordfilt2(A, nth, domain, ...)
  39. S = zeros(size(domain));
  40. padding = "zeros";
  41. nargin = nargin - 3;
  42. va_start();
  43. while(nargin--)
  44. a = va_arg();
  45. if(isstr(a))
  46. padding = a;
  47. elseif(is_matrix(a) && size(a) == size(domain))
  48. S = a;
  49. endif
  50. endwhile
  51. if(!islogical(domain))
  52. % warning("domain should be a boolean matrix, converting");
  53. domain = logical(domain);
  54. endif;
  55. xpad(1) = floor((size(domain, 2)+1)/2) - 1;
  56. xpad(2) = size(domain,2) - xpad(1) - 1;
  57. ypad(1) = floor((size(domain, 1)+1)/2) - 1;
  58. ypad(2) = size(domain,1) - ypad(1) - 1;
  59. if(ypad(1) >= size(A,1) || xpad(1) >= size(A,2))
  60. error("domain matrix too large");
  61. endif;
  62. A = impad(A, xpad, ypad, padding);
  63. retval = cordflt2(A, nth, domain, S);
  64. endfunction