]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/grayslice.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / grayslice.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{X} =} grayslice (@var{I},@var{n})
18 ## @deftypefnx {Function File} {@var{X} =} grayslice (@var{I},@var{v})
19 ## creates an indexed image @var{X} from an intensitiy image @var{I}
20 ## using multiple threshold levels.
21 ## A scalar integer value @var{n} sets the levels to
22 ## @example
23 ## 
24 ## @group
25 ## 1  2       n-1
26 ## -, -, ..., ---
27 ## n  n        n
28 ## @end group
29 ## @end example
30 ##
31 ## X = grayslice(I,5);
32 ##
33 ## For irregular threshold values a real vector @var{v} can be used.
34 ## The values must be in the range [0,1].
35 ##
36 ## @group
37 ## X = grayslice(I,[0.1,0.33,0.75,0.9])
38 ## @end group
39 ##
40 ## @seealso{im2bw}
41 ## @end deftypefn
42
43 ## Author:      Kai Habel <kai.habel@gmx.de>
44 ## Date:        03. August 2000
45
46 function X = grayslice (I, v)
47
48   if (nargin != 2)
49     usage ("grayslice(...) number of arguments must be 1 or 2");
50   endif
51
52   if (is_scalar(v) && (fix(v) == v))
53
54     v = (1:v - 1) / v;
55
56   elseif (isvector(v))
57
58     if (any (v < 0) || (any (v > 1)))
59       error ("slice vector must be in range [0,1]")
60     endif
61     v = [0,v,1];
62   else
63
64     usage("second argument");
65
66   endif
67
68   [r, c] = size (I);
69   [m, n] = sort ([v(:); I(:)]);
70   lx = length (v);
71   o = cumsum (n <= lx);
72   idx = o (find(n>lx));
73   [m, n] = sort (I(:));
74   [m, n] = sort (n);
75   X = reshape (idx(n), r, c);
76
77 endfunction