comparison scripts/image/rgb2hsv.m @ 15714:b1cd65881592

Clean up scripts in image directory. Use Octave coding conventions. Redo docstrings. Add %!tests. * brighten.m: Put input validation first. Use iscolormap to simplify input checking. * cmunique.m: Use faster method of validating input class. * colormap.m: Tweak docstring. Improve input validation. * contrast.m: Tweak docstring. Use cmap instead of map as variable name for clarity. * gray2ind.m: Wrap long lines. Use faster method of validating input class. Delete unreachable code for n>65536. * hsv2rgb.m: Use faster method of validating input class. * imwrite.m: Tweak FIXME notes. * ind2gray.m: Use correct caller name for ind2x. Update %!tests with new 2-input calling convention. * ind2rgb.m: Tweak docstring. Update %!tests with new 2-input calling convention. * iscolormap.m: Tweak docstring. Re-order validation tests. * ntsc2rgb.m: Use faster method of validating input class. Better input validation. Add %!tests. * private/ind2x.m: Use more descriptive variable names. * rgb2hsv.m: Tweak docstring. Use faster method of validating input class. * rgb2ind.m: Tweak docstring. Wrap long lines. * rgb2ntsc.m: Use faster method of validating input class. Improve input validation. Add %!tests. * rgbplot.m: Match variable names in docstring to those in function prototype.
author Rik <rik@octave.org>
date Sun, 02 Dec 2012 10:02:57 -0800
parents 806ea52af230
children d63878346099
comparison
equal deleted inserted replaced
15713:168e380c8f18 15714:b1cd65881592
22 ## Transform a colormap or image from red-green-blue (RGB) space to 22 ## Transform a colormap or image from red-green-blue (RGB) space to
23 ## hue-saturation-value (HSV) space. 23 ## hue-saturation-value (HSV) space.
24 ## 24 ##
25 ## A color in the RGB space consists of red, green, and blue intensities. 25 ## A color in the RGB space consists of red, green, and blue intensities.
26 ## 26 ##
27 ## A color in HSV space is represented by hue, saturation and value 27 ## A color in HSV space is represented by hue, saturation, and value
28 ## (brightness) levels. Value gives the amount of light in the color. Hue 28 ## (brightness) levels. Value gives the amount of light in the color. Hue
29 ## describes the dominant wavelength. Saturation is the amount of hue mixed 29 ## describes the dominant wavelength. Saturation is the amount of hue mixed
30 ## into the color. 30 ## into the color.
31 ## @seealso{hsv2rgb, rgb2ind, rgb2ntsc} 31 ## @seealso{hsv2rgb, rgb2ind, rgb2ntsc}
32 ## @end deftypefn 32 ## @end deftypefn
39 if (nargin != 1) 39 if (nargin != 1)
40 print_usage (); 40 print_usage ();
41 endif 41 endif
42 42
43 cls = class (rgb); 43 cls = class (rgb);
44 if (! any (isa (rgb, {"uint8", "uint16", "single", "double"}))) 44 if (! any (strcmp (cls, {"uint8", "uint16", "single", "double"})))
45 error ("rgb2hsv: invalid data type '%s'", cls); 45 error ("rgb2hsv: invalid data type '%s'", cls);
46 elseif (isfloat (rgb) && (any (rgb(:) < 0) || any (rgb(:) > 1))) 46 elseif (isfloat (rgb) && (any (rgb(:) < 0) || any (rgb(:) > 1)))
47 error ("rgb2hsv: floating point images may only contain values between 0 and 1"); 47 error ("rgb2hsv: floating point images may only contain values between 0 and 1");
48 endif 48 endif
49 49
52 is_image = true; 52 is_image = true;
53 sz = size (rgb); 53 sz = size (rgb);
54 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)]; 54 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
55 ## Convert to a double image. 55 ## Convert to a double image.
56 if (isinteger (rgb)) 56 if (isinteger (rgb))
57 cls = class (rgb);
58 low = double (intmin (cls)); 57 low = double (intmin (cls));
59 high = double (intmax (cls)); 58 high = double (intmax (cls));
60 rgb = (double (rgb) - low) / (high - low); 59 rgb = (double (rgb) - low) / (high - low);
61 endif 60 endif
62 else 61 else
63 is_image = false; 62 is_image = false;
64 endif 63 endif
65 64
66 if (! ismatrix (rgb) || columns (rgb) != 3 || issparse (rgb)) 65 if (! ismatrix (rgb) || columns (rgb) != 3 || issparse (rgb))
67 error ("rgb2hsv: input must be a matrix of size Nx3"); 66 error ("rgb2hsv: input must be a matrix of size Nx3 or MxNx3");
68 endif 67 endif
69 68
70 ## get the max and min for each row 69 ## get the max and min for each row
71 s = min (rgb, [], 2); 70 s = min (rgb, [], 2);
72 v = max (rgb, [], 2); 71 v = max (rgb, [], 2);
98 s(! notgray) = 0; 97 s(! notgray) = 0;
99 s(notgray) = 1 - s(notgray) ./ v(notgray); 98 s(notgray) = 1 - s(notgray) ./ v(notgray);
100 99
101 hsv_map = [h, s, v]; 100 hsv_map = [h, s, v];
102 101
102 ## FIXME: rgb2hsv does not preserve class of image.
103 ## Should it also convert back to uint8, uint16 for integer images?
103 ## If input was an image, convert it back into one. 104 ## If input was an image, convert it back into one.
104 if (is_image) 105 if (is_image)
105 hsv_map = reshape (hsv_map, sz); 106 hsv_map = reshape (hsv_map, sz);
106 endif 107 endif
107 108