Mercurial > octave-dspies
annotate 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 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
1 ## Copyright (C) 1994-2012 John W. Eaton |
2313 | 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. | |
2313 | 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/>. | |
559 | 18 |
3381 | 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} =} ntsc2rgb (@var{yiq_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} =} ntsc2rgb (@var{yiq_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 luminance-chrominance (NTSC) space to |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
23 ## red-green-blue (RGB) color space. |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
24 ## |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
25 ## Implementation Note: |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
26 ## The conversion matrix is chosen to be the inverse of the |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
27 ## matrix used for rgb2ntsc such that |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
28 ## |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
29 ## @example |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
30 ## x == ntsc2rgb (rgb2ntsc (x)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
31 ## @end example |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
32 ## |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
33 ## @sc{matlab} uses a slightly different matrix where rounding |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
34 ## means the equality above does not hold. |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
35 ## @seealso{rgb2ntsc, hsv2rgb, ind2rgb} |
3373 | 36 ## @end deftypefn |
1024 | 37 |
3202 | 38 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 39 ## Created: July 1994 |
40 ## Adapted-By: jwe | |
1024 | 41 |
2312 | 42 function rgb = ntsc2rgb (yiq) |
1024 | 43 |
44 if (nargin != 1) | |
6046 | 45 print_usage (); |
1024 | 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:
14260
diff
changeset
|
48 cls = class (yiq); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
49 if (! any (isa (yiq, {"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:
14260
diff
changeset
|
50 error ("ntsc2rgb: 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:
14260
diff
changeset
|
51 endif |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
52 |
7375 | 53 ## If we have an image convert it into a color map. |
54 if (ismatrix (yiq) && ndims (yiq) == 3) | |
55 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:
14260
diff
changeset
|
56 sz = size (yiq); |
7375 | 57 yiq = [yiq(:,:,1)(:), yiq(:,:,2)(:), yiq(:,:,3)(:)]; |
58 ## Convert to a double image. | |
59 if (isinteger (yiq)) | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
60 cls = class (yiq); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
61 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:
14260
diff
changeset
|
62 high = double (intmax (cls)); |
7375 | 63 yiq = (double (yiq) - low) / (high - low); |
64 endif | |
65 else | |
66 is_image = false; | |
67 endif | |
68 | |
69 if (! ismatrix (yiq) || columns (yiq) != 3) | |
70 error ("ntsc2rgb: argument must be a matrix of size Nx3 or NxMx3"); | |
71 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
72 |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
73 ## Conversion matrix constructed from 'inv (rgb2ntsc matrix)'. See |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
74 ## programming notes in rgb2ntsc.m. Note: Matlab matrix for inverse |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
75 ## is slightly different. We prefer this matrix so that |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
76 ## x == ntsc2rgb (rgb2ntsc (x)) rather than maintaining strict compatibility |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
77 ## with Matlab. |
1024 | 78 trans = [ 1.0, 1.0, 1.0; |
79 0.95617, -0.27269, -1.10374; | |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
80 0.62143, -0.64681, 1.70062 ]; |
559 | 81 |
82 rgb = yiq * trans; | |
83 | |
7375 | 84 ## If input was an image, convert it back into one. |
85 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:
14260
diff
changeset
|
86 rgb = reshape (rgb, sz); |
7375 | 87 endif |
88 | |
559 | 89 endfunction |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
90 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
91 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
92 %!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
93 %! rgb_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:
14260
diff
changeset
|
94 %! assert (ntsc2rgb (rgb2ntsc (rgb_map)), rgb_map, 1e-3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
95 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
96 %!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
97 %! rgb_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:
14260
diff
changeset
|
98 %! assert (ntsc2rgb (rgb2ntsc (rgb_img)), rgb_img, 1e-3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
99 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
100 %%!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
101 %%! ntsc_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:
14260
diff
changeset
|
102 %%! assert (rgb2ntsc (ntsc2rgb (ntsc_map)), ntsc_map, 1e-3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
103 % |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
104 %%!test |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
105 %%! ntsc_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:
14260
diff
changeset
|
106 %%! assert (rgb2ntsc (ntsc2rgb (ntsc_img)), ntsc_img, 1e-3); |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
107 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
108 %% Test input validation |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
109 %!error ntsc2rgb () |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
110 %!error ntsc2rgb (1,2) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
111 %!error <invalid data type 'cell'> ntsc2rgb ({1}) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
112 %!error <must be a matrix of size Nx3 or NxMx3> ntsc2rgb (ones (2,2)) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
113 |