Adding salt and pepper noise to image

Viewed 246

I have to add salt and pepper noise in an image where the noise density of pepper is 0.1 and the noise density of salt is 0.2. How to do this in matlab

I know img = imnoise2(img,'salt & pepper',M,N,a,b) I know img is image M,N = size of image but between a and b which is the density of which is what's confusing me. Please help

1 Answers

Is this function the imnoise2 you are referring to? I tried it and it is not doing what it is supposed to do because of a mistake in the code (at line 75, R(c) = 1; should be replaced by R(c) = 0; because we are supposed to add pepper noise not salt). Modify this line in your imnoise2.m file and it should do the job now. Lines 70 to 75 should look like this:

    X = rand(M,N);
    c = find(X<=a);
    R(c) = 1;
    u = a + b;
    c = find(X > a & X <= u);
    R(c) = 0; % <---- In this line, 1 has been replaced by 0

Here is what I obtain after editing the code for a noise with 5% salt and 1% pepper:

>> sum(sum(imnoise2('salt & pepper',500,500,0.05,0.01)==1))/(500*500) % Probability of ones (salt)
ans =
    0.0502
>> sum(sum(imnoise2('salt & pepper',500,500,0.05,0.01)==0))/(500*500) % Probability of zeros (pepper)
ans =
    0.0098

Thus the first parameter a is salt and the second b is pepper. Don't hesitate if you have questions. Note that its not impossible that more mistakes are present in other types of noise (parameter type~='salt & pepper).

Related