0

Hi I am a Matlab beginner, I would like to ask help about degrading an image file by adding in a certain amount of random noise data.

The percentage of the noise will be in a range of 0-100, indicating how the output will be:

0 will be no modification, 25 indicates that 75% of the image's content and 25% of noise, 75 indicates that 25% of the image's content and 75% noise, 100 would indicate the output should be the same size as the contents of the image, but containing all random noise and none of the original image data.

The following is my codes:

function out_image = image_plus_noise( in_image, percent_noise )
in_image = imread('sample_image.png');
image_proportion = 0.25;
percent_noise = rand(0:100);
my_percent_noise = uint8(percent_noise);
out_image = in_image{in_image}*image_proportion + percent_noise{my_percent_noise}*(1-image_proportion);
imshow(out_image)

However I can't run it correctly, there is a bug on line 6. Can anyone tell me what kinda bug I have? Am I on the right track?*

1 Answers1

0

A couple comments:

  • You're overwriting your input arguments percent_noise and in_image
  • rand(0:100) gives you 100 random values each on the range of 0 to 1. You probably don't need all of these.
  • You're trying to index matrices using {} instead of () in line 6. That won't work.
  • You need to define what you mean by "percent of the image's content." At each pixel? In terms of the maximum?

Here's one way to add some random noise to an image:

scaledImage = in_image * (1-percent_noise / 100); %scale image so
                                              %its maximum
                                              %value is some
                                              %percentage of
                                              %the original 
maxOfImage = max(in_image(:));
noiseImage = (percent_noise/100) * maxOfImage * rand(size(in_image)); %create noise
                                                      %image
                                                      %relative to
                                                      %the maximum
                                                      %of the
                                                      %original image
out_image = scaledImage + noiseImage; %add noise to original image. 

If you have access to the communications toolbox you could use the awgn function instead.

Molly
  • 13,240
  • 4
  • 44
  • 45