Mercurial > hg > octave-image
annotate inst/mat2gray.m @ 885:40269ff6760d
imcast: new function to convert image between arbitrary classes.
* imcast.m: new function acting as wrapper for im2double, im2single,
im2uint8, im2uint16, and im2int16.
* imcast.m, imnoise.m, imrotate.m: replace ugly hack using feval with
call to imcast.
* COPYING: specify license of new function.
* INDEX: add new function.
* NEWS: add note about new function for release 2.4.0.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Tue, 18 Mar 2014 00:51:13 +0000 |
parents | c45838839d86 |
children |
rev | line source |
---|---|
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
557
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 | 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 | 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 | 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 | 16 |
17 ## -*- texinfo -*- | |
541 | 18 ## @deftypefn {Function File} {@var{I} =} mat2gray (@var{M}) |
19 ## @deftypefnx {Function File} {@var{I} =} mat2gray (@var{M}, [@var{min} @var{max}]) | |
20 ## Convert a matrix to an intensity image. | |
21 ## | |
22 ## The returned matrix @var{I} is a grayscale image, of double class and in the | |
23 ## range of values [0, 1]. The optional arguments @var{min} and @var{max} will | |
24 ## set the limits of the conversion; values in @var{M} below @var{min} and | |
25 ## above @var{max} will be set to 0 and 1 on @var{I} respectively. | |
26 ## | |
27 ## @var{max} and @var{min} default to the maximum and minimum values of @var{M}. | |
28 ## | |
29 ## If @var{min} is larger than @var{max}, the `inverse' will be returned. Values | |
30 ## in @var{M} above @var{max} will be set to 0 while the ones below @var{min} | |
31 ## will be set to 1. | |
32 ## | |
557
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
33 ## @strong{Caution:} For compatibility with @sc{matlab}, if @var{min} and @var{max} |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
34 ## are equal (either from being actually being set manually or automatically |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
35 ## calculated from the @var{M} min and max values, Octave's mat2gray will truncate |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
36 ## all values between [0 1]. For example |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
37 ## |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
38 ## @example |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
39 ## @group |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
40 ## mat2gray ([-2 0 0.5 0.9 5], [2 2]) |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
41 ## @result{} [0 0 0.5 0.9 1] |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
42 ## mat2gray ([0.5 0.5 0.5]) |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
43 ## @result{} [0.5 0.5 0.5] |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
44 ## mat2gray ([4 4 4]) |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
45 ## @result{} [1 1 1] |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
46 ## @end group |
1ae80d1517b4
mat2gray: help text to mention situation when min and max are equal
carandraug
parents:
556
diff
changeset
|
47 ## @end example |
541 | 48 ## |
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
|
49 ## @seealso{gray2ind, ind2gray, rgb2gray, im2double, im2uin16, im2uint8, im2int16} |
186 | 50 ## @end deftypefn |
51 | |
556
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
52 function in = mat2gray (in, scale) |
186 | 53 |
494 | 54 if (nargin < 1 || nargin > 2) |
55 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
|
56 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
|
57 error ("mat2gray: first argument must be a matrix"); |
494 | 58 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
|
59 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
|
60 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
|
61 |
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 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
|
63 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
|
64 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
|
65 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
|
66 ## 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
|
67 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
|
68 out_max = max (scale (1), scale (2)); |
186 | 69 endif |
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 ## 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
|
72 ## 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
|
73 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
|
74 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
|
75 |
555
5b1405660aa2
mat2gray: matlab compatibility fixes for when max and min are the same
carandraug
parents:
541
diff
changeset
|
76 ## if max and min are the same, matlab seems to simple truncate the input |
5b1405660aa2
mat2gray: matlab compatibility fixes for when max and min are the same
carandraug
parents:
541
diff
changeset
|
77 ## between 0 and 1, and ignores the min/max values set. Don't get the logic |
5b1405660aa2
mat2gray: matlab compatibility fixes for when max and min are the same
carandraug
parents:
541
diff
changeset
|
78 ## but hey! Matlab compatibility |
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
|
79 if (out_min == out_max) |
555
5b1405660aa2
mat2gray: matlab compatibility fixes for when max and min are the same
carandraug
parents:
541
diff
changeset
|
80 in(in>1) = 1; |
5b1405660aa2
mat2gray: matlab compatibility fixes for when max and min are the same
carandraug
parents:
541
diff
changeset
|
81 in(in<0) = 0; |
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
|
82 return |
186 | 83 endif |
84 | |
556
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
85 ## we are editing the input matrix rather than creating a new one to save |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
86 ## memory. We need to make sure it's double though |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
87 in = double(in); |
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 ## it's faster to get the index of values between max and min only once |
556
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
90 ## than to have it calculated on both sides of the assignment later on. We |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
91 ## need to get the index before starting editing |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
92 idx = (in > out_min & in < out_max); |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
93 |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
94 in(in <= out_min) = 0; |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
95 in(in >= out_max) = 1; |
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
96 in(idx) = (1/(out_max - out_min)) * (double(in(idx)) - out_min); |
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
|
97 |
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
|
98 ## 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
|
99 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
|
100 ## 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
|
101 ## checking which one is actually correct. Seems to just invert it |
556
cdc1d454378a
mat2gray: use less memory by editing the input image rather than creating a copy and editing that one
carandraug
parents:
555
diff
changeset
|
102 in = abs (in - 1); |
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
|
103 endif |
186 | 104 |
105 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
|
106 |
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
|
107 %!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
|
108 %!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
|
109 %!assert(mat2gray([1 2 3], [2 2]), [1 1 1]); # equal min and max |
555
5b1405660aa2
mat2gray: matlab compatibility fixes for when max and min are the same
carandraug
parents:
541
diff
changeset
|
110 %!assert(mat2gray([-1 0 0.5 3], [2 2]), [0 0 0.5 1]); # equal min and max |
5b1405660aa2
mat2gray: matlab compatibility fixes for when max and min are the same
carandraug
parents:
541
diff
changeset
|
111 %!assert(mat2gray(ones(3*0.5)), ones(3*0.5)); # equal min and max from the image (not set) |
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
|
112 %!assert(mat2gray([1 2 3], [3 1]), [1 0.5 0]); # max and min inverted |