annotate scripts/image/cmunique.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 4beb3a4bd440
children 20e9b56bbf2f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14896
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
1 ## Copyright (C) 2004 Josep Mones i Teixidor
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
2 ## Copyright (C) 2012 Rik Wehbring
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
3 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
4 ## This file is part of Octave.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
5 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
9 ## your option) any later version.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
10 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
14 ## General Public License for more details.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
15 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
18 ## <http://www.gnu.org/licenses/>.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
19
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
20 ## -*- texinfo -*-
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
21 ## @deftypefn {Function File} {[@var{Y}, @var{newmap}] =} cmunique (@var{X}, @var{map})
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
22 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] =} cmunique (@var{RGB})
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
23 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] =} cmunique (@var{I})
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
24 ## Convert an input image @var{X} to an ouput indexed image @var{Y} which uses
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
25 ## the smallest colormap possible @var{newmap}.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
26 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
27 ## When the input is an indexed image (@var{X} with colormap @var{map}) the
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
28 ## output is a colormap @var{newmap} from which any repeated rows have been
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
29 ## eliminated. The output image, @var{Y}, is the original input image with
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
30 ## the indices adjusted to match the new, possibly smaller, colormap.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
31 ##
15040
bc61fba0e9fd doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 14897
diff changeset
32 ## When the input is an RGB image (an @nospell{MxNx3} array), the output
bc61fba0e9fd doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 14897
diff changeset
33 ## colormap will contain one entry for every unique color in the original image.
14896
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
34 ## In the worst case the new map could have as many rows as the number of
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
35 ## pixels in the original image.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
36 ##
14897
8e2a6fc55787 doc: Use 'grayscale' rather than 'gray-scale' in documentation.
Rik <rik@octave.org>
parents: 14896
diff changeset
37 ## When the input is a grayscale image @var{I}, the output colormap will
14896
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
38 ## contain one entry for every unique intensity value in the original image.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
39 ## In the worst case the new map could have as many rows as the number of
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
40 ## pixels in the original image.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
41 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
42 ## Implementation Details:
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
43 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
44 ## @var{newmap} is always an Mx3 matrix, even if the input image is
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
45 ## an intensity grayscale image @var{I} (all three RGB planes are
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
46 ## assigned the same value).
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
47 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
48 ## The output image is of class uint8 if the size of the new colormap is
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
49 ## less than or equal to 256. Otherwise, the output image is of class double.
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
50 ##
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
51 ## @seealso{rgb2ind, gray2ind}
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
52 ## @end deftypefn
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
53
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
54
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
55 ## Author: Josep Mones i Teixidor <jmones@puntbarra.com>
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
56
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
57 function [Y, newmap] = cmunique (X, map)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
58
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
59 if (nargin < 1 || nargin > 2)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
60 print_usage ();
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
61 endif
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
62
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
63 cls = class (X);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
64 ## FIXME: Documentation accepts only 3 classes. Could easily add 'single'.
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15515
diff changeset
65 if (! any (strcmp (cls, {"uint8", "uint16", "double"})))
14896
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
66 error ("cmunique: X is of invalid data type '%s'", cls);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
67 endif
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
68
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
69 if (nargin == 2)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
70 ## (X, map) case
15515
4beb3a4bd440 rgbplot.m, cmpermute.m, cmunique.m, ind2rgb.m, imwrite.m: use core iscolormap function
Carnë Draug <carandraug+dev@gmail.com>
parents: 15040
diff changeset
71 if (! iscolormap (map))
14896
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
72 error ("cmunique: MAP must be a valid colormap");
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
73 endif
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
74 [newmap,i,j] = unique (map, "rows"); # calculate unique colormap
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
75 if (isa (X, "double"))
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
76 Y = j(X); # find new indices
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
77 else
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
78 Y = j(double (X) + 1); # find new indices
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
79 endif
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
80 else
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
81 switch (size (X,3))
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
82 case (1)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
83 ## I case
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
84 [newmap,i,j] = unique (X); # calculate unique colormap
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
85 newmap = repmat (newmap,1,3); # get a RGB colormap
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
86 Y = reshape (j, rows (X), columns (X)); # Y is j reshaped
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
87 case (3)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
88 ## RGB case
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
89 ## build a map with all values
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
90 map = [X(:,:,1)(:), X(:,:,2)(:), X(:,:,3)(:)];
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
91 [newmap,i,j] = unique (map, "rows"); # calculate unique colormap
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
92 Y = reshape (j, rows (X), columns (X)); # Y is j reshaped
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
93 otherwise
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
94 error ("cmunique: X is not a valid image");
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
95 endswitch
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
96
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
97 ## if image was uint8 or uint16 we have to convert newmap to [0,1] range
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
98 if (! isa (X, "double"))
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
99 newmap = double (newmap) / double (intmax (class (X)));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
100 endif
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
101 endif
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
102
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
103 if (rows (newmap) <= 256)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
104 ## convert Y to uint8 (0-based indices then)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
105 Y = uint8 (Y-1);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
106 endif
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
107
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
108 endfunction
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
109
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
110
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
111 %!demo
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
112 %! [Y, newmap] = cmunique ([1:4;5:8], [hot(4);hot(4)])
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
113 %! ## Both rows are equal since map maps colors to the same value
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
114 %! ## cmunique will give the same indices to both
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
115
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
116 ## Check that output is uint8 in short colormaps
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
117 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
118 %! [Y, newmap] = cmunique ([1:4;5:8], [hot(4);hot(4)]);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
119 %! assert (Y, uint8 ([0:3;0:3]));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
120 %! assert (newmap, hot (4));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
121
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
122 ## Check that output is double in bigger
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
123 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
124 %! [Y, newmap] = cmunique ([1:300;301:600], [hot(300);hot(300)]);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
125 %! assert (Y, [1:300;1:300]);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
126 %! assert (newmap, hot (300));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
127
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
128 ## Check boundary case 256
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
129 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
130 %! [Y, newmap] = cmunique ([1:256;257:512], [hot(256);hot(256)]);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
131 %! assert (Y, uint8 ([0:255;0:255]));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
132 %! assert (newmap, hot (256));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
133
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
134 ## Check boundary case 257
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
135 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
136 %! [Y, newmap] = cmunique ([1:257;258:514], [hot(257);hot(257)]);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
137 %! assert (Y, [1:257;1:257]);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
138 %! assert (newmap, hot (257));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
139
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
140 ## Random RGB image
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
141 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
142 %! RGB = rand (10,10,3);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
143 %! [Y, newmap] = cmunique (RGB);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
144 %! assert (RGB(:,:,1), newmap(:,1)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
145 %! assert (RGB(:,:,2), newmap(:,2)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
146 %! assert (RGB(:,:,3), newmap(:,3)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
147
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
148 ## Random uint8 RGB image
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
149 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
150 %! RGB = uint8 (rand (10,10,3)*255);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
151 %! RGBd = double (RGB) / 255;
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
152 %! [Y, newmap] = cmunique (RGB);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
153 %! assert (RGBd(:,:,1), newmap(:,1)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
154 %! assert (RGBd(:,:,2), newmap(:,2)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
155 %! assert (RGBd(:,:,3), newmap(:,3)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
156
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
157 ## Random uint16 RGB image
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
158 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
159 %! RGB = uint16 (rand (10,10,3)*65535);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
160 %! RGBd = double (RGB) / 65535;
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
161 %! [Y, newmap] = cmunique (RGB);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
162 %! assert (RGBd(:,:,1), newmap(:,1)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
163 %! assert (RGBd(:,:,2), newmap(:,2)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
164 %! assert (RGBd(:,:,3), newmap(:,3)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
165
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
166 ## Random I image
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
167 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
168 %! I = rand (10,10);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
169 %! [Y, newmap] = cmunique (I);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
170 %! assert (I, newmap(:,1)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
171 %! assert (I, newmap(:,2)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
172 %! assert (I, newmap(:,3)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
173
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
174 ## Random uint8 I image
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
175 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
176 %! I = uint8 (rand (10,10)*256);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
177 %! Id = double (I) / 255;
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
178 %! [Y, newmap] = cmunique (I);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
179 %! assert (Id, newmap(:,1)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
180 %! assert (Id, newmap(:,2)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
181 %! assert (Id, newmap(:,3)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
182
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
183 ## Random uint16 I image
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
184 %!test
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
185 %! I = uint16 (rand (10,10)*65535);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
186 %! Id = double (I) / 65535;
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
187 %! [Y,newmap] = cmunique (I);
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
188 %! assert (Id,newmap (:,1)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
189 %! assert (Id,newmap (:,2)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
190 %! assert (Id,newmap (:,3)(Y+1));
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
191
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
192 ## Test input validation
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
193 %!error cmpermute ()
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
194 %!error cmpermute (1,2,3)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
195 %!error <X is of invalid data type> cmunique (single (magic (16)))
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
196 %!error <MAP must be a valid colormap> cmunique (1, "a")
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
197 %!error <MAP must be a valid colormap> cmunique (1, i)
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
198 %!error <MAP must be a valid colormap> cmunique (1, ones (3,3,3))
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
199 %!error <MAP must be a valid colormap> cmunique (1, ones (3,2))
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
200 %!error <MAP must be a valid colormap> cmunique (1, [-1 1 1])
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
201 %!error <MAP must be a valid colormap> cmunique (1, [2 1 1])
0ba7be7fed1c Add new functions cmpermute(), cmunque().
Rik <rik@octave.org>
parents:
diff changeset
202