PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/octave-3.6.2/scripts/image/gray2ind.m

#
Objective C | 61 lines | 56 code | 5 blank | 0 comment | 13 complexity | 156167dcabc65a1c5ef50fd9bc56b0b4 MD5 | raw file
Possible License(s): GPL-3.0
  1. ## Copyright (C) 1994-2012 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 3 of the License, or (at
  8. ## your option) any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING. If not, see
  17. ## <http://www.gnu.org/licenses/>.
  18. ## -*- texinfo -*-
  19. ## @deftypefn {Function File} {[@var{img}, @var{map}] =} gray2ind (@var{I}, @var{n})
  20. ## Convert a gray scale intensity image to an Octave indexed image.
  21. ## The indexed image will consist of @var{n} different intensity values. If not
  22. ## given @var{n} will default to 64.
  23. ## @end deftypefn
  24. ## Author: Tony Richardson <arichard@stark.cc.oh.us>
  25. ## Created: July 1994
  26. ## Adapted-By: jwe
  27. function [X, map] = gray2ind (I, n = 64)
  28. ## Check input
  29. if (nargin < 1 || nargin > 2)
  30. print_usage ();
  31. endif
  32. C = class(I);
  33. if (! ismatrix (I) || ndims (I) != 2)
  34. error ("gray2ind: first input argument must be a gray scale image");
  35. endif
  36. if (! isscalar (n) || n < 0)
  37. error ("gray2ind: second input argument must be a positive integer");
  38. endif
  39. ints = {"uint8", "uint16", "int8", "int16"};
  40. floats = {"double", "single"};
  41. if (! ismember (C, {ints{:}, floats{:}}))
  42. error ("gray2ind: invalid data type '%s'", C);
  43. endif
  44. if (ismember (C, floats) && (min (I(:)) < 0 || max (I(:)) > 1))
  45. error ("gray2ind: floating point images may only contain values between 0 and 1");
  46. endif
  47. ## Convert data
  48. map = gray (n);
  49. ## If @var{I} is an integer matrix convert it to a double matrix with values in [0, 1]
  50. if (ismember (C, ints))
  51. low = double (intmin (C));
  52. high = double (intmax (C));
  53. I = (double (I) - low) / (high - low);
  54. endif
  55. X = round (I*(n-1)) + 1;
  56. endfunction