annotate inst/mat2gray.m @ 541:a1c3dcfb7dab

mat2gray: more complete documentation
author carandraug
date Tue, 13 Mar 2012 18:25:56 +0000
parents 5a6afea737df
children 5b1405660aa2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
494
fdfaaf68dab0 mat2gray: fixes on input checking
carandraug
parents: 321
diff changeset
1 ## Copyright (C) 1999,2000 Kai Habel <kai.habel@gmx.de>
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
2 ## Copyright (C) 2011, 2012 Carnë Draug <carandraug+dev@gmail.com>
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
3 ##
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
4 ## This program is free software; you can redistribute it and/or modify it under
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
5 ## the terms of the GNU General Public License as published by the Free Software
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
6 ## Foundation; either version 3 of the License, or (at your option) any later
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
7 ## version.
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
8 ##
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
12 ## details.
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
13 ##
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
14 ## You should have received a copy of the GNU General Public License along with
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
16
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
17 ## -*- texinfo -*-
541
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
18 ## @deftypefn {Function File} {@var{I} =} mat2gray (@var{M})
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
19 ## @deftypefnx {Function File} {@var{I} =} mat2gray (@var{M}, [@var{min} @var{max}])
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
20 ## Convert a matrix to an intensity image.
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
21 ##
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
22 ## The returned matrix @var{I} is a grayscale image, of double class and in the
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
23 ## range of values [0, 1]. The optional arguments @var{min} and @var{max} will
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
24 ## set the limits of the conversion; values in @var{M} below @var{min} and
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
25 ## above @var{max} will be set to 0 and 1 on @var{I} respectively.
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
26 ##
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
27 ## @var{max} and @var{min} default to the maximum and minimum values of @var{M}.
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
28 ##
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
29 ## If @var{min} is larger than @var{max}, the `inverse' will be returned. Values
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
30 ## in @var{M} above @var{max} will be set to 0 while the ones below @var{min}
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
31 ## will be set to 1.
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
32 ##
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
33 ## @strong{Caution:} For compatibility with @sc{matlab}, Octave's mat2gray
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
34 ## will return a matrix of ones if @var{min} and @var{max} are equal, even for
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
35 ## values below @var{min}.
a1c3dcfb7dab mat2gray: more complete documentation
carandraug
parents: 540
diff changeset
36 ##
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
37 ## @seealso{gray2ind, ind2gray, rgb2gray, im2double, im2uin16, im2uint8, im2int16}
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
38 ## @end deftypefn
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
39
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
40 function out = mat2gray (in, scale)
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
41
494
fdfaaf68dab0 mat2gray: fixes on input checking
carandraug
parents: 321
diff changeset
42 if (nargin < 1 || nargin > 2)
fdfaaf68dab0 mat2gray: fixes on input checking
carandraug
parents: 321
diff changeset
43 print_usage;
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
44 elseif (!ismatrix (in) || ischar(in))
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
45 error ("mat2gray: first argument must be a matrix");
494
fdfaaf68dab0 mat2gray: fixes on input checking
carandraug
parents: 321
diff changeset
46 elseif (nargin == 2 && (!isvector (scale) || numel (scale) != 2))
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
47 error ("mat2gray: second argument must be a vector with 2 elements");
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
48 endif
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
49
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
50 if (nargin == 1)
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
51 out_min = min (in(:));
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
52 out_max = max (in(:));
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
53 else
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
54 ## see more at the end for the cases where max and min are swapped
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
55 out_min = min (scale (1), scale (2));
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
56 out_max = max (scale (1), scale (2));
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
57 endif
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
58
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
59 ## since max() and min() return a value of same class as input,
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
60 ## need to make this values double or the calculations later may fail
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
61 out_min = double (out_min);
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
62 out_max = double (out_max);
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
63
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
64 out = ones (size (in));
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
65 if (out_min == out_max)
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
66 ## if max and min are the same, matlab seems to simple return 1s, even
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
67 ## for values under the minimum/maximum. As such, we are done here
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
68 return
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
69 endif
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
70
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
71 out(in <= out_min) = 0;
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
72 ## no need to worry with values above or equal to out_max because
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
73 ## the output matrix was already generated with ones()
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
74
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
75 ## it's faster to get the index of values between max and min only once
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
76 ## than to have it calculated on both sides of the assignment
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
77 idx = (in > out_min & in < out_max);
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
78 out(idx) = (1/(out_max - out_min)) * (double(in(idx)) - out_min);
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
79
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
80 ## if the given min and max are in the inverse order...
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
81 if (nargin > 1 && scale(1) > scale (2))
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
82 ## matlab seems to allow setting the min higher than the max but not by
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
83 ## checking which one is actually correct. Seems to just invert it
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
84 out = abs (out - 1);
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
85 endif
186
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
86
13c6a9bdec24 Changed the structure to match the package system
hauberg
parents:
diff changeset
87 endfunction
540
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
88
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
89 %!assert(mat2gray([1 2 3]), [0 0.5 1]); # standard use
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
90 %!assert(mat2gray(repmat ([1 2; 3 3], [1 1 3])), repmat ([0 0.5; 1 1], [1 1 3])); # setting min and max
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
91 %!assert(mat2gray([1 2 3], [2 2]), [1 1 1]); # equal min and max
5a6afea737df mat2gray: big rewrite, function seems to not be working for several octave releases. Also should now be completely compatible with matlab
carandraug
parents: 494
diff changeset
92 %!assert(mat2gray([1 2 3], [3 1]), [1 0.5 0]); # max and min inverted