Mercurial > hg > octave-nkf
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 |
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 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3803 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3803 | 18 |
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 | 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 | 32 ## @end deftypefn |
33 | |
34 ## Author: Kai Habel <kai.habel@gmx.de> | |
35 ## Adapted-by: jwe | |
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 | 38 |
39 if (nargin != 1) | |
6046 | 40 print_usage (); |
3803 | 41 endif |
42 | |
7375 | 43 ## If we have an image convert it into a color map. |
44 if (ismatrix (rgb) && ndims (rgb) == 3) | |
45 is_image = true; | |
46 Sz = size (rgb); | |
47 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)]; | |
48 ## Convert to a double image. | |
49 if (isinteger (rgb)) | |
50 C = class (rgb); | |
51 low = double (intmin (C)); | |
52 high = double (intmax (C)); | |
53 rgb = (double (rgb) - low) / (high - low); | |
54 endif | |
55 else | |
56 is_image = false; | |
57 endif | |
58 | |
4030 | 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 | 61 endif |
62 | |
3904 | 63 ## get the max and min |
64 s = min (rgb')'; | |
65 v = max (rgb')'; | |
3803 | 66 |
3904 | 67 ## set hue to zero for undefined values (gray has no hue) |
68 h = zeros (size (v)); | |
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 | 71 ## blue hue |
72 idx = (v == rgb(:,3) & notgray); | |
73 if (any (idx)) | |
74 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx)); | |
75 endif | |
3803 | 76 |
3904 | 77 ## green hue |
78 idx = (v == rgb(:,2) & notgray); | |
79 if (any (idx)) | |
80 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx)); | |
81 endif | |
3803 | 82 |
3904 | 83 ## red hue |
84 idx = (v == rgb(:,1) & notgray); | |
85 if (any (idx)) | |
86 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx)); | |
87 endif | |
3803 | 88 |
3904 | 89 ## correct for negative red |
90 idx = (h < 0); | |
91 h(idx) = 1+h(idx); | |
3803 | 92 |
3904 | 93 ## set the saturation |
94 s(! notgray) = 0; | |
95 s(notgray) = 1 - s(notgray) ./ v(notgray); | |
3803 | 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 | 99 ## If input was an image, convert it back into one. |
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 | 102 endif |
3803 | 103 |
104 endfunction |