手动添加图像中的脉冲噪声

时间:2013-12-22 14:02:36

标签: matlab image-processing noise

盐和盐怎么样?根据盐和胡椒的概率,胡椒噪声分别添加到图像中。 imnoise整体上将噪声密度取一个值,该值是盐(白点)和胡椒(黑点)的量度。我想知道我们是否只需要用两种不同的概率分别添加白色(盐)和黑色(胡椒)噪声。必须使用什么等式?

3 个答案:

答案 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

答案 1 :(得分: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;

答案 2 :(得分: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');