Mercurial > hg > octave-nkf
annotate scripts/image/gray2ind.m @ 15690:7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
author | Carnë Draug <carandraug+dev@gmail.com> |
---|---|
date | Mon, 12 Nov 2012 05:25:37 +0000 |
parents | 5d3a684236b0 |
children | dffb28f47ea8 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1 ## Copyright (C) 1994-2012 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
559 | 18 |
3381 | 19 ## -*- texinfo -*- |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{img} =} gray2ind (@var{I}) |
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{img} =} gray2ind (@var{I}, @var{n}) |
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{img}, @var{map} =} gray2ind (@dots{}) |
15690
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
23 ## Convert a gray scale or binary image to an indexed image. |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
24 ## |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
25 ## The indexed image will consist of @var{n} different intensity values. |
15690
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
26 ## If not given @var{n} defaults to 64 and 2 for gray scale and binary images |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
27 ## respectively. |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
28 ## @seealso{ind2gray, rgb2ind} |
3373 | 29 ## @end deftypefn |
1024 | 30 |
3202 | 31 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 32 ## Created: July 1994 |
33 ## Adapted-By: jwe | |
1024 | 34 |
7375 | 35 function [X, map] = gray2ind (I, n = 64) |
36 ## Check input | |
1024 | 37 if (nargin < 1 || nargin > 2) |
6046 | 38 print_usage (); |
7375 | 39 endif |
15690
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
40 |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
41 ## default n is different if image is logical |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
42 if (nargin < 2 && isa (I, "logical")) |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
43 n = 2; |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
44 endif |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
45 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14260
diff
changeset
|
46 C = class (I); |
7375 | 47 if (! ismatrix (I) || ndims (I) != 2) |
48 error ("gray2ind: first input argument must be a gray scale image"); | |
49 endif | |
50 if (! isscalar (n) || n < 0) | |
51 error ("gray2ind: second input argument must be a positive integer"); | |
52 endif | |
53 ints = {"uint8", "uint16", "int8", "int16"}; | |
54 floats = {"double", "single"}; | |
15690
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
55 if (! ismember (C, {ints{:}, floats{:}, "logical"})) |
7375 | 56 error ("gray2ind: invalid data type '%s'", C); |
57 endif | |
58 if (ismember (C, floats) && (min (I(:)) < 0 || max (I(:)) > 1)) | |
59 error ("gray2ind: floating point images may only contain values between 0 and 1"); | |
559 | 60 endif |
61 | |
7375 | 62 ## Convert data |
1024 | 63 map = gray (n); |
7375 | 64 ## If @var{I} is an integer matrix convert it to a double matrix with values in [0, 1] |
65 if (ismember (C, ints)) | |
66 low = double (intmin (C)); | |
67 high = double (intmax (C)); | |
68 I = (double (I) - low) / (high - low); | |
69 endif | |
1024 | 70 X = round (I*(n-1)) + 1; |
559 | 71 |
72 endfunction |