buildGpyr.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. % [PYR, INDICES] = buildGpyr(IM, HEIGHT, FILT, EDGES)
  2. %
  3. % Construct a Gaussian pyramid on matrix IM.
  4. %
  5. % HEIGHT (optional) specifies the number of pyramid levels to build. Default
  6. % is 1+maxPyrHt(size(IM),size(FILT)).
  7. % You can also specify 'auto' to use this value.
  8. %
  9. % FILT (optional) can be a string naming a standard filter (see
  10. % namedFilter), or a vector which will be used for (separable)
  11. % convolution. Default = 'binom5'. 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.
  18. function [pyr,pind] = buildGpyr(im, ht, filt, edges)
  19. if (nargin < 1)
  20. error('First argument (IM) is required');
  21. end
  22. im_sz = size(im);
  23. %------------------------------------------------------------
  24. %% OPTIONAL ARGS:
  25. if (exist('filt') ~= 1)
  26. filt = 'binom5';
  27. end
  28. if isstr(filt)
  29. filt = namedFilter(filt);
  30. end
  31. if ( (size(filt,1) > 1) & (size(filt,2) > 1) )
  32. error('FILT should be a 1D filter (i.e., a vector)');
  33. else
  34. filt = filt(:);
  35. end
  36. max_ht = 1 + maxPyrHt(im_sz, size(filt,1));
  37. if ( (exist('ht') ~= 1) | (ht == 'auto') )
  38. ht = max_ht;
  39. else
  40. if (ht > max_ht)
  41. error(sprintf('Cannot build pyramid higher than %d levels.',max_ht));
  42. end
  43. end
  44. if (exist('edges') ~= 1)
  45. edges= 'reflect1';
  46. end
  47. %------------------------------------------------------------
  48. if (ht <= 1)
  49. pyr = im(:);
  50. pind = im_sz;
  51. else
  52. if (im_sz(2) == 1)
  53. lo2 = corrDn(im, filt, edges, [2 1], [1 1]);
  54. elseif (im_sz(1) == 1)
  55. lo2 = corrDn(im, filt', edges, [1 2], [1 1]);
  56. else
  57. lo = corrDn(im, filt', edges, [1 2], [1 1]);
  58. lo2 = corrDn(lo, filt, edges, [2 1], [1 1]);
  59. end
  60. [npyr,nind] = buildGpyr(lo2, ht-1, filt, edges);
  61. pyr = [im(:); npyr];
  62. pind = [im_sz; nind];
  63. end