annotate scripts/image/ind2gray.m @ 33617:ec2635a02328 bytecode-interpreter tip

maint: Merge default to bytecode-interpreter.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 21 May 2024 18:29:03 +0200
parents 2e484f9f1f18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 1994-2024 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
8 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
2313
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 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
19 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
20 ## 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
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 917
diff changeset
25
3381
69b167451491 [project @ 1999-12-15 20:48:10 by jwe]
jwe
parents: 3373
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
27 ## @deftypefn {} {@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
28 ## 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
29 ##
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
30 ## The image @var{x} must be an indexed image which will be converted using the
24575
93714122ed59 doc: Fixes for signal, image, audio, and OOP chapters (bug #52844).
Rik <rik@octave.org>
parents: 23219
diff changeset
31 ## colormap @var{map}. If @var{map} does not contain enough colors for the
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
32 ## 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
33 ## 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
34 ##
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 ## 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
36 ## 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
37 ##
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
38 ## 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
39 ## grayscale intensities. This functions uses the luminance value obtained
24610
3ad53e4793fc rgb2gray: New function to convert RGB images to grayscale images.
Rik <rik@octave.org>
parents: 24576
diff changeset
40 ## from @code{rgb2gray} 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
41 ## 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
42 ## 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
43 ## @seealso{gray2ind, ind2rgb}
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3202
diff changeset
44 ## @end deftypefn
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 559
diff changeset
45
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
46 function I = ind2gray (x, map)
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 917
diff changeset
47
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
48 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
49 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
50 endif
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
51 [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
52
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
53 ## 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
54 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
55
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
56 ## 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
57 ## 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
58 cls = class (x);
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
59 if (isinteger (x))
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 20852
diff changeset
60 ## if we later add support for int16 images, this will not work. Look into
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
61 ## 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
62 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
63 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
64 map = single (map);
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
65 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
66
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
67 ## 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
68 I = reshape (map(x(:)), size (x));
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
69
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
70 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
71
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
72
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
73 %!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
74 %! i2g = ind2gray (1:100, gray (100));
18566
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
75 %!
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
76 %!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
77 %!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
78
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
79 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27985
diff changeset
80 %!error <Invalid call> ind2gray ()
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27985
diff changeset
81 %!error <Invalid call> ind2gray (1)
18566
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
82 %!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
83 %!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
84 %!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
85 %!error <X must be an indexed image> ind2gray (1.1, jet (64))
18566
875f4510d6dc Accept indexed images with color values below the first colormap entry (bug #41851).
Rik <rik@octave.org>
parents: 17744
diff changeset
86 %!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
87 %!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
88 %!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
89 %!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
90 %!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
91 %!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
92 %!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
93
18566
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 ([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
95 %!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
96 %!warning <contains colors outside of colormap> ind2gray (uint8 ([1 2 5]), gray (5));