annotate scripts/image/rgb2hsv.m @ 8920:eb63fbe60fab

update copyright notices
author John W. Eaton <jwe@octave.org>
date Sat, 07 Mar 2009 10:41:27 -0500
parents 4fbfce35012a
children 3140cb7a05a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 7375
diff changeset
1 ## Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Kai Habel
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
3 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
5 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
6 ## 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
7 ## 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
8 ## 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
9 ## your option) any later version.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
10 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
15 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
16 ## 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
17 ## 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
18 ## <http://www.gnu.org/licenses/>.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
19
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6046
diff changeset
21 ## @deftypefn {Function File} {@var{hsv_map} =} rgb2hsv (@var{rgb_map})
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
22 ## Transform a colormap or image from the rgb space to the hsv space.
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
23 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
24 ## A color n the RGB space consists of the red, green and blue intensities.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
25 ##
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
26 ## In the HSV space each color is represented by their hue, saturation
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
27 ## and value (brightness). Value gives the amount of light in the color.
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6547
diff changeset
28 ## Hue describes the dominant wavelength.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
29 ## Saturation is the amount of Hue mixed into the color.
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
30 ## @seealso{hsv2rgb}
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
31 ## @end deftypefn
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
32
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
33 ## Author: Kai Habel <kai.habel@gmx.de>
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
34 ## Adapted-by: jwe
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
35
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
36 function hsval = rgb2hsv (rgb)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
37
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
38 if (nargin != 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
39 print_usage ();
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
40 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
41
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
42 ## If we have an image convert it into a color map.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
43 if (ismatrix (rgb) && ndims (rgb) == 3)
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
44 is_image = true;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
45 Sz = size (rgb);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
46 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
47 ## Convert to a double image.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
48 if (isinteger (rgb))
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
49 C = class (rgb);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
50 low = double (intmin (C));
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
51 high = double (intmax (C));
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
52 rgb = (double (rgb) - low) / (high - low);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
53 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
54 else
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
55 is_image = false;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
56 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
57
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3904
diff changeset
58 if (! ismatrix (rgb) || columns (rgb) != 3)
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
59 error ("rgb2hsv: argument must be a matrix of size n x 3");
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
60 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
61
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
62 ## get the max and min
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
63 s = min (rgb')';
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
64 v = max (rgb')';
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
65
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
66 ## set hue to zero for undefined values (gray has no hue)
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
67 h = zeros (size (v));
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
68 notgray = (s != v);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
69
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
70 ## blue hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
71 idx = (v == rgb(:,3) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
72 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
73 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
74 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
75
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
76 ## green hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
77 idx = (v == rgb(:,2) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
78 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
79 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
80 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
81
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
82 ## red hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
83 idx = (v == rgb(:,1) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
84 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
85 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
86 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
87
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
88 ## correct for negative red
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
89 idx = (h < 0);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
90 h(idx) = 1+h(idx);
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
91
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
92 ## set the saturation
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
93 s(! notgray) = 0;
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
94 s(notgray) = 1 - s(notgray) ./ v(notgray);
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 hsval = [h, s, v];
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
97
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
98 ## 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
99 if (is_image)
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
100 hsval = reshape (hsval, Sz);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
101 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
102
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
103 endfunction