5934
|
1 ## Copyright (C) 2005 Soren Hauberg |
|
2 ## |
2313
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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}) |
|
26 ## @deftypefnx {Function File} {} imshow (@dots{}, @var{string_param1}, @var{value1}, ...) |
|
27 ## Display the image @var{im}, where @var{im} can a 2-dimensional |
|
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 |
|
47 ## @item "display_range" |
|
48 ## @var{value1} is the display range as described above. |
3426
|
49 ## |
5934
|
50 ## @item "InitialMagnification" |
|
51 ## @var{value1} sets the zoom level in percent. |
|
52 ## If @var{value1} is 100 the image is showed unscaled. |
|
53 ## @end table |
5642
|
54 ## @seealso{image, imagesc, colormap, gray2ind, rgb2ind} |
3373
|
55 ## @end deftypefn |
904
|
56 |
5934
|
57 ## Author: Soren Hauberg <hauberg at gmail dot com> |
2312
|
58 ## Adapted-By: jwe |
559
|
59 |
5934
|
60 function imshow (im, varargin) |
559
|
61 |
5934
|
62 if (nargin == 0) |
|
63 print_usage (); |
5318
|
64 endif |
|
65 |
5935
|
66 ## Get the image. |
5934
|
67 if (ischar (im)) |
|
68 im = loadimage (im); # It would be better to use imread from octave-forge |
|
69 elseif (! ismatrix (im)) |
|
70 error ("imshow: first argument must be an image or the filename of an image"); |
5318
|
71 endif |
4836
|
72 |
5934
|
73 ## Is the function called with 3 matrices (i.e., imshow (R, G, B))? |
|
74 if (nargin >= 3 |
|
75 && ndims (im) == 2 |
|
76 && ndims (varargin{1}) == 2 |
|
77 && ndims (varargin{2}) == 2 |
|
78 && size (im) == size (varargin{1}) |
|
79 && size (im) == size (varargin{2})) |
|
80 im(:,:,3) = varargin{2}; |
|
81 im(:,:,2) = varargin{1}; |
|
82 varargin(1:2) = []; |
5318
|
83 endif |
|
84 |
5934
|
85 ## Set default display range. |
|
86 switch class (im) |
|
87 case {"uint8"} |
|
88 display_range = [0, 255]; |
|
89 case {"uint16"} |
|
90 display_range = [0, 65535]; |
|
91 case {"double", "single", "logical"} |
|
92 display_range = [0, 1]; |
|
93 otherwise |
|
94 error ("imshow: currently only images whos class is uint8, uint16, logical, or double are supported"); |
|
95 endswitch |
|
96 |
|
97 ## Set other default parameters. |
5318
|
98 isindexed = false; |
5934
|
99 initial_magnification = 100; |
|
100 old_colormap = color_map = colormap (); |
5318
|
101 |
5934
|
102 ## Handle the rest of the arguments. |
|
103 narg = 1; |
|
104 while (narg <= length (varargin)) |
|
105 arg = varargin{narg}; |
|
106 if (ismatrix (arg) && ndims (arg) == 2) |
|
107 display_range = arg; |
|
108 elseif (isempty (arg)) |
|
109 display_range = [min(im(:)), max(im(:))]; |
|
110 elseif (ismatrix (arg) && size (arg, 2) == 3) |
|
111 color_map = arg; |
|
112 isindexed = true; |
|
113 elseif (ischar (arg) && strcmpi (arg, "truesize")) |
|
114 initial_magnification = 100; |
|
115 elseif (ischar (arg) && strcmpi (arg, "displayrange")) |
|
116 narg++; |
|
117 display_range = varargin{narg}; |
|
118 elseif (ischar (arg) && strcmpi (arg, "initialmagnification")) |
|
119 narg++; |
|
120 initial_magnification = varargin{narg}; |
|
121 else |
|
122 warning ("imshow: input argument number %d is unsupported", narg) |
4911
|
123 endif |
5934
|
124 narg++; |
|
125 endwhile |
|
126 |
5935
|
127 ## Check for complex images. |
5934
|
128 if (iscomplex (im)) |
|
129 warning ("imshow: only showing real part of complex image"); |
|
130 im = real (im); |
5318
|
131 endif |
|
132 |
5934
|
133 ## Scale the image to the interval [0, 1] according to display_range. |
|
134 if (! isindexed) |
|
135 low = display_range(1); |
|
136 high = display_range(2); |
|
137 im = (double (im) - low)/(high-low); |
|
138 im(im < 0) = 0; |
|
139 im(im > 1) = 1; |
4836
|
140 endif |
|
141 |
5935
|
142 ## Convert to indexed image. |
5934
|
143 dim = ndims (im); |
|
144 if (dim == 2) |
|
145 im = round ((size (color_map, 1) - 1) * im); |
|
146 elseif (dim == 3 && size (im, 3) == 3) |
5935
|
147 [im, color_map] = rgb2ind (im); |
5934
|
148 else |
|
149 error ("imshow: input image must be a 2D or 3D matrix"); |
|
150 endif |
|
151 |
5935
|
152 ## And now, we show the image. |
5934
|
153 colormap (color_map); |
|
154 image (im, initial_magnification/100); |
|
155 colormap (old_colormap); |
559
|
156 |
|
157 endfunction |
4836
|
158 |
|
159 %!error imshow () # no arguments |
5934
|
160 %!error imshow ({"cell"}) # No image or filename given |
|
161 %!error imshow (int8(1)) # Unsupported image class |
|
162 %!error imshow (ones(4,4,4)) # Too many dimensions in image |
4836
|
163 |
|
164 %!demo |
|
165 %! imshow (loadimage ("default.img")); |
|
166 |
|
167 %!demo |
|
168 %! I = loadimage ("default.img"); |
|
169 %! imshow (I, "truesize") |
|
170 |
|
171 %!demo |
|
172 %! [I, M] = loadimage ("default.img"); |
5318
|
173 %! imshow (I, M); |
|
174 |
|
175 %!demo |
|
176 %! [I, M] = loadimage ("default.img"); |
5934
|
177 %! imshow (cat(3, I, I*0.5, I*0.8)); |
5318
|
178 |
|
179 %!demo |
5934
|
180 %! I = loadimage("default.img"); |
|
181 %! imshow(I, I, I); |