Pregunta

How can salt & pepper noise be added to an image separately depending on the PROBABILITY of salt and pepper. imnoise would take ONE value for the noise density as a whole and that value is the measure for both salt (white dots) and pepper (black dots). I want to know if we have just to add white (salt) and then black (pepper) noise separately with two different probabilities. What equation would have to be used?

¿Fue útil?

Solución 2

clc;
close all;
originalImage = imread('Cameraman.tif');
[rows cols] = size(originalImage);
totalPixels = int32(rows * cols);
subplot(1, 2, 1);
imshow(originalImage);
percentage = str2double(cell2mat(inputdlg('Enter the percent noise: ', 'Enter answer', 1, {'2'}))) / 100.;
numberOfNoisePixels = int32(percentage * double(rows) * double(cols));
locations = randi(totalPixels, [numberOfNoisePixels, 1]);
noisyImage = originalImage;
noisyImage(locations) = 255;
subplot(1, 2, 2);
imshow(noisyImage, []);

Source

https://groups.google.com/forum/#!topic/comp.soft-sys.matlab/YcF2xZwnq1o

That does Salt Noise, Pepper noise would be

noisyImage(locations) = 0;

instead of

noisyImage(locations) = 255;

Otros consejos

img = .5*ones(100,200); %// example image
p_salt = .05; %// probability of salt
p_pepper = .01; %// probability of pepper

if strcmp(class(img),'uint8')
    salt_value = uint8(255);
else
    salt_value = 1;
end
pepper_value = 0;

aux = rand(size(img)); %// generate random values
img(aux<=p_salt) = salt_value;  %// add salt
img((aux>p_salt) & (aux<=p_salt+p_pepper)) = pepper_value; %// add pepper

imshow(img) %// show image

This approach is similar to that used in imnoise, and avoids salt and pepper to be added at the same pixels. It assumes p_salt + p_pepper is at most 1.

enter image description here

This code is simple and useful(Same imnoise in MATLAB)

im=imread('Parrot.jpg');
B=rgb2gray(im);
%if Pa==Pb;
percen=10;
%Noise level 10
Prob_den_f=255*percen/100;
NoiseImg = B;
Rmatrix = randint(size(B,1),size(B,2),[0,255]);
NoiseImg(Rmatrix <=Prob_den_f/2) = 0;
NoiseImg(Rmatrix >Prob_den_f/2&Rmatrix<Prob_den_f) =255;
subplot(1,2,2),imshow(NoiseImg),title('Add ''Salt and Pepper'' Noise'); 
subplot(1,2,1),imshow(B),title('Original Image'); 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top