# Adding Salt n Pepper noise into an Image manually

When i looked in to the algorithm of Salt n Pepper noise in a Matlab i've found this,

case 'salt & pepper' % Salt & pepper noise
b = a;
x = rand(sizeA);
d = find(x < p3/2);
b(d) = 0; % Minimum value
d = find(x >= p3/2 & x < p3);
b(d) = 1; % Maximum (saturated) value


Which i simplified as this,

% [a, code, classIn, classChanged, p3, p4]
% p3 = density
% p4 = variance
% a = Image
% code = noise Type
% classIn = Image Class i.e unit8 , unit16, double


% -------- ALGORITHM ----------%

%     b = a;   <-- Assign b to the input image
%     x = rand(sizeA); <--- Generate random pixels from the image pixels
%     d = find(x < p3/2); <--- Find the pixels whose values are less than half of the mean value
%     b(d) = 0 <-- Implement minimum noise to them
%     d = find(x >= p3/2 & x < p3) <--- Find the pixels whose values are
%                                        greater than half of the mean
%                                        value & less than mean value
%     b(d) = 1;     <-- Implement maximum noise to them


instead of using imnoise, I am trying to add Salt n Pepper noise manually into the image using the above algorithm,

I = imread('2.jpg');
J = rgb2gray(I);
p3=0.05 %default
b = J;
x = rand(size(b));
d = find(x < p3/2);
b(d) = 0; % Minimum value
d = find(x >= p3/2 & x < p3);
b(d) = 1; % Maximum (saturated) value


I don't know where the final image is stored as when i use imshow for b it show a simple image without salt n pepper noise. Where the final image is stored ? how can i see the it ?

P.S: What does it mean by image class ? that is unit8 , unit16 ?

• b(d) = 255; % Maximum (saturated) value This worked for me . – user4838 Jun 17 '13 at 18:17