Adding Specific Ratio of Noise to an Image

5 views (last 30 days)
How can I adding a noise (e.g 75% of gaussian noise) to an Image?
In (imnoise) function there is only the (mean & variance), I dont want to add (e.g gaussian noise with m = 0 & standard deviation of 75%), I want to add (e.g 75% of image or elements have noise and 25% don't). how can I do that?

Accepted Answer

DGM
DGM on 25 Jul 2022
Edited: DGM on 25 Jul 2022
The 'salt & pepper' option is the only option with a density parameter. Salt & Pepper noise will affect a specified fraction of the pixels in the image by slamming them to black/white.
inpict = imread('cameraman.tif');
outpict = imnoise(inpict,'salt & pepper',0.75);
imshow(outpict)
If you're trying to restrict some other type of noise by using a density parameter, then you'll have to write that yourself For example, if you wanted gaussian noise with a restricted density:
inpict = imread('cameraman.tif'); % a grayscale image
density = 0.75; % density
gaumean = 0; % mean
gauvar = 0.01; % variance
s0 = size(inpict);
inpict = im2double(inpict);
gnpict = inpict + gaumean + sqrt(gauvar)*randn(s0);
noisemask = rand(s0)<density;
nnz(noisemask)/numel(noisemask) % check the density
ans = 0.7504
outpict = inpict;
outpict(noisemask) = gnpict(noisemask);
imshow(outpict)
Bear in mind that doing this density restriction alters the noise variance. If you want to specify the final variance instead of the variance of the image prior to sampling, you'll have to take that into account.
% adjust variance to compensate for density
gnpict = inpict + gaumean + sqrt(gauvar/density)*randn(s0);
  4 Comments
DGM
DGM on 26 Jul 2022
I'm not really sure how you're trying to do this. If you're working with integer images, you could indeed pick a variance that would result in an approximate fraction of pixels being unaffected without the need for masking:
inpict = imread('cameraman.tif');
v = 0.000038;
outpict = imnoise(inpict,'gaussian',0,v);
nnz(outpict~=inpict)/numel(inpict) % fraction of altered pixels
ans = 0.7510
imshow(outpict)
This only really works because the image is quantized. Noise is still applied to every pixel during the process; it's just that (in this example) ~25% of the time, the noise is smaller than 1 LSB, so those pixels don't change. If this is the desired method, then the amount of achievable noise will be very small, and will be dependent on the image class.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by