buildLpyr.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. % [PYR, INDICES] = buildLpyr(IM, HEIGHT, FILT1, FILT2, EDGES)
  2. %
  3. % Construct a Laplacian pyramid on matrix (or vector) IM.
  4. %
  5. % HEIGHT (optional) specifies the number of pyramid levels to build. Default
  6. % is 1+maxPyrHt(size(IM),size(FILT)). You can also specify 'auto' to
  7. % use this value.
  8. %
  9. % FILT1 (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'. FILT2 specifies the "expansion"
  12. % filter (default = filt1). EDGES specifies edge-handling, and
  13. % defaults to 'reflect1' (see corrDn).
  14. %
  15. % PYR is a vector containing the N pyramid subbands, ordered from fine
  16. % to coarse. INDICES is an Nx2 matrix containing the sizes of
  17. % each subband. This is compatible with the MatLab Wavelet toolbox.
  18. % Eero Simoncelli, 6/96.
  19. function [pyr,pind] = buildLpyr(im, ht, filt1, filt2, edges)
  20. if (nargin < 1)
  21. error('First argument (IM) is required');
  22. end
  23. im_sz = size(im);
  24. %------------------------------------------------------------
  25. %% OPTIONAL ARGS:
  26. if (exist('filt1') ~= 1)
  27. filt1 = 'binom5';
  28. end
  29. if isstr(filt1)
  30. filt1 = namedFilter(filt1);
  31. end
  32. if ( (size(filt1,1) > 1) & (size(filt1,2) > 1) )
  33. error('FILT1 should be a 1D filter (i.e., a vector)');
  34. else
  35. filt1 = filt1(:);
  36. end
  37. if (exist('filt2') ~= 1)
  38. filt2 = filt1;
  39. end
  40. if isstr(filt2)
  41. filt2 = namedFilter(filt2);
  42. end
  43. if ( (size(filt2,1) > 1) & (size(filt2,2) > 1) )
  44. error('FILT2 should be a 1D filter (i.e., a vector)');
  45. else
  46. filt2 = filt2(:);
  47. end
  48. max_ht = 1 + maxPyrHt(im_sz, max(size(filt1,1), size(filt2,1)));
  49. if ( (exist('ht') ~= 1) | (ht == 'auto') )
  50. ht = max_ht;
  51. else
  52. if (ht > max_ht)
  53. error(sprintf('Cannot build pyramid higher than %d levels.',max_ht));
  54. end
  55. end
  56. if (exist('edges') ~= 1)
  57. edges= 'reflect1';
  58. end
  59. %------------------------------------------------------------
  60. if (ht <= 1)
  61. pyr = im(:);
  62. pind = im_sz;
  63. else
  64. if (im_sz(2) == 1)
  65. lo2 = corrDn(im, filt1, edges, [2 1], [1 1]);
  66. elseif (im_sz(1) == 1)
  67. lo2 = corrDn(im, filt1', edges, [1 2], [1 1]);
  68. else
  69. lo = corrDn(im, filt1', edges, [1 2], [1 1]);
  70. int_sz = size(lo);
  71. lo2 = corrDn(lo, filt1, edges, [2 1], [1 1]);
  72. end
  73. [npyr,nind] = buildLpyr(lo2, ht-1, filt1, filt2, edges);
  74. if (im_sz(1) == 1)
  75. hi2 = upConv(lo2, filt2', edges, [1 2], [1 1], im_sz);
  76. elseif (im_sz(2) == 1)
  77. hi2 = upConv(lo2, filt2, edges, [2 1], [1 1], im_sz);
  78. else
  79. hi = upConv(lo2, filt2, edges, [2 1], [1 1], int_sz);
  80. hi2 = upConv(hi, filt2', edges, [1 2], [1 1], im_sz);
  81. end
  82. hi2 = im - hi2;
  83. pyr = [hi2(:); npyr];
  84. pind = [im_sz; nind];
  85. end