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

In summary: 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.
  • #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.
 
Physics news on Phys.org
  • #2
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
 
  • #3
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.
 
  • #4
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
 
  • #5
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.
 

1. How do I convert a negative number to uint8 in Matlab?

To convert a negative number to uint8 in Matlab, you can use the function int8 to convert the number to a signed 8-bit integer, and then use the function uint8 to convert it to an unsigned 8-bit integer. For example, if your negative number is -5, you can use uint8(int8(-5)) to convert it to an unsigned 8-bit integer.

2. Can I store negative numbers in a uint8 variable in Matlab?

No, a uint8 variable in Matlab can only store unsigned 8-bit integers, which range from 0 to 255. Trying to store a negative number in a uint8 variable will result in an error.

3. How do I perform arithmetic operations on negative numbers using uint8 in Matlab?

If you want to perform arithmetic operations on negative numbers using uint8 in Matlab, you will first need to convert the negative numbers to signed 8-bit integers using the int8 function. After performing the operation, you can then convert the result back to an unsigned 8-bit integer using the uint8 function.

4. Can I use uint8 to handle negative numbers in Matlab without converting them to signed integers?

No, uint8 is an unsigned 8-bit integer data type in Matlab, which means it can only store positive numbers. To handle negative numbers using uint8, you will need to convert them to signed integers first.

5. Will converting a negative number to uint8 in Matlab change its value?

Yes, converting a negative number to uint8 in Matlab will change its value. The uint8 function will convert the negative number to its 8-bit binary representation, resulting in a different value. For example, int8(-5) is equal to 11111011 in binary, but uint8(int8(-5)) is equal to 251 in decimal.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top