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

图形图像处理

开发平台:

Matlab

  1. % makenoise.m
  2. % written by: Duncan Po
  3. % Date: August 24, 2002
  4. % Generate additive Gaussian noise and add the noise to an image
  5. % This function assumes that the image is stored in uint8 format with
  6. % values from 0 to 255
  7. % Usage: noisepic = makenoise(imname, imformat, nvar)
  8. % Inputs:   imname      - name of the image file
  9. %           imformat    - format of the image file
  10. %           nvar        - variance of the noise normalized to the image range
  11. % Output:   noisepic    - the resulting noisy image
  12. function noisepic = makenoise(imname, imformat, nvar)
  13. pic = imread(imname,imformat);
  14. figure;
  15. imshow(pic);
  16. pic = double(pic);
  17. % the following step is needed for the command imnoise (normalize)
  18. pic2 = pic/255;
  19. % adds zero mean Gaussian noise of power (0.1*255)^2 = 650.25
  20. noisepic = imnoise(pic2, 'Gaussian', 0, nvar);
  21. % converts the image back to a scale of 8 bits (0 to 255)
  22. noisepic = noisepic*255;
  23. % calculate initial MSE of the noisy image
  24. MSE = sqrt(sum(sum((noisepic - pic).*(noisepic-pic)/(size(pic,1)*size(pic,2))...
  25.     /(size(pic,1)*size(pic,2)))))
  26. noisepic = uint8(noisepic);
  27. figure;
  28. imshow(noisepic);