reconWpyr.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. % RES = reconWpyr(PYR, INDICES, FILT, EDGES, LEVS, BANDS)
  2. %
  3. % Reconstruct image from its separable orthonormal QMF/wavelet pyramid
  4. % representation, as created by buildWpyr.
  5. %
  6. % PYR is a vector containing the N pyramid subbands, ordered from fine
  7. % to coarse. INDICES is an Nx2 matrix containing the sizes of
  8. % each subband. This is compatible with the MatLab Wavelet toolbox.
  9. %
  10. % FILT (optional) can be a string naming a standard filter (see
  11. % namedFilter), or a vector which will be used for (separable)
  12. % convolution. Default = 'qmf9'. EDGES specifies edge-handling,
  13. % and defaults to 'reflect1' (see corrDn).
  14. %
  15. % LEVS (optional) should be a vector of levels to include, or the string
  16. % 'all' (default). 1 corresponds to the finest scale. The lowpass band
  17. % corresponds to wpyrHt(INDICES)+1.
  18. %
  19. % BANDS (optional) should be a vector of bands to include, or the string
  20. % 'all' (default). 1=horizontal, 2=vertical, 3=diagonal. This is only used
  21. % for pyramids of 2D images.
  22. % Eero Simoncelli, 6/96.
  23. function res = reconWpyr(pyr, ind, filt, edges, levs, bands)
  24. if (nargin < 2)
  25. error('First two arguments (PYR INDICES) are required');
  26. end
  27. %%------------------------------------------------------------
  28. %% OPTIONAL ARGS:
  29. if (exist('filt') ~= 1)
  30. filt = 'qmf9';
  31. end
  32. if (exist('edges') ~= 1)
  33. edges= 'reflect1';
  34. end
  35. if (exist('levs') ~= 1)
  36. levs = 'all';
  37. end
  38. if (exist('bands') ~= 1)
  39. bands = 'all';
  40. end
  41. %%------------------------------------------------------------
  42. maxLev = 1+wpyrHt(ind);
  43. if strcmp(levs,'all')
  44. levs = [1:maxLev]';
  45. else
  46. if (any(levs > maxLev))
  47. error(sprintf('Level numbers must be in the range [1, %d].', maxLev));
  48. end
  49. levs = levs(:);
  50. end
  51. if strcmp(bands,'all')
  52. bands = [1:3]';
  53. else
  54. if (any(bands < 1) | any(bands > 3))
  55. error('Band numbers must be in the range [1,3].');
  56. end
  57. bands = bands(:);
  58. end
  59. if isstr(filt)
  60. filt = namedFilter(filt);
  61. end
  62. filt = filt(:);
  63. hfilt = modulateFlip(filt);
  64. %% For odd-length filters, stagger the sampling lattices:
  65. if (mod(size(filt,1),2) == 0)
  66. stag = 2;
  67. else
  68. stag = 1;
  69. end
  70. %% Compute size of result image: assumes critical sampling (boundaries correct)
  71. res_sz = ind(1,:);
  72. if (res_sz(1) == 1)
  73. loind = 2;
  74. res_sz(2) = sum(ind(:,2));
  75. elseif (res_sz(2) == 1)
  76. loind = 2;
  77. res_sz(1) = sum(ind(:,1));
  78. else
  79. loind = 4;
  80. res_sz = ind(1,:) + ind(2,:); %%horizontal + vertical bands.
  81. hres_sz = [ind(1,1), res_sz(2)];
  82. lres_sz = [ind(2,1), res_sz(2)];
  83. end
  84. %% First, recursively collapse coarser scales:
  85. if any(levs > 1)
  86. if (size(ind,1) > loind)
  87. nres = reconWpyr( pyr(1+sum(prod(ind(1:loind-1,:)')):size(pyr,1)), ...
  88. ind(loind:size(ind,1),:), filt, edges, levs-1, bands);
  89. else
  90. nres = pyrBand(pyr, ind, loind); % lowpass subband
  91. end
  92. if (res_sz(1) == 1)
  93. res = upConv(nres, filt', edges, [1 2], [1 stag], res_sz);
  94. elseif (res_sz(2) == 1)
  95. res = upConv(nres, filt, edges, [2 1], [stag 1], res_sz);
  96. else
  97. ires = upConv(nres, filt', edges, [1 2], [1 stag], lres_sz);
  98. res = upConv(ires, filt, edges, [2 1], [stag 1], res_sz);
  99. end
  100. else
  101. res = zeros(res_sz);
  102. end
  103. %% Add in reconstructed bands from this level:
  104. if any(levs == 1)
  105. if (res_sz(1) == 1)
  106. upConv(pyrBand(pyr,ind,1), hfilt', edges, [1 2], [1 2], res_sz, res);
  107. elseif (res_sz(2) == 1)
  108. upConv(pyrBand(pyr,ind,1), hfilt, edges, [2 1], [2 1], res_sz, res);
  109. else
  110. if any(bands == 1) % horizontal
  111. ires = upConv(pyrBand(pyr,ind,1),filt',edges,[1 2],[1 stag],hres_sz);
  112. upConv(ires,hfilt,edges,[2 1],[2 1],res_sz,res); %destructively modify res
  113. end
  114. if any(bands == 2) % vertical
  115. ires = upConv(pyrBand(pyr,ind,2),hfilt',edges,[1 2],[1 2],lres_sz);
  116. upConv(ires,filt,edges,[2 1],[stag 1],res_sz,res); %destructively modify res
  117. end
  118. if any(bands == 3) % diagonal
  119. ires = upConv(pyrBand(pyr,ind,3),hfilt',edges,[1 2],[1 2],hres_sz);
  120. upConv(ires,hfilt,edges,[2 1],[2 1],res_sz,res); %destructively modify res
  121. end
  122. end
  123. end