pdfbdenoiseimage.m
上传用户:l56789
上传日期:2022-02-25
资源大小:2422k
文件大小:4k
源码类别:

图形图像处理

开发平台:

Matlab

  1. % pdfbdenoiseimagethmt.m
  2. % written by: Duncan Po
  3. % Date: January 14, 2004
  4. % Clean the noisy image using the model and state probabilities of the
  5. % noisy image. The image is assumed to be square.
  6. % Usage: cleanimage = pdfbdenoiseimage( model, stateprob, nvar, nc, imname, imformat)
  7. % Inputs:   model       - model of the noisy image
  8. %           stateprob   - state probabilities of the noisy image
  9. %           nvar        - noise variance, normalized to the image range
  10. %                           (ie. should be between 0 and 1)
  11. %           nc          - noise variance in contourlet domain. Input '' if
  12. %                           unknown
  13. %           imname      - name of the image file
  14. %           imformat    - format of the image file (e.g. 'gif')
  15. % Output:   cleanimage  - the denoised image
  16. function cleanimage = pdfbdenoiseimage( model, stateprob, nvar, nc, imname, imformat)
  17. pyrfilter = '9-7';
  18. dirfilter = 'pkva';
  19. nlevel = model{1}.nlevels;
  20. for lev = 1:nlevel
  21.     levndir(lev) = log2(length(model{1}.stdv{lev})*length(model));
  22. end;
  23. if nargin == 6
  24.     coef = contourlet(pyrfilter, dirfilter, levndir, imname, imformat);
  25. elseif nargin == 5
  26.     coef = contourlet(pyrfilter, dirfilter, levndir, imname);
  27. end;
  28. % Verify image is square and obtain image dimensions
  29. [nrow, ncol] = size(coef{2}{1});
  30. imagestats = imfinfo(imname, imformat);
  31. imdim = imagestats.Width;
  32. if imdim ~= imagestats.Height
  33.     error('Image must be square.');
  34. end
  35. for state = 1:size(stateprob{1}{1},2)
  36.     for lev = 1:nlevel+1
  37.         newstateprob{state}{lev} = [];
  38.         covariance{state}{lev} = [];
  39.     end;
  40. end;
  41. % process stateprob, and covariance so that it has the same structure as coef
  42. for state = 1:size(stateprob{1}{1},2)
  43.     for dir = 1:2.^levndir(1)
  44.         for scale = 1:length(stateprob{dir})
  45.             tempsp{scale} = stateprob{dir}{scale}(:,state);
  46.         end;
  47.         tempstateprob = tree2contourlet(tempsp, dir, levndir, nrow, ncol);
  48.         
  49.         for lev = 1:nlevel
  50.             newstateprob{state}{lev+1} = [newstateprob{state}{lev+1} tempstateprob{lev}];
  51.             for subdir = 1:length(model{dir}.stdv{lev})
  52.                covariance{state}{lev+1} = [covariance{state}{lev+1} ...
  53.                      {ones(size(coef{lev+1}{(dir-1)*(2.^(levndir(lev)-levndir(1)))+subdir},1),...
  54.                         size(coef{lev+1}{(dir-1)*(2.^(levndir(lev)-levndir(1)))+subdir},2))*...
  55.                         model{dir}.stdv{lev}{subdir}(state)*model{dir}.stdv{lev}{subdir}(state)}];
  56.             end;
  57.         end;
  58.     end;
  59. end;
  60. % calculates the noise variance in contourlet domain
  61. if isempty(nc)
  62.     nc = contournc(nvar, pyrfilter, dirfilter, levndir, imdim);
  63. end;
  64. % computes the attenuator for each coefficient and state
  65. for state = 1:length(covariance)
  66.    for lev = 2:nlevel+1
  67.       for dir = 1:length(covariance{state}{lev})
  68.          [covrow, covcol] = size(covariance{state}{lev}{dir});
  69.          covariance{state}{lev}{dir}=max(covariance{state}{lev}{dir}...
  70.             -nc{lev}{dir}, zeros(covrow, covcol))./covariance{state}{lev}{dir};
  71.       end;
  72.    end;
  73. end;
  74. % multiplies each contourlet coefficient with its corresponding state probabilities
  75. for state = 1:length(newstateprob)
  76.    for lev = 2:nlevel+1
  77.       for dir = 1:length(newstateprob{state}{lev})
  78.          newcoef{state}{lev}{dir} = newstateprob{state}{lev}{dir}...
  79.             .*coef{lev}{dir};
  80.       end;
  81.    end;
  82. end;
  83. % we don't denoise the scaling function since its SNR is high
  84. cleancoef{1} = coef{1};
  85. % initialize the clean coefficients
  86. for lev = 2:nlevel+1
  87.     for dir = 1:length(newstateprob{state}{lev})
  88.         cleancoef{lev}{dir} = zeros(size(coef{lev}{dir},1), size(coef{lev}{dir},2));
  89.     end;
  90. end;
  91.           
  92. % denoise
  93. for state = 1:length(newstateprob)
  94.    for lev = 2:nlevel+1
  95.       for dir = 1:length(newstateprob{state}{lev})
  96.          cleancoef{lev}{dir}=cleancoef{lev}{dir}+covariance{state}{lev}{dir}...
  97.             .*newcoef{state}{lev}{dir};
  98.       end;
  99.    end;
  100. end;
  101. cleanimage = pdfbrec(cleancoef, pyrfilter, dirfilter);
  102. figure;
  103. imshow(uint8(cleanimage));