/tags/R2008-02-16/main/image/inst/im2bw.m

# · MATLAB · 72 lines · 66 code · 6 blank · 0 comment · 13 complexity · d2c649fec49565d1f6e971110e3e10e9 MD5 · raw file

  1. ## Copyright (C) 2000 Kai Habel
  2. ##
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2 of the License, or
  6. ## (at your option) any later version.
  7. ##
  8. ## This program is distributed in the hope that it will be useful,
  9. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ## GNU 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, see <http://www.gnu.org/licenses/>.
  15. ## -*- texinfo -*-
  16. ## @deftypefn {Function File} @var{BW}= im2bw (@var{I},threshold)
  17. ## @deftypefnx {Function File} @var{BW}= im2bw (@var{X},@var{cmap},threshold)
  18. ## Converts image data types to a black-white (binary) image.
  19. ## The treshold value should be in the range [0,1].
  20. ## @end deftypefn
  21. ## Author: Kai Habel <kai.habel@gmx.de>
  22. ## Date: 19. March 2000
  23. function BW = im2bw (img, a, b)
  24. if (nargin < 2 || nargin > 3)
  25. usage("im2bw: number of arguments must be 2 or 3");
  26. endif
  27. ## Convert img to gray scale
  28. if (isrgb(img))
  29. img = rgb2gray(img);
  30. if (nargin != 2)
  31. error("im2bw: two input arguments must be given when the image is a color image");
  32. endif
  33. t = a;
  34. elseif (isind (img) && ismatrix(a) && columns (a) == 3)
  35. img = ind2gray (img, a);
  36. if (nargin != 3)
  37. error("im2bw: three input arguments must be given when the image is indexed");
  38. endif
  39. t = b;
  40. elseif (isgray(img))
  41. if (nargin != 2)
  42. error("im2bw: two input arguments must be given when the image is gray scale");
  43. endif
  44. t = a;
  45. else
  46. error ("im2bw: first input argument must be an image");
  47. endif
  48. ## Do the thresholding
  49. if (isscalar (a))
  50. if (a < 0 || a > 1)
  51. error("im2bw: threshold must be in the interval [0, 1]");
  52. endif
  53. switch (class(img))
  54. case {"double", "single"} % octave doesn't support single yet, but it shouldn't hurt...
  55. BW = (img >= a);
  56. case {"uint8"}
  57. BW = (img >= 255*a);
  58. case {"uint16"}
  59. BW = (img >= 65535*a);
  60. otherwise
  61. error("im2bw: unsupport image class");
  62. endswitch
  63. else
  64. error ("im2bw: threshold value must be scalar");
  65. endif
  66. endfunction