Mercurial > hg > octave-lyh
annotate scripts/image/rgb2hsv.m @ 17535:c12c688a35ed default tip lyh
Fix warnings
author | LYH <lyh.kernel@gmail.com> |
---|---|
date | Fri, 27 Sep 2013 17:43:27 +0800 |
parents | b1cd65881592 |
children |
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 ## |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
27 ## A color in HSV space is represented by hue, saturation, and value |
14260
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 | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
43 cls = class (rgb); |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
44 if (! any (strcmp (cls, {"uint8", "uint16", "single", "double"}))) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
45 error ("rgb2hsv: invalid data type '%s'", cls); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
46 elseif (isfloat (rgb) && (any (rgb(:) < 0) || any (rgb(:) > 1))) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
47 error ("rgb2hsv: floating point images may only contain values between 0 and 1"); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
48 endif |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
49 |
7375 | 50 ## If we have an image convert it into a color map. |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
51 if (isreal (rgb) && ndims (rgb) == 3) |
7375 | 52 is_image = true; |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
53 sz = size (rgb); |
7375 | 54 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)]; |
55 ## Convert to a double image. | |
56 if (isinteger (rgb)) | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
57 low = double (intmin (cls)); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
58 high = double (intmax (cls)); |
7375 | 59 rgb = (double (rgb) - low) / (high - low); |
60 endif | |
61 else | |
62 is_image = false; | |
63 endif | |
64 | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
65 if (! ismatrix (rgb) || columns (rgb) != 3 || issparse (rgb)) |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
66 error ("rgb2hsv: input must be a matrix of size Nx3 or MxNx3"); |
3803 | 67 endif |
68 | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
69 ## get the max and min for each row |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
70 s = min (rgb, [], 2); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
71 v = max (rgb, [], 2); |
3803 | 72 |
3904 | 73 ## set hue to zero for undefined values (gray has no hue) |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
74 h = zeros (rows (rgb), 1); |
3904 | 75 notgray = (s != v); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
76 |
3904 | 77 ## blue hue |
78 idx = (v == rgb(:,3) & notgray); | |
79 if (any (idx)) | |
80 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx)); | |
81 endif | |
3803 | 82 |
3904 | 83 ## green hue |
84 idx = (v == rgb(:,2) & notgray); | |
85 if (any (idx)) | |
86 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx)); | |
87 endif | |
3803 | 88 |
3904 | 89 ## red hue |
90 idx = (v == rgb(:,1) & notgray); | |
91 if (any (idx)) | |
92 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx)); | |
93 endif | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
94 h(h < 0) += 1; # correct for negative red |
3803 | 95 |
3904 | 96 ## set the saturation |
97 s(! notgray) = 0; | |
98 s(notgray) = 1 - s(notgray) ./ v(notgray); | |
3803 | 99 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
100 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
|
101 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
102 ## FIXME: rgb2hsv does not preserve class of image. |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
103 ## Should it also convert back to uint8, uint16 for integer images? |
7375 | 104 ## If input was an image, convert it back into one. |
105 if (is_image) | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
106 hsv_map = reshape (hsv_map, sz); |
7375 | 107 endif |
3803 | 108 |
109 endfunction | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
110 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
111 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
112 %% Test pure colors and gray |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
113 %!assert (rgb2hsv ([1 0 0]), [0 1 1]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
114 %!assert (rgb2hsv ([0 1 0]), [1/3 1 1]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
115 %!assert (rgb2hsv ([0 0 1]), [2/3 1 1]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
116 %!assert (rgb2hsv ([0.5 0.5 0.5]), [0 0 0.5]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
117 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
118 %!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
119 %! rgb_map = rand (64, 3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
120 %! assert (hsv2rgb (rgb2hsv (rgb_map)), rgb_map, 1e-6); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
121 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
122 %!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
123 %! rgb_img = rand (64, 64, 3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
124 %! assert (hsv2rgb (rgb2hsv (rgb_img)), rgb_img, 1e-6); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
125 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
126 %% Test input validation |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
127 %!error rgb2hsv () |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
128 %!error rgb2hsv (1,2) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
129 %!error <invalid data type 'cell'> rgb2hsv ({1}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
130 %!error <must be a matrix of size Nx3> rgb2hsv (ones (2,2)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
131 |