How can I use uint8 to handle negative number in Matlab?

  • Thread starter Thread starter NexusN
  • Start date Start date
  • Tags Tags
    Matlab Negative
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 8K views
NexusN
Messages
23
Reaction score
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.
 
Physics news on Phys.org
Exactly how do you want negative numbers to be shown in an image? Blacker than black?

Perhaps you should add some constant to the image so that the original image is around say 100 and then add nosie to that
 
NobodySpecial said:
Exactly how do you want negative numbers to be shown in an image? Blacker than black?

Perhaps you should add some constant to the image so that the original image is around say 100 and then add nosie to that

Thanks for the reply.
I think the asgn is not going to make me show a negative number on the image,
what it means should be adding a noise matrix of a value(positive and negative) to the image matrix to alter the "255" byte on each cell of the image to change the darkness.
Here is the difference between my output and the desired output:

effect.jpg


Size is not the issue, and the variance is set to the same.
 
If you want to add signed noise to an image the normal way is to add an offset to the image, eg 128 to every pixel.
If the original image used the full range (0-255) you will have to rescale eg. divide each pixel by 2 to reduce the range to 0-127 then add 128.
Then you can add upto +/- 127 of noise to each pixel.

Another approach is to convert the image to some other type (float, signed 16bit etc) do the noise and then convert back to unsigned 8 bit
 
NobodySpecial said:
If you want to add signed noise to an image the normal way is to add an offset to the image, eg 128 to every pixel.
If the original image used the full range (0-255) you will have to rescale eg. divide each pixel by 2 to reduce the range to 0-127 then add 128.
Then you can add upto +/- 127 of noise to each pixel.

Another approach is to convert the image to some other type (float, signed 16bit etc) do the noise and then convert back to unsigned 8 bit

Thank you for your help,
I have just tried the method of first convert it to double followed by introduction of noise and then back to uint8 for image, but it seems there are still too much noise.
I think I should contact my tutors to see if this is a normal case.