Mercurial > octave-dspies
annotate scripts/image/hsv2rgb.m @ 15683:806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
* NEWS: Add note about overhaul of image scripts to support integer
classes.
* brighten.m: Add demo.
* colormap.m: Better input validation.
* contrast.m: Re-position window in demo.
* gray2ind.m: Redo docstring. Match variables in docstring to function
prototype. Better input validation. Return integer class outputs as
Matlab does. Add %!tests.
* hsv2rgb.m: Add to docstring. Better input validation. Redo algorithm
to get rido of obsolete matrix-fill methods like kron(). Add %!tests.
* image.m: Redo docstring. Match variables in docstring to function
prototype. Better input validation without using for loops.
* imagesc.m: Redo docstring. Match variables in docstring to function
prototype. Remove DEPRECATEDZOOM functionality. Add demos.
* imfinfo.m: Redo docstring.
* ind2gray.m: Redo docstring. Match variables in docstring to function
prototype. Redo algorithm to directly calculate luminance value rather
than getting it from rgb2ntsc. Add %!tests.
* ind2rgb.m: Redo docstring. Better input validation. Add some %!tests.
* ntsc2rgb.m: Redo docstring. Better input validation. Add %!tests.
* rgb2hsv.m: Better input validation. Add %!tests.
* rgb2ind.m: Better input validation. Code algorithm in cleaner method
for ease of understanding.
* rgb2ntsc.m: Redo docstring: Better input validation. Add some %!tests.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 27 Nov 2012 16:38:13 -0800 |
parents | 86854d032a37 |
children | b1cd65881592 |
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{rgb_map} =} hsv2rgb (@var{hsv_map}) |
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{rgb_img} =} hsv2rgb (@var{hsv_img}) |
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 hue-saturation-value (HSV) space to |
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
23 ## red-green-blue (RGB) space. |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
24 ## |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
25 ## A color in HSV space is represented by hue, saturation and value |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
26 ## (brightness) levels. Value gives the amount of light in the color. Hue |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
27 ## describes the dominant wavelength. Saturation is the amount of hue mixed |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
28 ## into the color. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
29 ## |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
30 ## A color in the RGB space consists of red, green, and blue intensities. |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
31 ## @seealso{rgb2hsv, ind2rgb, ntsc2rgb} |
3803 | 32 ## @end deftypefn |
33 | |
34 ## Author: Kai Habel <kai.habel@gmx.de> | |
35 ## Adapted-by: jwe | |
36 | |
37 function rgb_map = hsv2rgb (hsv_map) | |
38 | |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
39 ## Each color value x = (r,g,b) is calculated with |
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
40 ## x = (1-sat)*val+sat*val*f_x(hue) |
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
41 ## where f_x(hue) is a piecewise defined function for |
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
42 ## each color with f_r(hue-2/3) = f_g(hue) = f_b(hue-1/3). |
3803 | 43 |
44 if (nargin != 1) | |
6046 | 45 print_usage (); |
3803 | 46 endif |
47 | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
48 cls = class (hsv_map); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
49 if (! any (isa (hsv_map, {"uint8", "uint16", "single", "double"}))) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
50 error ("hsv2rgb: 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:
14552
diff
changeset
|
51 elseif (isfloat (hsv_map) && (any (hsv_map(:) < 0) || any (hsv_map(:) > 1))) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
52 error ("hsv2rgb: 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:
14552
diff
changeset
|
53 endif |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
54 |
7375 | 55 ## 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:
14552
diff
changeset
|
56 if (isreal (hsv_map) && ndims (hsv_map) == 3) |
7375 | 57 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:
14552
diff
changeset
|
58 sz = size (hsv_map); |
7375 | 59 hsv_map = [hsv_map(:,:,1)(:), hsv_map(:,:,2)(:), hsv_map(:,:,3)(:)]; |
60 ## Convert to a double image. | |
61 if (isinteger (hsv_map)) | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
62 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:
14552
diff
changeset
|
63 high = double (intmax (cls)); |
7375 | 64 hsv_map = (double (hsv_map) - low) / (high - low); |
65 endif | |
66 else | |
67 is_image = false; | |
68 endif | |
69 | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
70 if (! isreal (hsv_map) || columns (hsv_map) != 3 || issparse (hsv_map)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
71 error ("hsv2rgb: input must be a matrix of size Nx3 or MxNx3"); |
3803 | 72 endif |
73 | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
74 ## FIXME: Currently input is validated and an error results if values |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
75 ## are outside range [0, 1]. We could also simply allow those values |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
76 ## and re-instate this code to produce saturating semantics. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
77 ## Trim map to range [0, 1] |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
78 #hsv_map(hsv_map < 0) = 0; |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
79 #hsv_map(hsv_map > 1) = 1; |
3803 | 80 |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
81 h = hsv_map(:,1); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
82 s = hsv_map(:,2); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
83 v = hsv_map(:,3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
84 ## Prefill rgb map with v*(1-s) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
85 rgb_map = repmat (v .* (1 - s), 1, 3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
86 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
87 ## red = hue-2/3 : green = hue : blue = hue-1/3 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
88 ## Apply modulo 1 for red and blue to keep within range [0, 1] |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
89 hue = [mod(h - 2/3, 1), h , mod(h - 1/3, 1)]; |
3803 | 90 |
91 ## factor s*v -> f | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
92 f = repmat (s .* v, 1, 3); |
3803 | 93 |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
94 ## add s*v*hue-function to rgb map |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
95 rgb_map += f .* (6 * (hue < 1/6) .* hue |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
96 + (hue >= 1/6 & hue < 1/2) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
97 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue)); |
3803 | 98 |
7375 | 99 ## If input was an image, convert it back into one. |
100 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:
14552
diff
changeset
|
101 rgb_map = reshape (rgb_map, sz); |
7375 | 102 endif |
103 | |
3803 | 104 endfunction |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
105 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
106 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
107 %% Test pure colors |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
108 %!assert (hsv2rgb ([0 1 1]), [1 0 0]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
109 %!assert (hsv2rgb ([1 1 1]), [1 0 0]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
110 %!assert (hsv2rgb ([1/3 1 1]), [0 1 0]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
111 %!assert (hsv2rgb ([2/3 1 1]), [0 0 1]) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
112 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
113 %!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
114 %! hsv_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:
14552
diff
changeset
|
115 %! assert (rgb2hsv (hsv2rgb (hsv_map)), hsv_map, 1e-6); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
116 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
117 %!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
118 %! hsv_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:
14552
diff
changeset
|
119 %! assert (rgb2hsv (hsv2rgb (hsv_img)), hsv_img, 1e-6); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
120 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
121 %% Test input validation |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
122 %!error hsv2rgb () |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
123 %!error hsv2rgb (1,2) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
124 %!error <invalid data type> hsv2rgb ({1}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
125 %!error <must be a matrix of size Nx3> hsv2rgb (ones (2,2)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
126 %!error <must be a matrix of size Nx3> hsv2rgb (sparse (ones(1,3))) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14552
diff
changeset
|
127 |