annotate scripts/image/rgb2hsv.m @ 14278:fa894f89b18f

convhull.m: Ensure column (not row) vectors
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sun, 29 Jan 2012 00:52:19 -0500
parents 1f911333ed3d
children 806ea52af230
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
1 ## Copyright (C) 1999-2012 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 ##
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
27 ## A color in HSV space is represented by hue, saturation and value
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
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
43 ## 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
44 if (ismatrix (rgb) && ndims (rgb) == 3)
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
45 is_image = true;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
46 Sz = size (rgb);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
47 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
48 ## Convert to a double image.
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
49 if (isinteger (rgb))
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
50 C = class (rgb);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
51 low = double (intmin (C));
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
52 high = double (intmax (C));
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
53 rgb = (double (rgb) - low) / (high - low);
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
54 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
55 else
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
56 is_image = false;
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
57 endif
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
58
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3904
diff changeset
59 if (! ismatrix (rgb) || columns (rgb) != 3)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
60 error ("rgb2hsv: RGB_MAP must be a matrix of size n x 3");
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
61 endif
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
62
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
63 ## get the max and min
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
64 s = min (rgb')';
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
65 v = max (rgb')';
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
66
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
67 ## 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
68 h = zeros (size (v));
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
69 notgray = (s != v);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
70
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
71 ## blue hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
72 idx = (v == rgb(:,3) & notgray);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
73 if (any (idx))
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
74 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
75 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
76
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
77 ## green hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
78 idx = (v == rgb(:,2) & 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) = 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
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 ## red hue
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
84 idx = (v == rgb(:,1) & 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/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
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 ## correct for negative red
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
90 idx = (h < 0);
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
91 h(idx) = 1+h(idx);
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
92
3904
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
93 ## set the saturation
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
94 s(! notgray) = 0;
6b00ac653c0f [project @ 2002-04-24 04:56:10 by jwe]
jwe
parents: 3803
diff changeset
95 s(notgray) = 1 - s(notgray) ./ v(notgray);
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
96
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
97 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
98
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
99 ## 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
100 if (is_image)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
101 hsv_map = reshape (hsv_map, Sz);
7375
4fbfce35012a [project @ 2008-01-15 01:04:06 by jwe]
jwe
parents: 7017
diff changeset
102 endif
3803
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
103
63c75bc3db82 [project @ 2001-02-28 08:24:40 by jwe]
jwe
parents:
diff changeset
104 endfunction