annotate scripts/image/rgb2hsv.m @ 3904:6b00ac653c0f

[project @ 2002-04-24 04:56:10 by jwe]
author jwe
date Wed, 24 Apr 2002 04:56:11 +0000
parents 63c75bc3db82
children 22bd65326ec1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 1999, 2000 Kai Habel
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
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
8 ## any later version.
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
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
18 ## 02111-1307, USA.
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 -*-
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {} @var{hsv_map} = rgb2hsv (@var{rgb_map})
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
22 ## Transform a colormap from the rgb space to the hsv space.
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.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
28 ## Hue describes the dominant wavelegth.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
29 ## Saturation is the amount of Hue mixed into the color.
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
30 ## @end deftypefn
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
31 ## @seealso{hsv2rgb}
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)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
39 usage ("hsv_map = rgb2hsv (rgb_map)");
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
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
42 if (! is_matrix (rgb) || columns (rgb) != 3)
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
43 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
44 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
45
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
46 ## get the max and min
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
47 s = min (rgb')';
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
48 v = max (rgb')';
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
49
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
50 ## 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
51 h = zeros (size (v));
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
52 notgray = (s != v);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
53
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
54 ## blue hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
55 idx = (v == rgb(:,3) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
56 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
57 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
58 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
59
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
60 ## green hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
61 idx = (v == rgb(:,2) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
62 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
63 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
64 endif
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 ## red hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
67 idx = (v == rgb(:,1) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
68 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
69 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
70 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
71
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
72 ## correct for negative red
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
73 idx = (h < 0);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
74 h(idx) = 1+h(idx);
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 ## set the saturation
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
77 s(! notgray) = 0;
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
78 s(notgray) = 1 - s(notgray) ./ v(notgray);
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
79
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
80 hsval = [h, s, v];
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
81
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
82 endfunction