showIm.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. % RANGE = showIm (MATRIX, RANGE, ZOOM, LABEL, NSHADES )
  2. %
  3. % Display a MatLab MATRIX as a grayscale image in the current figure,
  4. % inside the current axes. If MATRIX is complex, the real and imaginary
  5. % parts are shown side-by-side, with the same grayscale mapping.
  6. %
  7. % If MATRIX is a string, it should be the name of a variable bound to a
  8. % MATRIX in the base (global) environment. This matrix is displayed as an
  9. % image, with the title set to the string.
  10. %
  11. % RANGE (optional) is a 2-vector specifying the values that map to
  12. % black and white, respectively. Passing a value of 'auto' (default)
  13. % sets RANGE=[min,max] (as in MatLab's imagesc). 'auto2' sets
  14. % RANGE=[mean-2*stdev, mean+2*stdev]. 'auto3' sets
  15. % RANGE=[p1-(p2-p1)/8, p2+(p2-p1)/8], where p1 is the 10th percentile
  16. % value of the sorted MATRIX samples, and p2 is the 90th percentile
  17. % value.
  18. %
  19. % ZOOM specifies the number of matrix samples per screen pixel. It
  20. % will be rounded to an integer, or 1 divided by an integer. A value
  21. % of 'same' or 'auto' (default) causes the zoom value to be chosen
  22. % automatically to fit the image into the current axes. A value of
  23. % 'full' fills the axis region (leaving no room for labels). See
  24. % pixelAxes.m.
  25. %
  26. % If LABEL (optional, default = 1, unless zoom='full') is non-zero, the range
  27. % of values that are mapped into the gray colormap and the dimensions
  28. % (size) of the matrix and zoom factor are printed below the image. If label
  29. % is a string, it is used as a title.
  30. %
  31. % NSHADES (optional) specifies the number of gray shades, and defaults
  32. % to the size of the current colormap.
  33. % Eero Simoncelli, 6/96.
  34. %%TODO: should use "newplot"
  35. function range = showIm( im, range, zoom, label, nshades );
  36. %------------------------------------------------------------
  37. %% OPTIONAL ARGS:
  38. if (nargin < 1)
  39. error('Requires at least one input argument.');
  40. end
  41. MLv = version;
  42. if isstr(im)
  43. if (strcmp(MLv(1),'4'))
  44. error('Cannot pass string arg for MATRIX in MatLab version 4.x');
  45. end
  46. label = im;
  47. im = evalin('base',im);
  48. end
  49. if (exist('range') ~= 1)
  50. range = 'auto1';
  51. end
  52. if (exist('nshades') ~= 1)
  53. nshades = size(colormap,1);
  54. end
  55. nshades = max( nshades, 2 );
  56. if (exist('zoom') ~= 1)
  57. zoom = 'auto';
  58. end
  59. if (exist('label') ~= 1)
  60. if strcmp(zoom,'full')
  61. label = 0; % no labeling
  62. else
  63. label = 1; % just print grayrange & dims
  64. end
  65. end
  66. %------------------------------------------------------------
  67. %% Automatic range calculation:
  68. if (strcmp(range,'auto1') | strcmp(range,'auto'))
  69. if isreal(im)
  70. [mn,mx] = range2(im);
  71. else
  72. [mn1,mx1] = range2(real(im));
  73. [mn2,mx2] = range2(imag(im));
  74. mn = min(mn1,mn2);
  75. mx = max(mx1,mx2);
  76. end
  77. if any(size(im)==1)
  78. pad = (mx-mn)/12; % MAGIC NUMBER: graph padding
  79. range = [mn-pad, mx+pad];
  80. else
  81. range = [mn,mx];
  82. end
  83. elseif strcmp(range,'auto2')
  84. if isreal(im)
  85. stdev = sqrt(var2(im));
  86. av = mean2(im);
  87. else
  88. stdev = sqrt((var2(real(im)) + var2(imag(im)))/2);
  89. av = (mean2(real(im)) + mean2(imag(im)))/2;
  90. end
  91. range = [av-2*stdev,av+2*stdev]; % MAGIC NUMBER: 2 stdevs
  92. elseif strcmp(range, 'auto3')
  93. percentile = 0.1; % MAGIC NUMBER: 0<p<0.5
  94. [N,X] = histo(im);
  95. binsz = X(2)-X(1);
  96. N = N+1e-10; % Ensure cumsum will be monotonic for call to interp1
  97. cumN = [0, cumsum(N)]/sum(N);
  98. cumX = [X(1)-binsz, X] + (binsz/2);
  99. ctrRange = interp1(cumN,cumX, [percentile, 1-percentile]);
  100. range = mean(ctrRange) + (ctrRange-mean(ctrRange))/(1-2*percentile);
  101. elseif isstr(range)
  102. error(sprintf('Bad RANGE argument: %s',range))
  103. end
  104. if ((range(2) - range(1)) <= eps)
  105. range(1) = range(1) - 0.5;
  106. range(2) = range(2) + 0.5;
  107. end
  108. if isreal(im)
  109. factor=1;
  110. else
  111. factor = 1+sqrt(-1);
  112. end
  113. xlbl_offset = 0; % default value
  114. if (~any(size(im)==1))
  115. %% MatLab's "image" rounds when mapping to the colormap, so we compute
  116. %% (im-r1)*(nshades-1)/(r2-r1) + 1.5
  117. mult = ((nshades-1) / (range(2)-range(1)));
  118. d_im = (mult * im) + factor*(1.5 - range(1)*mult);
  119. end
  120. if isreal(im)
  121. if (any(size(im)==1))
  122. hh = plot( im);
  123. axis([1, prod(size(im)), range]);
  124. else
  125. hh = image( d_im );
  126. axis('off');
  127. zoom = pixelAxes(size(d_im),zoom);
  128. end
  129. else
  130. if (any(size(im)==1))
  131. subplot(2,1,1);
  132. hh = plot(real(im));
  133. axis([1, prod(size(im)), range]);
  134. subplot(2,1,2);
  135. hh = plot(imag(im));
  136. axis([1, prod(size(im)), range]);
  137. else
  138. subplot(1,2,1);
  139. hh = image(real(d_im));
  140. axis('off'); zoom = pixelAxes(size(d_im),zoom);
  141. ax = gca; orig_units = get(ax,'Units');
  142. set(ax,'Units','points');
  143. pos1 = get(ax,'Position');
  144. set(ax,'Units',orig_units);
  145. subplot(1,2,2);
  146. hh = image(imag(d_im));
  147. axis('off'); zoom = pixelAxes(size(d_im),zoom);
  148. ax = gca; orig_units = get(ax,'Units');
  149. set(ax,'Units','points');
  150. pos2 = get(ax,'Position');
  151. set(ax,'Units',orig_units);
  152. xlbl_offset = (pos1(1)-pos2(1))/2;
  153. end
  154. end
  155. if ~any(size(im)==1)
  156. colormap(gray(nshades));
  157. end
  158. if ((label ~= 0))
  159. if isstr(label)
  160. title(label);
  161. h = get(gca,'Title');
  162. orig_units = get(h,'Units');
  163. set(h,'Units','points');
  164. pos = get(h,'Position');
  165. pos(1:2) = pos(1:2) + [xlbl_offset, -3]; % MAGIC NUMBER: y pixel offset
  166. set(h,'Position',pos);
  167. set(h,'Units',orig_units);
  168. end
  169. if (~any(size(im)==1))
  170. if (zoom > 1)
  171. zformat = sprintf('* %d',round(zoom));
  172. else
  173. zformat = sprintf('/ %d',round(1/zoom));
  174. end
  175. if isreal(im)
  176. format=[' Range: [%.3g, %.3g] \n Dims: [%d, %d] ', zformat];
  177. else
  178. format=['Range: [%.3g, %.3g] ---- Dims: [%d, %d]', zformat];
  179. end
  180. xlabel(sprintf(format, range(1), range(2), size(im,1), size(im,2)));
  181. h = get(gca,'Xlabel');
  182. set(h,'FontSize', 9); % MAGIC NUMBER: font size!!!
  183. orig_units = get(h,'Units');
  184. set(h,'Units','points');
  185. pos = get(h,'Position');
  186. pos(1:2) = pos(1:2) + [xlbl_offset, 10]; % MAGIC NUMBER: y offset in points
  187. set(h,'Position',pos);
  188. set(h,'Units',orig_units);
  189. set(h,'Visible','on'); % axis('image') turned the xlabel off...
  190. end
  191. end
  192. return;