Mercurial > hg > octave-nkf
annotate scripts/image/hsv2rgb.m @ 20296:23fb65b45d8c
do not call custom editor at startup and when debugging (bug #44701)
* file-editor.cc (call_custom_editor): return with true but without opening
a file;
(empty_script): do not open an empty script in the cutom editor
at startup
author | Torsten <ttl@justmail.de> |
---|---|
date | Fri, 17 Apr 2015 19:55:24 +0200 |
parents | 9fc020886ae9 |
children | 84ca63c8a038 |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1999-2015 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); |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
49 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:
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] |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
15714
diff
changeset
|
78 ## hsv_map(hsv_map < 0) = 0; |
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
15714
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 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
99 ## FIXME: hsv2rgb does not preserve class of image. |
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15683
diff
changeset
|
100 ## Should it also convert back to uint8, uint16 for integer images? |
7375 | 101 ## If input was an image, convert it back into one. |
102 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
|
103 rgb_map = reshape (rgb_map, sz); |
7375 | 104 endif |
105 | |
3803 | 106 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
|
107 |
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 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
109 ## Test pure colors |
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
|
110 %!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
|
111 %!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
|
112 %!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
|
113 %!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
|
114 |
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 %!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
|
116 %! 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
|
117 %! 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
|
118 |
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 %!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
|
120 %! 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
|
121 %! 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
|
122 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
123 ## Test input validation |
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
|
124 %!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
|
125 %!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
|
126 %!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
|
127 %!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
|
128 %!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
|
129 |