comparison scripts/image/ntsc2rgb.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 1f911333ed3d
children b1cd65881592
comparison
equal deleted inserted replaced
15682:48e3841a7510 15683:806ea52af230
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{rgb_map} =} ntsc2rgb (@var{yiq_map}) 20 ## @deftypefn {Function File} {@var{rgb_map} =} ntsc2rgb (@var{yiq_map})
21 ## @deftypefnx {Function File} {@var{rgb_img} =} ntsc2rgb (@var{yiq_img}) 21 ## @deftypefnx {Function File} {@var{rgb_img} =} ntsc2rgb (@var{yiq_img})
22 ## Transform a colormap or image from luminance-chrominance (NTSC) space to 22 ## Transform a colormap or image from luminance-chrominance (NTSC) space to
23 ## red-green-blue (RGB) space. 23 ## red-green-blue (RGB) color space.
24 ##
25 ## Implementation Note:
26 ## The conversion matrix is chosen to be the inverse of the
27 ## matrix used for rgb2ntsc such that
28 ##
29 ## @example
30 ## x == ntsc2rgb (rgb2ntsc (x))
31 ## @end example
32 ##
33 ## @sc{matlab} uses a slightly different matrix where rounding
34 ## means the equality above does not hold.
24 ## @seealso{rgb2ntsc, hsv2rgb, ind2rgb} 35 ## @seealso{rgb2ntsc, hsv2rgb, ind2rgb}
25 ## @end deftypefn 36 ## @end deftypefn
26 37
27 ## Author: Tony Richardson <arichard@stark.cc.oh.us> 38 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
28 ## Created: July 1994 39 ## Created: July 1994
32 43
33 if (nargin != 1) 44 if (nargin != 1)
34 print_usage (); 45 print_usage ();
35 endif 46 endif
36 47
48 cls = class (yiq);
49 if (! any (isa (yiq, {"uint8", "uint16", "single", "double"})))
50 error ("ntsc2rgb: invalid data type '%s'", cls);
51 endif
52
37 ## If we have an image convert it into a color map. 53 ## If we have an image convert it into a color map.
38 if (ismatrix (yiq) && ndims (yiq) == 3) 54 if (ismatrix (yiq) && ndims (yiq) == 3)
39 is_image = true; 55 is_image = true;
40 Sz = size (yiq); 56 sz = size (yiq);
41 yiq = [yiq(:,:,1)(:), yiq(:,:,2)(:), yiq(:,:,3)(:)]; 57 yiq = [yiq(:,:,1)(:), yiq(:,:,2)(:), yiq(:,:,3)(:)];
42 ## Convert to a double image. 58 ## Convert to a double image.
43 if (isinteger (yiq)) 59 if (isinteger (yiq))
44 C = class (yiq); 60 cls = class (yiq);
45 low = double (intmin (C)); 61 low = double (intmin (cls));
46 high = double (intmax (C)); 62 high = double (intmax (cls));
47 yiq = (double (yiq) - low) / (high - low); 63 yiq = (double (yiq) - low) / (high - low);
48 endif 64 endif
49 else 65 else
50 is_image = false; 66 is_image = false;
51 endif 67 endif
52 68
53 if (! ismatrix (yiq) || columns (yiq) != 3) 69 if (! ismatrix (yiq) || columns (yiq) != 3)
54 error ("ntsc2rgb: argument must be a matrix of size Nx3 or NxMx3"); 70 error ("ntsc2rgb: argument must be a matrix of size Nx3 or NxMx3");
55 endif 71 endif
56 72
57 ## Convert data 73 ## Conversion matrix constructed from 'inv (rgb2ntsc matrix)'. See
74 ## programming notes in rgb2ntsc.m. Note: Matlab matrix for inverse
75 ## is slightly different. We prefer this matrix so that
76 ## x == ntsc2rgb (rgb2ntsc (x)) rather than maintaining strict compatibility
77 ## with Matlab.
58 trans = [ 1.0, 1.0, 1.0; 78 trans = [ 1.0, 1.0, 1.0;
59 0.95617, -0.27269, -1.10374; 79 0.95617, -0.27269, -1.10374;
60 0.62143, -0.64681, 1.70062 ]; 80 0.62143, -0.64681, 1.70062 ];
61 81
62 rgb = yiq * trans; 82 rgb = yiq * trans;
63 83
64 ## If input was an image, convert it back into one. 84 ## If input was an image, convert it back into one.
65 if (is_image) 85 if (is_image)
66 rgb = reshape (rgb, Sz); 86 rgb = reshape (rgb, sz);
67 endif 87 endif
68 88
69 endfunction 89 endfunction
90
91
92 %!test
93 %! rgb_map = rand (64, 3);
94 %! assert (ntsc2rgb (rgb2ntsc (rgb_map)), rgb_map, 1e-3);
95
96 %!test
97 %! rgb_img = rand (64, 64, 3);
98 %! assert (ntsc2rgb (rgb2ntsc (rgb_img)), rgb_img, 1e-3);
99
100 %%!test
101 %%! ntsc_map = rand (64, 3);
102 %%! assert (rgb2ntsc (ntsc2rgb (ntsc_map)), ntsc_map, 1e-3);
103 %
104 %%!test
105 %%! ntsc_img = rand (64, 64, 3);
106 %%! assert (rgb2ntsc (ntsc2rgb (ntsc_img)), ntsc_img, 1e-3);
107
108 %% Test input validation
109 %!error ntsc2rgb ()
110 %!error ntsc2rgb (1,2)
111 %!error <invalid data type 'cell'> ntsc2rgb ({1})
112 %!error <must be a matrix of size Nx3 or NxMx3> ntsc2rgb (ones (2,2))
113