annotate scripts/image/ind2gray.m @ 19630:0e1f5a750d00

maint: Periodic merge of gui-release to default.
author John W. Eaton <jwe@octave.org>
date Tue, 20 Jan 2015 10:24:46 -0500
parents 875f4510d6dc 446c46af4b42
children 4197fc428c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17281
diff changeset
1 ## Copyright (C) 1994-2013 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6653
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6653
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6653
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6653
diff changeset
17 ## <http://www.gnu.org/licenses/>.
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 917
diff changeset
18
3381
69b167451491 [project @ 1999-12-15 20:48:10 by jwe]
jwe
parents: 3373
diff changeset
19 ## -*- texinfo -*-
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
20 ## @deftypefn {Function File} {@var{I} =} ind2gray (@var{x}, @var{map})
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
21 ## Convert a color indexed image to a grayscale intensity image.
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>
parents: 15689 15683
diff changeset
22 ##
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
23 ## The image @var{x} must be an indexed image which will be converted using the
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
24 ## colormap @var{cmap}. If @var{cmap} does not contain enough colors for the
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
25 ## image, pixels in @var{x} outside the range are mapped to the last color in
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
26 ## the map before conversion to grayscale.
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
27 ##
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
28 ## The output @var{I} is of the same class as the input @var{x} and may be
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
29 ## one of @code{uint8}, @code{uint16}, @code{single}, or @code{double}.
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
30 ##
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
31 ## Implementation Note: There are several ways of converting colors to
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
32 ## grayscale intensities. This functions uses the luminance value obtained
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 15714
diff changeset
33 ## from @code{rgb2ntsc} which is @code{I = 0.299*R + 0.587*G + 0.114*B}.
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
34 ## Other possibilities include the value component from @code{rgb2hsv} or
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
35 ## using a single color channel from @code{ind2rgb}.
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
36 ## @seealso{gray2ind, ind2rgb}
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3202
diff changeset
37 ## @end deftypefn
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 559
diff changeset
38
3202
44d82b369c78 [project @ 1998-10-29 20:27:57 by jwe]
jwe
parents: 2847
diff changeset
39 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
40 ## Created: July 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
41 ## Adapted-By: jwe
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
42
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
43 function I = ind2gray (x, map)
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 917
diff changeset
44
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
45 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
46 print_usage ();
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
47 endif
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
48 [x, map] = ind2x ("ind2gray", x, map);
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
49
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
50 ## Convert colormap to luminance intensity values
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>
parents: 15689 15683
diff changeset
51 map *= [0.29894; 0.58704; 0.11402];
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
52
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
53 ## Convert colormap to same class as that of input so that reshape
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
54 ## will produce output of the same type as the input.
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>
parents: 15689 15683
diff changeset
55 cls = class (x);
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
56 if (isinteger (x))
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
57 ## if we later add support for int16 images, this will not work. Look into
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
58 ## im2int16 from image package for such case
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
59 map *= intmax (cls);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
60 elseif (strcmp (cls, "single"))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
61 map = single (map);
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
62 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
63
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
64 ## Replace indices in the input matrix with the indexed luminance value.
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
65 I = reshape (map(x(:)), size (x));
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
66
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
67 endfunction
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
68
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
69
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>
parents: 15689 15683
diff changeset
70 %!shared i2g
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
71 %! i2g = ind2gray (1:100, gray (100));
18578
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
72 %!
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
73 %!assert (i2g, 0:1/99:1, eps)
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
74 %!assert (gray2ind (i2g, 100), 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: 14260
diff changeset
75
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
76 %% Test input validation
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
77 %!error ind2gray ()
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
78 %!error ind2gray (1)
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
79 %!error ind2gray (1,2,3)
18578
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
80 %!error <X must be an indexed image> ind2gray (ones (3,3,3), jet (64))
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
81 %!error <X must be an indexed image> ind2gray (1+i, jet (64))
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
82 %!error <X must be an indexed image> ind2gray (sparse (1), jet (64))
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
83 %!error <X must be an indexed image> ind2gray (1.1, jet (64))
18578
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
84 %!error <X must be an indexed image> ind2gray ({1}, jet (64))
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
85 %!error <MAP must be a valid colormap> ind2gray (1, {1})
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
86 %!error <MAP must be a valid colormap> ind2gray (1, 1+i)
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
87 %!error <MAP must be a valid colormap> ind2gray (1, ones (2,2,2))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
88 %!error <MAP must be a valid colormap> ind2gray (1, ones (2,4))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
89 %!error <MAP must be a valid colormap> ind2gray (1, [-1])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
90 %!error <MAP must be a valid colormap> ind2gray (1, [2])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
91
18578
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
92 %!warning <contains colors outside of colormap> ind2gray ([0 1 2], gray (5));
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
93 %!warning <contains colors outside of colormap> ind2gray ([1 2 6], gray (5));
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
94 %!warning <contains colors outside of colormap> ind2gray (uint8 ([1 2 5]), gray (5));
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
95