annotate scripts/image/rgb2hsv.m @ 19833:9fc020886ae9

maint: Clean up m-files to follow Octave coding conventions. Try to trim long lines to < 80 chars. Use '##' for single line comments. Use '(...)' around tests for if/elseif/switch/while. Abut cell indexing operator '{' next to variable. Abut array indexing operator '(' next to variable. Use space between negation operator '!' and following expression. Use two newlines between endfunction and start of %!test or %!demo code. Remove unnecessary parens grouping between short-circuit operators. Remove stray extra spaces (typos) between variables and assignment operators. Remove stray extra spaces from ends of lines.
author Rik <rik@octave.org>
date Mon, 23 Feb 2015 14:54:39 -0800
parents 4197fc428c7d
children 131ce8cfaa80
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 1999-2015 Kai Habel
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
2 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
4 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
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: 7001
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: 7001
diff changeset
8 ## your option) any later version.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
9 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
14 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
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: 7001
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: 7001
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
18
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
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{hsv_map} =} rgb2hsv (@var{rgb})
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
21 ## @deftypefnx {Function File} {@var{hsv_map} =} rgb2hsv (@var{rgb})
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
22 ## Transform a colormap or image from red-green-blue (RGB) space to
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
23 ## hue-saturation-value (HSV) space.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
24 ##
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
25 ## A color in the RGB space consists of red, green, and blue intensities.
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
26 ##
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
27 ## A color in HSV space is represented by hue, saturation, and value
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
28 ## (brightness) levels. Value gives the amount of light in the color. Hue
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
29 ## describes the dominant wavelength. Saturation is the amount of hue mixed
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
30 ## into the color.
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
31 ## @seealso{hsv2rgb, rgb2ind, rgb2ntsc}
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
32 ## @end deftypefn
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
33
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
34 ## Author: Kai Habel <kai.habel@gmx.de>
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
35 ## Adapted-by: jwe
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
36
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
37 function hsv_map = rgb2hsv (rgb)
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
38
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
39 if (nargin != 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
40 print_usage ();
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
41 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
42
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
43 cls = class (rgb);
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
44 if (! any (strcmp (cls, {"uint8", "uint16", "single", "double"})))
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
45 error ("rgb2hsv: invalid data type '%s'", 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
46 elseif (isfloat (rgb) && (any (rgb(:) < 0) || any (rgb(:) > 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
47 error ("rgb2hsv: floating point images may only contain values between 0 and 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
48 endif
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
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
50 ## If we have an image convert it into a color 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
51 if (isreal (rgb) && ndims (rgb) == 3)
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
52 is_image = true;
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
53 sz = size (rgb);
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
54 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
55 ## Convert to a double image.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
56 if (isinteger (rgb))
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
57 low = double (intmin (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
58 high = double (intmax (cls));
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
59 rgb = (double (rgb) - low) / (high - low);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
60 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
61 else
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
62 is_image = false;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
63 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
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
65 if (! ismatrix (rgb) || columns (rgb) != 3 || issparse (rgb))
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
66 error ("rgb2hsv: input must be a matrix of size Nx3 or MxNx3");
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
67 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
68
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
69 ## get the max and min for each row
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
70 s = min (rgb, [], 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
71 v = max (rgb, [], 2);
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
72
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
73 ## set hue to zero for undefined values (gray has no hue)
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 h = zeros (rows (rgb), 1);
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
75 notgray = (s != v);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
76
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
77 ## blue hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
78 idx = (v == rgb(:,3) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
79 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
80 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx));
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
81 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
82
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
83 ## green hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
84 idx = (v == rgb(:,2) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
85 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
86 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx));
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
87 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
88
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
89 ## red hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
90 idx = (v == rgb(:,1) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
91 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
92 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx));
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
93 endif
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
94 h(h < 0) += 1; # correct for negative red
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
95
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
96 ## set the saturation
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
97 s(! notgray) = 0;
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
98 s(notgray) = 1 - s(notgray) ./ v(notgray);
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
99
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
100 hsv_map = [h, s, v];
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
101
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
102 ## FIXME: rgb2hsv does not preserve class of image.
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
103 ## Should it also convert back to uint8, uint16 for integer images?
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
104 ## If input was an image, convert it back into one.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
105 if (is_image)
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
106 hsv_map = reshape (hsv_map, sz);
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
107 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
108
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
109 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
110
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
111
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
112 ## Test pure colors and gray
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
113 %!assert (rgb2hsv ([1 0 0]), [0 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
114 %!assert (rgb2hsv ([0 1 0]), [1/3 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
115 %!assert (rgb2hsv ([0 0 1]), [2/3 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
116 %!assert (rgb2hsv ([0.5 0.5 0.5]), [0 0 0.5])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
117
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
118 %!test
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
119 %! rgb_map = rand (64, 3);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
120 %! assert (hsv2rgb (rgb2hsv (rgb_map)), rgb_map, 1e-6);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
121
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
122 %!test
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
123 %! rgb_img = rand (64, 64, 3);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
124 %! assert (hsv2rgb (rgb2hsv (rgb_img)), rgb_img, 1e-6);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
125
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
126 ## 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
127 %!error rgb2hsv ()
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14260
diff changeset
128 %!error rgb2hsv (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
129 %!error <invalid data type 'cell'> rgb2hsv ({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
130 %!error <must be a matrix of size Nx3> rgb2hsv (ones (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
131