buildWpyr.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. % [PYR, INDICES] = buildWpyr(IM, HEIGHT, FILT, EDGES)
  2. %
  3. % Construct a separable orthonormal QMF/wavelet pyramid on matrix (or vector) IM.
  4. %
  5. % HEIGHT (optional) specifies the number of pyramid levels to build. Default
  6. % is maxPyrHt(IM,FILT). You can also specify 'auto' to use this value.
  7. %
  8. % FILT (optional) can be a string naming a standard filter (see
  9. % namedFilter), or a vector which will be used for (separable)
  10. % convolution. Filter can be of even or odd length, but should be symmetric.
  11. % Default = 'qmf9'. EDGES specifies edge-handling, and
  12. % defaults to 'reflect1' (see corrDn).
  13. %
  14. % PYR is a vector containing the N pyramid subbands, ordered from fine
  15. % to coarse. INDICES is an Nx2 matrix containing the sizes of
  16. % each subband. This is compatible with the MatLab Wavelet toolbox.
  17. % Eero Simoncelli, 6/96, based on original lisp code from 1987.
  18. function [pyr,pind] = buildWpyr(im, ht, filt, edges)
  19. if (nargin < 1)
  20. error('First argument (IM) is required');
  21. end
  22. %------------------------------------------------------------
  23. %% OPTIONAL ARGS:
  24. if (exist('filt') ~= 1)
  25. filt = 'qmf9';
  26. end
  27. if (exist('edges') ~= 1)
  28. edges= 'reflect1';
  29. end
  30. if isstr(filt)
  31. filt = namedFilter(filt);
  32. end
  33. if ( (size(filt,1) > 1) & (size(filt,2) > 1) )
  34. error('FILT should be a 1D filter (i.e., a vector)');
  35. else
  36. filt = filt(:);
  37. end
  38. hfilt = modulateFlip(filt);
  39. % Stagger sampling if filter is odd-length:
  40. if (mod(size(filt,1),2) == 0)
  41. stag = 2;
  42. else
  43. stag = 1;
  44. end
  45. im_sz = size(im);
  46. max_ht = maxPyrHt(im_sz, size(filt,1));
  47. if ( (exist('ht') ~= 1) | (ht == 'auto') )
  48. ht = max_ht;
  49. else
  50. if (ht > max_ht)
  51. error(sprintf('Cannot build pyramid higher than %d levels.',max_ht));
  52. end
  53. end
  54. if (ht <= 0)
  55. pyr = im(:);
  56. pind = im_sz;
  57. else
  58. if (im_sz(2) == 1)
  59. lolo = corrDn(im, filt, edges, [2 1], [stag 1]);
  60. hihi = corrDn(im, hfilt, edges, [2 1], [2 1]);
  61. elseif (im_sz(1) == 1)
  62. lolo = corrDn(im, filt', edges, [1 2], [1 stag]);
  63. hihi = corrDn(im, hfilt', edges, [1 2], [1 2]);
  64. else
  65. lo = corrDn(im, filt, edges, [2 1], [stag 1]);
  66. hi = corrDn(im, hfilt, edges, [2 1], [2 1]);
  67. lolo = corrDn(lo, filt', edges, [1 2], [1 stag]);
  68. lohi = corrDn(hi, filt', edges, [1 2], [1 stag]); % horizontal
  69. hilo = corrDn(lo, hfilt', edges, [1 2], [1 2]); % vertical
  70. hihi = corrDn(hi, hfilt', edges, [1 2], [1 2]); % diagonal
  71. end
  72. [npyr,nind] = buildWpyr(lolo, ht-1, filt, edges);
  73. if ((im_sz(1) == 1) | (im_sz(2) == 1))
  74. pyr = [hihi(:); npyr];
  75. pind = [size(hihi); nind];
  76. else
  77. pyr = [lohi(:); hilo(:); hihi(:); npyr];
  78. pind = [size(lohi); size(hilo); size(hihi); nind];
  79. end
  80. end