2013-12-22 36 views
0

根据盐和胡椒的概率,如何将椒盐噪声分别添加到图像中。 imnoise作为一个整体的噪声密度将取一个值,该值是盐(白点)和胡椒(黑点)的测量值。我想知道如果我们只是分别用两种不同的概率添加白色(盐)和黑色(胡椒)噪音。必须使用什么等式?手动添加图像中的脉冲噪声

回答

1
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, []); 

来源

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

那请问盐噪声,椒盐噪声会

noisyImage(locations) = 0; 

,而不是

noisyImage(locations) = 255; 
+0

只是一个问题,我打正确的前)。噪音的百分比和噪音的“概率”。在图像处理中这两个相同的术语? – learner

+0

不完全,但足够接近。 %是一个固定的项,所以0.05%将意味着始终有0.05 *总像素。概率将会接近0.05 *总像素。随着像素增加,这两个术语将开始意味着相同的东西 – exussum

+0

这种方法不会阻止在相同的像素处添加盐和噪声(可能需要或不需要) –

3
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 

该方法与imnoise中使用的方法相似,避免了在相同像素处添加盐和胡椒粉。它假定p_salt + p_pepper至多1

enter image description here

+0

根据强度会对此代码有任何影响值在0-255或0-1之间? – learner

+0

@LandonAshes更正。感谢您指出 –

0

此代码是简单而有用的(在MATLAB同imnoise)

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'); 
+0

请添加说明,而不是仅发布代码。 –