7017
|
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004, 2005, |
|
2 ## 2006, 2007 Soren Hauberg |
5934
|
3 ## |
2313
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
1024
|
19 |
3381
|
20 ## -*- texinfo -*- |
5934
|
21 ## @deftypefn {Function File} {} imshow (@var{im}) |
|
22 ## @deftypefnx {Function File} {} imshow (@var{im}, @var{limits}) |
|
23 ## @deftypefnx {Function File} {} imshow (@var{im}, @var{map}) |
|
24 ## @deftypefnx {Function File} {} imshow (@var{R}, @var{G}, @var{B}, @dots{}) |
|
25 ## @deftypefnx {Function File} {} imshow (@var{filename}) |
6309
|
26 ## @deftypefnx {Function File} {} imshow (@dots{}, @var{string_param1}, @var{value1}, @dots{}) |
6368
|
27 ## Display the image @var{im}, where @var{im} can be a 2-dimensional |
5934
|
28 ## (gray-scale image) or a 3-dimensional (RGB image) matrix. If three matrices |
|
29 ## of the same size are given as arguments, they will be concatenated into |
|
30 ## a 3-dimensional (RGB image) matrix. |
|
31 ## |
|
32 ## If @var{limits} is a 2-element vector @code{[@var{low}, @var{high}]}, |
|
33 ## the image is shown using a display range between @var{low} and |
|
34 ## @var{high}. If an empty matrix is passed for @var{limits}, the |
|
35 ## display range is computed as the range between the minimal and the |
|
36 ## maximal value in the image. |
3426
|
37 ## |
5934
|
38 ## If @var{map} is a valid color map, the image will be shown as an indexed |
|
39 ## image using the supplied color map. |
|
40 ## |
|
41 ## If a file name is given instead of an image, the file will be read and |
|
42 ## shown. |
3426
|
43 ## |
5934
|
44 ## If given, the parameter @var{string_param1} has value |
|
45 ## @var{value1}. @var{string_param1} can be any of the following: |
|
46 ## @table @samp |
6368
|
47 ## @item "displayrange" |
5934
|
48 ## @var{value1} is the display range as described above. |
|
49 ## @end table |
5642
|
50 ## @seealso{image, imagesc, colormap, gray2ind, rgb2ind} |
3373
|
51 ## @end deftypefn |
904
|
52 |
5934
|
53 ## Author: Soren Hauberg <hauberg at gmail dot com> |
2312
|
54 ## Adapted-By: jwe |
559
|
55 |
5934
|
56 function imshow (im, varargin) |
559
|
57 |
5934
|
58 if (nargin == 0) |
|
59 print_usage (); |
5318
|
60 endif |
|
61 |
5935
|
62 ## Get the image. |
5934
|
63 if (ischar (im)) |
|
64 im = loadimage (im); # It would be better to use imread from octave-forge |
|
65 elseif (! ismatrix (im)) |
|
66 error ("imshow: first argument must be an image or the filename of an image"); |
5318
|
67 endif |
4836
|
68 |
5934
|
69 ## Is the function called with 3 matrices (i.e., imshow (R, G, B))? |
|
70 if (nargin >= 3 |
|
71 && ndims (im) == 2 |
|
72 && ndims (varargin{1}) == 2 |
|
73 && ndims (varargin{2}) == 2 |
6157
|
74 && size_equal (im, varargin{1}) |
|
75 && size_equal (im, varargin{2})) |
5934
|
76 im(:,:,3) = varargin{2}; |
|
77 im(:,:,2) = varargin{1}; |
|
78 varargin(1:2) = []; |
5318
|
79 endif |
|
80 |
5934
|
81 ## Set default display range. |
|
82 switch class (im) |
|
83 case {"uint8"} |
|
84 display_range = [0, 255]; |
|
85 case {"uint16"} |
|
86 display_range = [0, 65535]; |
|
87 case {"double", "single", "logical"} |
|
88 display_range = [0, 1]; |
|
89 otherwise |
|
90 error ("imshow: currently only images whos class is uint8, uint16, logical, or double are supported"); |
|
91 endswitch |
|
92 |
|
93 ## Set other default parameters. |
5318
|
94 isindexed = false; |
5934
|
95 initial_magnification = 100; |
6310
|
96 color_map = colormap (); |
5318
|
97 |
5934
|
98 ## Handle the rest of the arguments. |
|
99 narg = 1; |
|
100 while (narg <= length (varargin)) |
|
101 arg = varargin{narg}; |
6756
|
102 if (ismatrix (arg) && size (arg, 2) == 3) |
|
103 color_map = arg; |
|
104 isindexed = true; |
6757
|
105 elseif (ismatrix (arg) && numel (arg) == 2) |
5934
|
106 display_range = arg; |
|
107 elseif (isempty (arg)) |
|
108 display_range = [min(im(:)), max(im(:))]; |
|
109 elseif (ischar (arg) && strcmpi (arg, "displayrange")) |
|
110 narg++; |
|
111 display_range = varargin{narg}; |
6368
|
112 elseif (ischar (arg) && |
|
113 (strcmpi (arg, "truesize") || |
|
114 strcmpi (arg, "initialmagnification"))) |
5934
|
115 narg++; |
6368
|
116 warning ("image: zoom argument ignored -- use GUI features"); |
5934
|
117 else |
|
118 warning ("imshow: input argument number %d is unsupported", narg) |
4911
|
119 endif |
5934
|
120 narg++; |
|
121 endwhile |
|
122 |
5935
|
123 ## Check for complex images. |
5934
|
124 if (iscomplex (im)) |
|
125 warning ("imshow: only showing real part of complex image"); |
|
126 im = real (im); |
5318
|
127 endif |
|
128 |
6219
|
129 nans = isnan (im(:)); |
|
130 if (any (nans)) |
|
131 warning ("Octave:imshow-NaN", |
|
132 "imshow: pixel with NaN or NA values are set to zero"); |
|
133 im(nans) = display_range(1); |
|
134 endif |
|
135 |
5934
|
136 ## Scale the image to the interval [0, 1] according to display_range. |
|
137 if (! isindexed) |
|
138 low = display_range(1); |
|
139 high = display_range(2); |
|
140 im = (double (im) - low)/(high-low); |
|
141 im(im < 0) = 0; |
|
142 im(im > 1) = 1; |
4836
|
143 endif |
6368
|
144 |
5934
|
145 dim = ndims (im); |
|
146 if (dim == 2) |
|
147 im = round ((size (color_map, 1) - 1) * im); |
6368
|
148 image (im); |
6309
|
149 colormap (color_map); |
5934
|
150 elseif (dim == 3 && size (im, 3) == 3) |
6309
|
151 __img__ ([] , [], im); |
|
152 ## FIXME -- needed anymore for a special case? |
|
153 ## Convert to indexed image. |
|
154 ## [im, color_map] = rgb2ind (im); |
5934
|
155 else |
|
156 error ("imshow: input image must be a 2D or 3D matrix"); |
|
157 endif |
|
158 |
559
|
159 endfunction |
4836
|
160 |
|
161 %!error imshow () # no arguments |
5934
|
162 %!error imshow ({"cell"}) # No image or filename given |
|
163 %!error imshow (int8(1)) # Unsupported image class |
|
164 %!error imshow (ones(4,4,4)) # Too many dimensions in image |
4836
|
165 |
|
166 %!demo |
|
167 %! imshow (loadimage ("default.img")); |
|
168 |
|
169 %!demo |
|
170 %! [I, M] = loadimage ("default.img"); |
5318
|
171 %! imshow (I, M); |
|
172 |
|
173 %!demo |
|
174 %! [I, M] = loadimage ("default.img"); |
5934
|
175 %! imshow (cat(3, I, I*0.5, I*0.8)); |
5318
|
176 |
|
177 %!demo |
5934
|
178 %! I = loadimage("default.img"); |
|
179 %! imshow(I, I, I); |