フィルターのクリア

How to make salt pepper noise own code

17 ビュー (過去 30 日間)
Ali Umur Kucur
Ali Umur Kucur 2020 年 4 月 22 日
回答済み: DGM 2022 年 4 月 22 日
After creating a matrix with the for loop, how can we assign the values 0 and 255 in the picture and add salt and pepper noise?

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 22 日
編集済み: Ameer Hamza 2020 年 4 月 22 日
Try this
im = imread('pears.png');
figure;
ax1 = axes();
imshow(im);
title(ax1, 'original');
a = 0.1; % 10% pixels altered
b = 0.5; % 50% percent white pixels among all altered pixels
n = numel(im(:,:,1));
m = fix(a*n);
idx = randperm(n, m);
k = fix(b*m);
idx1 = idx(1:k);
idx2 = idx(k+1:end);
idx1 = idx1' + n.*(0:size(im,3)-1);
idx1 = idx1(:);
idx2 = idx2' + n.*(0:size(im,3)-1);
idx2 = idx2(:);
im(idx1) = 255;
im(idx2) = 0;
figure;
ax2 = axes();
imshow(im);
title(ax2, 'noisy');
  4 件のコメント
Image Analyst
Image Analyst 2020 年 4 月 23 日
Ali, did you try my solution (or even see it below):
noisyImage = imnoise(originalImage,'salt & pepper', 0.05); % Or whatever percentage you want.
It's a lot simpler since it uses the built-in function.
Ali Umur Kucur
Ali Umur Kucur 2020 年 4 月 23 日
Thank you for your answer, but I have to write this salt and pepper noise with my own code, not with the ready function.

サインインしてコメントする。

その他の回答 (4 件)

David Welling
David Welling 2020 年 4 月 22 日
An easy way to do this is create a salt and pepper noise image to lay in front of the original image. So you need a way to randomly select pixels to make white. This can easily be done by creating a matrix the same size as your picture, filled with random numbers, and then select a cut off point above which you make pixels white, like this:
floor(rand(1000,1000)+0.01)*255; %array of 1000x1000, with approximately 1 percent white pixels. this can be adjusted by changing the 0.01 in the equation
  1 件のコメント
Ali Umur Kucur
Ali Umur Kucur 2020 年 4 月 22 日
thank you for answer.

サインインしてコメントする。


Image Analyst
Image Analyst 2020 年 4 月 22 日
The easiest way is to use the built-in imnoise() function:
noisyImage = imnoise(originalImage,'salt & pepper', 0.05); % Or whatever percentage you want.
  2 件のコメント
Ali Umur Kucur
Ali Umur Kucur 2020 年 4 月 23 日
Thank you for your answer, but I have to write this salt and pepper noise with my own code, not with the ready function.
Image Analyst
Image Analyst 2020 年 4 月 23 日
Why? It's not labeled as homework. If it is your assignment and you turned in Ameer's code as your own, then you could run into trouble with your teacher and institution (possibly cheating). In the future, tag homework with the homework tag so people don't give you complete solutions that will get you into trouble.

サインインしてコメントする。


Mykola Ponomarenko
Mykola Ponomarenko 2021 年 9 月 4 日
function [ima,map] = salt_and_pepper(ima, prob)
% ima - grayscale or color input image; prob - probability of salt&pepper noise (0..1)
[y,x,z]=size(ima);
map=repmat(rand(y,x)<prob, [1 1 z]);
sp=repmat(round(rand(y,x))*255, [1 1 z]);
ima(map)=sp(map);
end

DGM
DGM 2022 年 4 月 22 日
This is the way that MIMT imnoiseFB() does it when in fallback mode. This will replicate the behavior of IPT imnoise(). Note that this works regardless of the class of the input image.
inpict = imread('cameraman.tif');
snpdensity = 0.05; % default for imnoise()/imnoiseFB()
s0 = size(inpict);
noisemap = rand(s0);
outpict = im2double(inpict);
mk1 = noisemap < (snpdensity/2);
outpict(mk1) = 0;
outpict(~mk1 & (noisemap < snpdensity)) = 1;
imshow(outpict)

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by