]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/im2bw.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / im2bw.m
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
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} @var{BW}= im2bw (@var{I},threshold)
18 ## @deftypefnx {Function File} @var{BW}= im2bw (@var{X},@var{cmap},threshold)
19 ## Converts image data types to a black-white (binary) image.
20 ## The treshold value should be in the range [0,1].
21 ## @end deftypefn
22
23 ## Author:      Kai Habel <kai.habel@gmx.de>
24 ## Date:        19. March 2000
25
26 function BW = im2bw (img, a, b)
27   if (nargin < 2 || nargin > 3)
28     usage("im2bw: number of arguments must be 2 or 3");
29   endif
30   
31   ## Convert img to gray scale
32   if (isrgb(img))
33     img = rgb2gray(img);
34     if (nargin != 2)
35       error("im2bw: two input arguments must be given when the image is a color image");
36     endif
37     t = a;
38   elseif (isind (img) && ismatrix(a) && columns (a) == 3)
39     img = ind2gray (img, a);
40     if (nargin != 3)
41       error("im2bw: three input arguments must be given when the image is indexed");
42     endif
43     t = b;
44   elseif (isgray(img))
45     if (nargin != 2)
46       error("im2bw: two input arguments must be given when the image is gray scale");
47     endif
48     t = a;
49   else
50     error ("im2bw: first input argument must be an image");
51   endif
52
53   ## Do the thresholding
54   if (isscalar (t))
55     if (t < 0 || t > 1)
56       error("im2bw: threshold must be in the interval [0, 1]");
57     endif
58     switch (class(img))
59       case {"double", "single"}
60         BW = (img >= t);
61       case {"uint8"}
62         BW = (img >= 255*t);
63       case {"uint16"}
64         BW = (img >= 65535*t);
65       otherwise
66         error("im2bw: unsupport image class");
67     endswitch
68   else
69     error ("im2bw: threshold value must be scalar");
70   endif
71
72 endfunction