changelog shortlog tags changeset files revisions annotate raw

scripts/image/rgb2hsv.m

changeset 10289: 4b124317dc38
parent:4fbfce35012a
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (30 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008
2## Kai Habel
3##
4## This file is part of Octave.
5##
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {@var{hsv_map} =} rgb2hsv (@var{rgb_map})
22## Transform a colormap or image from the rgb space to the hsv space.
23##
24## A color n the RGB space consists of the red, green and blue intensities.
25##
26## In the HSV space each color is represented by their hue, saturation
27## and value (brightness). Value gives the amount of light in the color.
28## Hue describes the dominant wavelength.
29## Saturation is the amount of Hue mixed into the color.
30## @seealso{hsv2rgb}
31## @end deftypefn
32
33## Author: Kai Habel <kai.habel@gmx.de>
34## Adapted-by: jwe
35
36function hsval = rgb2hsv (rgb)
37
38 if (nargin != 1)
39 print_usage ();
40 endif
41
42 ## If we have an image convert it into a color map.
43 if (ismatrix (rgb) && ndims (rgb) == 3)
44 is_image = true;
45 Sz = size (rgb);
46 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
47 ## Convert to a double image.
48 if (isinteger (rgb))
49 C = class (rgb);
50 low = double (intmin (C));
51 high = double (intmax (C));
52 rgb = (double (rgb) - low) / (high - low);
53 endif
54 else
55 is_image = false;
56 endif
57
58 if (! ismatrix (rgb) || columns (rgb) != 3)
59 error ("rgb2hsv: argument must be a matrix of size n x 3");
60 endif
61
62 ## get the max and min
63 s = min (rgb')';
64 v = max (rgb')';
65
66 ## set hue to zero for undefined values (gray has no hue)
67 h = zeros (size (v));
68 notgray = (s != v);
69
70 ## blue hue
71 idx = (v == rgb(:,3) & notgray);
72 if (any (idx))
73 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx));
74 endif
75
76 ## green hue
77 idx = (v == rgb(:,2) & notgray);
78 if (any (idx))
79 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx));
80 endif
81
82 ## red hue
83 idx = (v == rgb(:,1) & notgray);
84 if (any (idx))
85 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx));
86 endif
87
88 ## correct for negative red
89 idx = (h < 0);
90 h(idx) = 1+h(idx);
91
92 ## set the saturation
93 s(! notgray) = 0;
94 s(notgray) = 1 - s(notgray) ./ v(notgray);
95
96 hsval = [h, s, v];
97
98 ## If input was an image, convert it back into one.
99 if (is_image)
100 hsval = reshape (hsval, Sz);
101 endif
102
103endfunction