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

  • Thread starter Thread starter NexusN
  • Start date Start date
  • Tags Tags
    Matlab Negative
Click For Summary

Discussion Overview

The discussion revolves around the challenge of adding normal random noise to an image in Matlab while using the uint8 data type, which does not support negative values. Participants explore methods to incorporate negative noise values without losing the integrity of the image data.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to add noise to a grayscale image using uint8, noting that negative values in the noise matrix are converted to zeros, which is not the desired outcome.
  • Another participant questions how negative numbers would be represented in an image, suggesting that adding a constant to the image could help maintain the desired pixel values.
  • A different participant proposes adding an offset to the image to allow for signed noise, recommending a rescaling method to accommodate the full range of pixel values.
  • Some participants suggest converting the image to a different data type (like float or signed 16-bit) to handle the noise before converting it back to uint8.
  • One participant mentions trying to convert the image to double before adding noise and then back to uint8, but still experiences excessive noise, indicating ongoing issues with the approach.

Areas of Agreement / Disagreement

There is no consensus on the best method to handle negative noise values in uint8 images. Multiple competing views and approaches are presented, with participants offering different strategies and expressing uncertainty about the effectiveness of their solutions.

Contextual Notes

Participants express limitations related to the representation of negative values in uint8 and the challenges of maintaining image quality while adding noise. The discussion highlights unresolved mathematical steps and the need for further clarification on the assignment requirements.

Who May Find This Useful

This discussion may be useful for students and practitioners working with image processing in Matlab, particularly those dealing with data type constraints and noise addition in digital images.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K