# HG changeset patch # User jwe # Date 1200359046 0 # Node ID 4fbfce35012a2d2deedbd58465e32ae8a259bb35 # Parent 4ff9611147ba96cf976bcc63c203dc837f8bf5f8 [project @ 2008-01-15 01:04:06 by jwe] diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,11 @@ +2008-01-14 Soren Hauberg + + * image/hsv2rgb.m, image/ntsc2rgb.m, image/rgb2hsv.m, + image/rgb2ntsc.m: Also accept images as input. + + * image/gray2ind.m: Handle image type other than double. + Improve error checking and documentation. + 2008-01-14 John W. Eaton * plot/__go_draw_axes__.m (get_fontname_and_size): Use strcmpi diff --git a/scripts/image/gray2ind.m b/scripts/image/gray2ind.m --- a/scripts/image/gray2ind.m +++ b/scripts/image/gray2ind.m @@ -18,24 +18,45 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {[@var{img}, @var{map}] =} gray2ind (@var{}) +## @deftypefn {Function File} {[@var{img}, @var{map}] =} gray2ind (@var{I}, @var{n}) ## Convert a gray scale intensity image to an Octave indexed image. +## The indexed image will consist of @var{n} different intensity values. If not +## given @var{n} will default to 64. ## @end deftypefn ## Author: Tony Richardson ## Created: July 1994 ## Adapted-By: jwe -function [X, map] = gray2ind (I, n) - +function [X, map] = gray2ind (I, n = 64) + ## Check input if (nargin < 1 || nargin > 2) print_usage (); - elseif (nargin == 1) - n = 64; + endif + C = class(I); + if (! ismatrix (I) || ndims (I) != 2) + error ("gray2ind: first input argument must be a gray scale image"); + endif + if (! isscalar (n) || n < 0) + error ("gray2ind: second input argument must be a positive integer"); + endif + ints = {"uint8", "uint16", "int8", "int16"}; + floats = {"double", "single"}; + if (! ismember (C, {ints{:}, floats{:}})) + error ("gray2ind: invalid data type '%s'", C); + endif + if (ismember (C, floats) && (min (I(:)) < 0 || max (I(:)) > 1)) + error ("gray2ind: floating point images may only contain values between 0 and 1"); endif + ## Convert data map = gray (n); - + ## If @var{I} is an integer matrix convert it to a double matrix with values in [0, 1] + if (ismember (C, ints)) + low = double (intmin (C)); + high = double (intmax (C)); + I = (double (I) - low) / (high - low); + endif X = round (I*(n-1)) + 1; endfunction diff --git a/scripts/image/hsv2rgb.m b/scripts/image/hsv2rgb.m --- a/scripts/image/hsv2rgb.m +++ b/scripts/image/hsv2rgb.m @@ -19,7 +19,7 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {@var{rgb_map} =} hsv2rgb (@var{hsv_map}) -## Transform a colormap from the hsv space to the rgb space. +## Transform a colormap or image from the hsv space to the rgb space. ## @seealso{rgb2hsv} ## @end deftypefn @@ -37,6 +37,22 @@ print_usage (); endif + ## If we have an image convert it into a color map. + if (ismatrix (hsv_map) && ndims (hsv_map) == 3) + is_image = true; + Sz = size (hsv_map); + hsv_map = [hsv_map(:,:,1)(:), hsv_map(:,:,2)(:), hsv_map(:,:,3)(:)]; + ## Convert to a double image. + if (isinteger (hsv_map)) + C = class (hsv_map); + low = double (intmin (C)); + high = double (intmax (C)); + hsv_map = (double (hsv_map) - low) / (high - low); + endif + else + is_image = false; + endif + if (! ismatrix (hsv_map) || columns (hsv_map) != 3) error ("hsv2rgb: argument must be a matrix of size nx3"); endif @@ -64,4 +80,9 @@ + (hue >= 1/6 & hue < 1/2) + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue)); + ## If input was an image, convert it back into one. + if (is_image) + rgb_map = reshape (rgb_map, Sz); + endif + endfunction diff --git a/scripts/image/ntsc2rgb.m b/scripts/image/ntsc2rgb.m --- a/scripts/image/ntsc2rgb.m +++ b/scripts/image/ntsc2rgb.m @@ -19,7 +19,8 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} ntsc2rgb (@var{yiq}) -## Image format conversion. +## Transform a colormap or image from NTSC to RGB. +## @seealso{rgb2ntsc} ## @end deftypefn ## Author: Tony Richardson @@ -32,10 +33,36 @@ print_usage (); endif + ## If we have an image convert it into a color map. + if (ismatrix (yiq) && ndims (yiq) == 3) + is_image = true; + Sz = size (yiq); + yiq = [yiq(:,:,1)(:), yiq(:,:,2)(:), yiq(:,:,3)(:)]; + ## Convert to a double image. + if (isinteger (yiq)) + C = class (yiq); + low = double (intmin (C)); + high = double (intmax (C)); + yiq = (double (yiq) - low) / (high - low); + endif + else + is_image = false; + endif + + if (! ismatrix (yiq) || columns (yiq) != 3) + error ("ntsc2rgb: argument must be a matrix of size Nx3 or NxMx3"); + endif + + ## Convert data trans = [ 1.0, 1.0, 1.0; 0.95617, -0.27269, -1.10374; 0.62143, -0.64681, 1.70062 ]; rgb = yiq * trans; + ## If input was an image, convert it back into one. + if (is_image) + rgb = reshape (rgb, Sz); + endif + endfunction diff --git a/scripts/image/rgb2hsv.m b/scripts/image/rgb2hsv.m --- a/scripts/image/rgb2hsv.m +++ b/scripts/image/rgb2hsv.m @@ -19,7 +19,7 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {@var{hsv_map} =} rgb2hsv (@var{rgb_map}) -## Transform a colormap from the rgb space to the hsv space. +## Transform a colormap or image from the rgb space to the hsv space. ## ## A color n the RGB space consists of the red, green and blue intensities. ## @@ -39,6 +39,22 @@ print_usage (); endif + ## If we have an image convert it into a color map. + if (ismatrix (rgb) && ndims (rgb) == 3) + is_image = true; + Sz = size (rgb); + rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)]; + ## Convert to a double image. + if (isinteger (rgb)) + C = class (rgb); + low = double (intmin (C)); + high = double (intmax (C)); + rgb = (double (rgb) - low) / (high - low); + endif + else + is_image = false; + endif + if (! ismatrix (rgb) || columns (rgb) != 3) error ("rgb2hsv: argument must be a matrix of size n x 3"); endif @@ -78,5 +94,10 @@ s(notgray) = 1 - s(notgray) ./ v(notgray); hsval = [h, s, v]; + + ## If input was an image, convert it back into one. + if (is_image) + hsval = reshape (hsval, Sz); + endif endfunction diff --git a/scripts/image/rgb2ntsc.m b/scripts/image/rgb2ntsc.m --- a/scripts/image/rgb2ntsc.m +++ b/scripts/image/rgb2ntsc.m @@ -19,7 +19,8 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} rgb2ntsc (@var{rgb}) -## Image format conversion. +## Transform a colormap or image from RGB to NTSC. +## @seealso{ntsc2rgb} ## @end deftypefn ## Author: Tony Richardson @@ -32,10 +33,36 @@ print_usage (); endif + ## If we have an image convert it into a color map. + if (ismatrix (rgb) && ndims (rgb) == 3) + is_image = true; + Sz = size (rgb); + rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)]; + ## Convert to a double image. + if (isinteger (rgb)) + C = class (rgb); + low = double (intmin (C)); + high = double (intmax (C)); + rgb = (double (rgb) - low) / (high - low); + endif + else + is_image = false; + endif + + if (! ismatrix (rgb) || columns (rgb) != 3) + error ("rgb2ntsc: argument must be a matrix of size Nx3 or NxMx3"); + endif + + ## Convert data trans = [ 0.299, 0.596, 0.211; 0.587, -0.274, -0.523; 0.114, -0.322, 0.312 ]; yiq = rgb * trans; + ## If input was an image, convert it back into one. + if (is_image) + yiq = reshape (yiq, Sz); + endif + endfunction