showLpyr.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. % RANGE = showLpyr (PYR, INDICES, RANGE, GAP, LEVEL_SCALE_FACTOR)
  2. %
  3. % Display a Laplacian (or Gaussian) pyramid, specified by PYR and
  4. % INDICES (see buildLpyr), in the current figure.
  5. %
  6. % RANGE is a 2-vector specifying the values that map to black and
  7. % white, respectively. These values are scaled by
  8. % LEVEL_SCALE_FACTOR^(lev-1) for bands at each level. Passing a value
  9. % of 'auto1' sets RANGE to the min and max values of MATRIX. 'auto2'
  10. % sets RANGE to 3 standard deviations below and above 0.0. In both of
  11. % these cases, the lowpass band is independently scaled. A value of
  12. % 'indep1' sets the range of each subband independently, as in a call
  13. % to showIm(subband,'auto1'). Similarly, 'indep2' causes each subband
  14. % to be scaled independently as if by showIm(subband,'indep2').
  15. % The default value for RANGE is 'auto1' for 1D images, and 'auto2' for
  16. % 2D images.
  17. %
  18. % GAP (optional, default=1) specifies the gap in pixels to leave
  19. % between subbands (2D images only).
  20. %
  21. % LEVEL_SCALE_FACTOR indicates the relative scaling between pyramid
  22. % levels. This should be set to the sum of the kernel taps of the
  23. % lowpass filter used to construct the pyramid (default assumes
  24. % L2-normalalized filters, using a value of 2 for 2D images, sqrt(2) for
  25. % 1D images).
  26. % Eero Simoncelli, 2/97.
  27. function [range] = showLpyr(pyr, pind, range, gap, scale);
  28. % Determine 1D or 2D pyramid:
  29. if ((pind(1,1) == 1) | (pind(1,2) ==1))
  30. oned = 1;
  31. else
  32. oned = 0;
  33. end
  34. %------------------------------------------------------------
  35. %% OPTIONAL ARGS:
  36. if (exist('range') ~= 1)
  37. if (oned==1)
  38. range = 'auto1';
  39. else
  40. range = 'auto2';
  41. end
  42. end
  43. if (exist('gap') ~= 1)
  44. gap = 1;
  45. end
  46. if (exist('scale') ~= 1)
  47. if (oned == 1)
  48. scale = sqrt(2);
  49. else
  50. scale = 2;
  51. end
  52. end
  53. %------------------------------------------------------------
  54. nind = size(pind,1);
  55. %% Auto range calculations:
  56. if strcmp(range,'auto1')
  57. range = zeros(nind,1);
  58. mn = 0.0; mx = 0.0;
  59. for bnum = 1:(nind-1)
  60. band = pyrBand(pyr,pind,bnum)/(scale^(bnum-1));
  61. range(bnum) = scale^(bnum-1);
  62. [bmn,bmx] = range2(band);
  63. mn = min(mn, bmn); mx = max(mx, bmx);
  64. end
  65. if (oned == 1)
  66. pad = (mx-mn)/12; % *** MAGIC NUMBER!!
  67. mn = mn-pad; mx = mx+pad;
  68. end
  69. range = range * [mn mx]; % outer product
  70. band = pyrLow(pyr,pind);
  71. [mn,mx] = range2(band);
  72. if (oned == 1)
  73. pad = (mx-mn)/12; % *** MAGIC NUMBER!!
  74. mn = mn-pad; mx = mx+pad;
  75. end
  76. range(nind,:) = [mn, mx];
  77. elseif strcmp(range,'indep1')
  78. range = zeros(nind,2);
  79. for bnum = 1:nind
  80. band = pyrBand(pyr,pind,bnum);
  81. [mn,mx] = range2(band);
  82. if (oned == 1)
  83. pad = (mx-mn)/12; % *** MAGIC NUMBER!!
  84. mn = mn-pad; mx = mx+pad;
  85. end
  86. range(bnum,:) = [mn mx];
  87. end
  88. elseif strcmp(range,'auto2')
  89. range = zeros(nind,1);
  90. sqsum = 0; numpixels = 0;
  91. for bnum = 1:(nind-1)
  92. band = pyrBand(pyr,pind,bnum)/(scale^(bnum-1));
  93. sqsum = sqsum + sum(sum(band.^2));
  94. numpixels = numpixels + prod(size(band));
  95. range(bnum) = scale^(bnum-1);
  96. end
  97. stdev = sqrt(sqsum/(numpixels-1));
  98. range = range * [ -3*stdev 3*stdev ]; % outer product
  99. band = pyrLow(pyr,pind);
  100. av = mean2(band); stdev = sqrt(var2(band));
  101. range(nind,:) = [av-2*stdev,av+2*stdev];
  102. elseif strcmp(range,'indep2')
  103. range = zeros(nind,2);
  104. for bnum = 1:(nind-1)
  105. band = pyrBand(pyr,pind,bnum);
  106. stdev = sqrt(var2(band));
  107. range(bnum,:) = [ -3*stdev 3*stdev ];
  108. end
  109. band = pyrLow(pyr,pind);
  110. av = mean2(band); stdev = sqrt(var2(band));
  111. range(nind,:) = [av-2*stdev,av+2*stdev];
  112. elseif isstr(range)
  113. error(sprintf('Bad RANGE argument: %s',range))
  114. elseif ((size(range,1) == 1) & (size(range,2) == 2))
  115. scales = scale.^[0:nind-1];
  116. range = scales(:) * range; % outer product
  117. band = pyrLow(pyr,pind);
  118. range(nind,:) = range(nind,:) + mean2(band) - mean(range(nind,:));
  119. end
  120. %% Clear Figure
  121. clf;
  122. if (oned == 1)
  123. %%%%% 1D signal:
  124. for bnum=1:nind
  125. band = pyrBand(pyr,pind,bnum);
  126. subplot(nind,1,nind-bnum+1);
  127. plot(band);
  128. axis([1, prod(size(band)), range(bnum,:)]);
  129. end
  130. else
  131. %%%%% 2D signal:
  132. colormap(gray);
  133. cmap = get(gcf,'Colormap');
  134. nshades = size(cmap,1);
  135. % Find background color index:
  136. clr = get(gcf,'Color');
  137. bg = 1;
  138. dist = norm(cmap(bg,:)-clr);
  139. for n = 1:nshades
  140. ndist = norm(cmap(n,:)-clr);
  141. if (ndist < dist)
  142. dist = ndist;
  143. bg = n;
  144. end
  145. end
  146. %% Compute positions of subbands:
  147. llpos = ones(nind,2);
  148. dir = [-1 -1];
  149. ctr = [pind(1,1)+1+gap 1];
  150. sz = [0 0];
  151. for bnum = 1:nind
  152. prevsz = sz;
  153. sz = pind(bnum,:);
  154. % Determine center position of new band:
  155. ctr = ctr + gap*dir/2 + dir.* floor((prevsz+(dir>0))/2);
  156. dir = dir * [0 -1; 1 0]; % ccw rotation
  157. ctr = ctr + gap*dir/2 + dir.* floor((sz+(dir<0))/2);
  158. llpos(bnum,:) = ctr - floor(sz./2);
  159. end
  160. %% Make position list positive, and allocate appropriate image:
  161. llpos = llpos - ones(nind,1)*min(llpos) + 1;
  162. urpos = llpos + pind - 1;
  163. d_im = bg + zeros(max(urpos));
  164. %% Paste bands into image, (im-r1)*(nshades-1)/(r2-r1) + 1.5
  165. for bnum=1:nind
  166. mult = (nshades-1) / (range(bnum,2)-range(bnum,1));
  167. d_im(llpos(bnum,1):urpos(bnum,1), llpos(bnum,2):urpos(bnum,2)) = ...
  168. mult*pyrBand(pyr,pind,bnum) + (1.5-mult*range(bnum,1));
  169. end
  170. hh = image(d_im);
  171. axis('off');
  172. pixelAxes(size(d_im),'full');
  173. set(hh,'UserData',range);
  174. end