annotate scripts/image/hsv2rgb.m @ 18549:43cc202335dc stable release-3-8-1

Version 3.8.1 released. * configure.ac (OCTAVE_VERSION): Now 3.8.1. (OCTAVE_MINOR_VERSION): Now 1. (OCTAVE_RELEASE_DATE): Set to 2014-03-06. Update copyright date for startup message.
author John W. Eaton <jwe@octave.org>
date Thu, 06 Mar 2014 14:40:35 -0500
parents d63878346099
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: 17336
diff changeset
1 ## Copyright (C) 1999-2013 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: 6547
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: 6547
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: 6547
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: 6547
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{rgb_map} =} hsv2rgb (@var{hsv_map})
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
21 ## @deftypefnx {Function File} {@var{rgb_img} =} hsv2rgb (@var{hsv_img})
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 hue-saturation-value (HSV) space to
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
23 ## red-green-blue (RGB) space.
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
24 ##
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
25 ## A color in HSV space is represented by hue, saturation and value
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
26 ## (brightness) levels. Value gives the amount of light in the color. Hue
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
27 ## describes the dominant wavelength. Saturation is the amount of hue mixed
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
28 ## into the color.
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
29 ##
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
30 ## A color in the RGB space consists of red, green, and blue intensities.
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
31 ## @seealso{rgb2hsv, ind2rgb, ntsc2rgb}
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
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
37 function rgb_map = hsv2rgb (hsv_map)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
38
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
39 ## Each color value x = (r,g,b) is calculated with
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
40 ## x = (1-sat)*val+sat*val*f_x(hue)
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
41 ## where f_x(hue) is a piecewise defined function for
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
42 ## each color with f_r(hue-2/3) = f_g(hue) = f_b(hue-1/3).
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
43
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
44 if (nargin != 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
45 print_usage ();
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
46 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
47
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
48 cls = class (hsv_map);
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
49 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: 14552
diff changeset
50 error ("hsv2rgb: 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: 14552
diff changeset
51 elseif (isfloat (hsv_map) && (any (hsv_map(:) < 0) || any (hsv_map(:) > 1)))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
52 error ("hsv2rgb: 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: 14552
diff changeset
53 endif
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
54
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
55 ## 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: 14552
diff changeset
56 if (isreal (hsv_map) && ndims (hsv_map) == 3)
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
57 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: 14552
diff changeset
58 sz = size (hsv_map);
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
59 hsv_map = [hsv_map(:,:,1)(:), hsv_map(:,:,2)(:), hsv_map(:,:,3)(:)];
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
60 ## Convert to a double image.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
61 if (isinteger (hsv_map))
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
62 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: 14552
diff changeset
63 high = double (intmax (cls));
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
64 hsv_map = (double (hsv_map) - low) / (high - low);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
65 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
66 else
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
67 is_image = false;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
68 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
69
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
70 if (! isreal (hsv_map) || columns (hsv_map) != 3 || issparse (hsv_map))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
71 error ("hsv2rgb: 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
72 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
73
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
74 ## FIXME: Currently input is validated and an error results if values
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
75 ## are outside range [0, 1]. We could also simply allow those values
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
76 ## and re-instate this code to produce saturating semantics.
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
77 ## Trim map to range [0, 1]
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 15714
diff changeset
78 ## hsv_map(hsv_map < 0) = 0;
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 15714
diff changeset
79 ## hsv_map(hsv_map > 1) = 1;
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
80
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
81 h = hsv_map(:,1);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
82 s = hsv_map(:,2);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
83 v = hsv_map(:,3);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
84 ## Prefill rgb map with v*(1-s)
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
85 rgb_map = repmat (v .* (1 - s), 1, 3);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
86
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
87 ## red = hue-2/3 : green = hue : blue = hue-1/3
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
88 ## Apply modulo 1 for red and blue to keep within range [0, 1]
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
89 hue = [mod(h - 2/3, 1), h , mod(h - 1/3, 1)];
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
90
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
91 ## factor s*v -> f
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
92 f = repmat (s .* v, 1, 3);
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
93
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
94 ## add s*v*hue-function to rgb map
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
95 rgb_map += f .* (6 * (hue < 1/6) .* hue
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
96 + (hue >= 1/6 & hue < 1/2)
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
97 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue));
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
98
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
99 ## FIXME: hsv2rgb does not preserve class of image.
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15683
diff changeset
100 ## 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
101 ## 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
102 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: 14552
diff changeset
103 rgb_map = reshape (rgb_map, sz);
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
104 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
105
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
106 endfunction
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
107
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
108
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
109 %% Test pure colors
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
110 %!assert (hsv2rgb ([0 1 1]), [1 0 0])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
111 %!assert (hsv2rgb ([1 1 1]), [1 0 0])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
112 %!assert (hsv2rgb ([1/3 1 1]), [0 1 0])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
113 %!assert (hsv2rgb ([2/3 1 1]), [0 0 1])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
114
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
115 %!test
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
116 %! hsv_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: 14552
diff changeset
117 %! assert (rgb2hsv (hsv2rgb (hsv_map)), hsv_map, 1e-6);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
118
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
119 %!test
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
120 %! hsv_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: 14552
diff changeset
121 %! assert (rgb2hsv (hsv2rgb (hsv_img)), hsv_img, 1e-6);
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
122
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
123 %% Test input validation
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
124 %!error hsv2rgb ()
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
125 %!error hsv2rgb (1,2)
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
126 %!error <invalid data type> hsv2rgb ({1})
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
127 %!error <must be a matrix of size Nx3> hsv2rgb (ones (2,2))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
128 %!error <must be a matrix of size Nx3> hsv2rgb (sparse (ones(1,3)))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 14552
diff changeset
129