- #1
NexusN
- 29
- 0
Homework Statement
This maybe a silly question to ask, but it did spend the silly me a night googling with no luck.
I am doing a Matlab asgn that I need to add some normal random noise to an image.
Homework Equations
Here is my code:
---------------------------------------------------------------------------------------
I=imread('C:\Study\Sem3\ENGG 2310B\Matlab\Workspace\lenna.bmp');
J=rgb2gray(I);
vari=0.1;
figure, imshow(I), figure, imshow(J);
vvvvvvvvvvvvvvv problems are here vvvvvvvvvvvvvvvvvvvvvvvv
Noise=uint8(randn(220,220)*255*sqrt(vari));
^^^^^^^^^^^problems are here^^^^^^^^^^^^^^^^^^
for m=1:1:220;
for n=1:1:220;
K(m,n)=J(m,n)+Noise(m,n);
end
end
figure, imshow(K);
-------------------------------------------------------------------------------------
Doing is this way, all the negative terms in the martix Noise will be turned to zeros, which is not the desired negative integers.
I tried to use other data types like double and int8, but they don't seem to be fitting the need of creating a bmp picture.
I then tried the following method(only the part of codes considering the problem above):
The Attempt at a Solution
PreNoise=randn(220,220)*255*sqrt(vari);
for m=1:220;
for n=1:220;
while (PreNoise(m,n) < 0)
PreNoise(m,n)=randn*255*sqrt(vari);
end
end
end
Noise=uint8(PreNoise);
However, looking at the output image, this doesn't look like the way I want.
Would you mind telling me the method for using uint8 with negative numbers?
Thank you in advance.