Adding noise into an image manually instead of using imnoise

조회 수: 85 (최근 30일)
Sufyan
Sufyan 2012년 8월 30일
댓글: Mintesnot 2022년 9월 29일
I am to trying to understand the algorithms behind matlab way of adding noise into an image,
The algorithm which Matlab use to add Gaussian noise is this,
b = a + sqrt(p4)*randn(sizeA) + p3;
When I tried to implement this algorithm manually it worked successfully however it doesn't work unless i changed the image class to double. Why is that so ? Why should i suppose to change the class to double when adding gaussian noise ?
Here is my code,
I = imread('2.jpg');
J = rgb2gray(I);
p3= 0;
p4 = 0.05;
J = im2double(J);
b = J + sqrt(p4)*randn(size(J)) + p3;
imshow(b)
Just like Gaussian Noise i tried adding the Salt n Pepper noise manually, here is the algorithm Matlab use to add Salt n Pepper noise,
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 saturation 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 saturation to them
and I implemented it as below,
I = imread('2.jpg');
J = rgb2gray(I);
p3= 0.5;
x = rand(size(J));
d = find(x < p3/2);
J(d) = 0; % Minimum value
d = find(x >= p3/2 & x < p3);
J(d) = 1; % Maximum (saturated) value
imshow(J)
but the output Image doesn't show any Salt n pepper noise in the Image , I wonder where the final image is actually stored in my code?
  댓글 수: 3
sravani honey
sravani honey 2018년 4월 20일
sir, i am doing my project on random impulse noise removal and i am adding random noise using noisyimage = (I + p*rand(size(I)))/(1+p) this function but it doesn't clear results on my project. can u please give any other command related to adding random impulse noise to the image?
Mintesnot
Mintesnot 2022년 9월 29일
how to culculate the percent of noise in an image.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 8월 30일
Images read by imread() are often data class uint8(). The uint8() property will be retained in the rgb2gray() step. Your saturation assignments of 0 and 1 are, however, based upon the notion that the minimum and maximum are 0 and 1 instead of 0 and 255.
Also, the minimum value that a uint8 variable can be is 0, so there is no need to explicitly set 0 as the minimum.
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 8월 31일
MATLAB uses different value ranges for the different data classes of images. Images that are represented as uint8(), use the value range 0 to 255. Images that are represented as floating point, use the value range 0 to 1. Your difficulty is that you are trying to apply an algorithm that depends upon the 0 to 1 value range, to data that has been represented with the 0 to 255 value range.
It is not because of grayscale: you can have grayscale in uint8 or in floating point.
Please look at the routine im2double()

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2012년 8월 31일
Gaussian noise can have any magnitude. So how are you going to add 4.28435435 to an image that can have only integer values? If you could and then it was converted to an integer, you'd lose the fractional part of the number, which means it would no longer be Gaussian. So that's why you have to convert the image to floating point.
  댓글 수: 3
Image Analyst
Image Analyst 2012년 8월 31일
No. Gaussian noise can be any floating point value. However if you force it to be in a uint8 image you are not letting it be any value, and so it's not Gaussian noise anymore. So to prevent that, you have to convert your image to floating point (single or double).
AMIR KHAN
AMIR KHAN 2021년 8월 29일
very helpful, thanks!

댓글을 달려면 로그인하십시오.


Ameerah Omar
Ameerah Omar 2015년 11월 21일
how i can add noise in image it come from Cauchy distribution???
  댓글 수: 1
Image Analyst
Image Analyst 2015년 11월 21일
Use the formula for the CDF and then use rand() to draw numbers from the distribution. Attached is an example where I do that for the Rayleigh distribution.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by