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