showWpyr.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. % RANGE = showWpyr (PYR, INDICES, RANGE, GAP, LEVEL_SCALE_FACTOR)
  2. %
  3. % Display a separable QMF/wavelet pyramid, specified by PYR and INDICES
  4. % (see buildWpyr), 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-normalized filters, using a value of 2 for 2D images, sqrt(2) for
  25. % 1D images).
  26. % Eero Simoncelli, 2/97.
  27. function [range] = showWpyr(pyr, pind, range, gap, scale);
  28. % Determine 1D or 2D pyramid:
  29. if ((pind(1,1) == 1) | (pind(1,2) ==1))
  30. nbands = 1;
  31. else
  32. nbands = 3;
  33. end
  34. %------------------------------------------------------------
  35. %% OPTIONAL ARGS:
  36. if (exist('range') ~= 1)
  37. if (nbands==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 (nbands == 1)
  48. scale = sqrt(2);
  49. else
  50. scale = 2;
  51. end
  52. end
  53. %------------------------------------------------------------
  54. ht = wpyrHt(pind);
  55. nind = size(pind,1);
  56. %% Auto range calculations:
  57. if strcmp(range,'auto1')
  58. range = zeros(nind,1);
  59. mn = 0.0; mx = 0.0;
  60. for lnum = 1:ht
  61. for bnum = 1:nbands
  62. band = wpyrBand(pyr,pind,lnum,bnum)/(scale^(lnum-1));
  63. range((lnum-1)*nbands+bnum) = scale^(lnum-1);
  64. [bmn,bmx] = range2(band);
  65. mn = min(mn, bmn); mx = max(mx, bmx);
  66. end
  67. end
  68. if (nbands == 1)
  69. pad = (mx-mn)/12; % *** MAGIC NUMBER!!
  70. mn = mn-pad; mx = mx+pad;
  71. end
  72. range = range * [mn mx]; % outer product
  73. band = pyrLow(pyr,pind);
  74. [mn,mx] = range2(band);
  75. if (nbands == 1)
  76. pad = (mx-mn)/12; % *** MAGIC NUMBER!!
  77. mn = mn-pad; mx = mx+pad;
  78. end
  79. range(nind,:) = [mn, mx];
  80. elseif strcmp(range,'indep1')
  81. range = zeros(nind,2);
  82. for bnum = 1:nind
  83. band = pyrBand(pyr,pind,bnum);
  84. [mn,mx] = range2(band);
  85. if (nbands == 1)
  86. pad = (mx-mn)/12; % *** MAGIC NUMBER!!
  87. mn = mn-pad; mx = mx+pad;
  88. end
  89. range(bnum,:) = [mn mx];
  90. end
  91. elseif strcmp(range,'auto2')
  92. range = zeros(nind,1);
  93. sqsum = 0; numpixels = 0;
  94. for lnum = 1:ht
  95. for bnum = 1:nbands
  96. band = wpyrBand(pyr,pind,lnum,bnum)/(scale^(lnum-1));
  97. sqsum = sqsum + sum(sum(band.^2));
  98. numpixels = numpixels + prod(size(band));
  99. range((lnum-1)*nbands+bnum) = scale^(lnum-1);
  100. end
  101. end
  102. stdev = sqrt(sqsum/(numpixels-1));
  103. range = range * [ -3*stdev 3*stdev ]; % outer product
  104. band = pyrLow(pyr,pind);
  105. av = mean2(band); stdev = sqrt(var2(band));
  106. range(nind,:) = [av-2*stdev,av+2*stdev];
  107. elseif strcmp(range,'indep2')
  108. range = zeros(nind,2);
  109. for bnum = 1:(nind-1)
  110. band = pyrBand(pyr,pind,bnum);
  111. stdev = sqrt(var2(band));
  112. range(bnum,:) = [ -3*stdev 3*stdev ];
  113. end
  114. band = pyrLow(pyr,pind);
  115. av = mean2(band); stdev = sqrt(var2(band));
  116. range(nind,:) = [av-2*stdev,av+2*stdev];
  117. elseif isstr(range)
  118. error(sprintf('Bad RANGE argument: %s',range))
  119. elseif ((size(range,1) == 1) & (size(range,2) == 2))
  120. scales = scale.^[0:ht];
  121. if (nbands ~= 1)
  122. scales = [scales; scales; scales];
  123. end
  124. range = scales(:) * range; % outer product
  125. band = pyrLow(pyr,pind);
  126. range(nind,:) = range(nind,:) + mean2(band) - mean(range(nind,:));
  127. end
  128. % CLEAR FIGURE:
  129. clf;
  130. if (nbands == 1)
  131. %%%%% 1D signal:
  132. for bnum=1:nind
  133. band = pyrBand(pyr,pind,bnum);
  134. subplot(nind,1,nind-bnum+1);
  135. plot(band);
  136. axis([1, prod(size(band)), range(bnum,:)]);
  137. end
  138. else
  139. %%%%% 2D signal:
  140. colormap(gray);
  141. cmap = get(gcf,'Colormap');
  142. nshades = size(cmap,1);
  143. % Find background color index:
  144. clr = get(gcf,'Color');
  145. bg = 1;
  146. dist = norm(cmap(bg,:)-clr);
  147. for n = 1:nshades
  148. ndist = norm(cmap(n,:)-clr);
  149. if (ndist < dist)
  150. dist = ndist;
  151. bg = n;
  152. end
  153. end
  154. %% Compute positions of subbands:
  155. llpos = ones(nind,2);
  156. for lnum = 1:ht
  157. ind1 = nbands*(lnum-1) + 1;
  158. xpos = pind(ind1,2) + 1 + gap*(ht-lnum+1);
  159. ypos = pind(ind1+1,1) + 1 + gap*(ht-lnum+1);
  160. llpos(ind1:ind1+2,:) = [ypos 1; 1 xpos; ypos xpos];
  161. end
  162. llpos(nind,:) = [1 1]; %lowpass
  163. %% Make position list positive, and allocate appropriate image:
  164. llpos = llpos - ones(nind,1)*min(llpos) + 1;
  165. urpos = llpos + pind - 1;
  166. d_im = bg + zeros(max(urpos));
  167. %% Paste bands into image, (im-r1)*(nshades-1)/(r2-r1) + 1.5
  168. for bnum=1:nind
  169. mult = (nshades-1) / (range(bnum,2)-range(bnum,1));
  170. d_im(llpos(bnum,1):urpos(bnum,1), llpos(bnum,2):urpos(bnum,2)) = ...
  171. mult*pyrBand(pyr,pind,bnum) + (1.5-mult*range(bnum,1));
  172. end
  173. hh = image(d_im);
  174. axis('off');
  175. pixelAxes(size(d_im),'full');
  176. set(hh,'UserData',range);
  177. end