Mercurial > hg > octave-nkf
annotate scripts/image/gray2ind.m @ 16659:608e307b4914 ss-3-7-5
snapshot 3.7.5
* configure.ac (OCTAVE_VERSION): Bump to 3.7.5.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 14 May 2013 05:23:45 -0400 |
parents | b1cd65881592 |
children | 3dec0a57ab55 |
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 -*- |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
20 ## @deftypefn {Function File} {@var{img} =} gray2ind (@var{I}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{img} =} gray2ind (@var{I}, @var{n}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{img} =} gray2ind (@var{BW}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{img} =} gray2ind (@var{BW}, @var{n}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{img}, @var{map}] =} gray2ind (@dots{}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
25 ## Convert a grayscale or binary intensity image to an indexed image. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
26 ## |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
27 ## The indexed image will consist of @var{n} different intensity values. |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
28 ## If not given @var{n} defaults to 64 for grayscale images or 2 for |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
29 ## binary black and white images. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
30 ## |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
31 ## The output @var{img} is of class uint8 if @var{n} is less than or |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
32 ## equal to 256; Otherwise the return class is uint16. |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
33 ## @seealso{ind2gray, rgb2ind} |
3373 | 34 ## @end deftypefn |
1024 | 35 |
3202 | 36 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 37 ## Created: July 1994 |
38 ## Adapted-By: jwe | |
1024 | 39 |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
40 function [I, map] = gray2ind (I, n = 64) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
41 |
1024 | 42 if (nargin < 1 || nargin > 2) |
6046 | 43 print_usage (); |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
44 elseif (! isreal (I) || issparse (I)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
45 error ("gray2ind: I must be a grayscale or binary image"); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
46 elseif (! isscalar (n) || n < 1 || n > 65536) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
47 error ("gray2ind: N must be a positive integer in the range [1, 65536]"); |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
48 elseif (! ismatrix (I) || ndims (I) != 2) |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
49 error ("gray2ind: I must be a grayscale or binary image"); |
7375 | 50 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
|
51 |
7d21456c09d1
gray2ind: also accept binary images and default n to 2 in such cases
Carnë Draug <carandraug+dev@gmail.com>
parents:
14868
diff
changeset
|
52 ## default n is different if image is logical |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
53 if (nargin == 1 && islogical (I)) |
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
|
54 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
|
55 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
|
56 |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
57 cls = class (I); |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
58 if (! any (strcmp (cls, {"logical", "uint8", "uint16", "int16", "single", "double"}))) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
59 error ("gray2ind: invalid data type '%s'", cls); |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
60 elseif (isfloat (I) && (min (I(:) < 0) || max (I(:) > 1))) |
7375 | 61 error ("gray2ind: floating point images may only contain values between 0 and 1"); |
559 | 62 endif |
63 | |
1024 | 64 map = gray (n); |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
65 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
66 ## Set up scale factor |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
67 if (isinteger (I)) |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
68 low = double (intmin (cls)); |
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
69 scale = double (intmax (cls)) - low; |
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
70 I = double (I) - low; |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
71 else |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
72 scale = 1; |
7375 | 73 endif |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
74 I *= (n-1)/scale; |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
75 |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
76 ## Note: no separate call to round () necessary because |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
77 ## type conversion does that automatically. |
15691
dffb28f47ea8
gray2ind: return indexed image as integer for matlab compatibility
Carnë Draug <carandraug+dev@gmail.com>
parents:
15690
diff
changeset
|
78 if (n < 256) |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
79 I = uint8 (I); |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
80 else |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
81 I = uint16 (I); |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
82 endif |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
83 |
559 | 84 endfunction |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
85 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
86 |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
87 %!assert (gray2ind ([0 0.25 0.5 1]), uint8 ([0 16 32 63])) |
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
88 %!assert (gray2ind ([0 0.25 0.5 1], 400), uint16 ([0 100 200 399])) |
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
89 %!assert (gray2ind (logical ([1 0 0 1])), uint8 ([1 0 0 1])) |
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
90 %!assert (gray2ind (uint8 ([0 64 128 192 255])), uint8 ([0 16 32 47 63])) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
91 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
92 %!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
93 %! i2g = ind2gray (1:100, gray (100)); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
94 %! g2i = gray2ind (i2g, 100); |
15693
a1b634240352
maint: merge default into image-overhaul head, specially changeset 806ea52. Resolving many conflicts and fixing bugs related to correctly indexing on image class (float vs integer have different offset)
Carnë Draug <carandraug+dev@gmail.com>
diff
changeset
|
95 %! assert (g2i, uint8 (0:99)); |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
96 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
97 %% Test input validation |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
98 %!error gray2ind () |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
99 %!error gray2ind (1,2,3) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
100 %!error <I must be a grayscale or binary image> gray2ind ({1}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
101 %!error <I must be a grayscale or binary image> gray2ind ([1+i]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
102 %!error <I must be a grayscale or binary image> gray2ind (sparse ([1])) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
103 %!error <N must be a positive integer> gray2ind (1, ones (2,2)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
104 %!error <N must be a positive integer> gray2ind (1, 0) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
105 %!error <N must be a positive integer> gray2ind (1, 65537) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
106 %!error <invalid data type> gray2ind (uint32 (1)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
107 %!error <values between 0 and 1> gray2ind (-1) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
108 %!error <values between 0 and 1> gray2ind (2) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
109 |