Clear Filters
Clear Filters

i'm confused about the function imnoise

7 views (last 30 days)
I debuged the function imnoise to add Gaussian noise. I entered following code
f = zeros(255);
noisedImg = imnoise(f, 0, 0.01);
Then I looked at the noisedImg data and found that all pixel values were greater than or equal to zero. I mean that pixel values should be distributed nearly to zero, some are greater than zero and others may be less than zero. I'm puzzled that why there are no pixel values less than zero.

Accepted Answer

Image Analyst
Image Analyst on 2 Apr 2016
I guess because it expects the output to be the same type as the input, and imnoise expects that if a double is being passed in it has to have a range of 0-1, so it clips anything less than 0 to 0. You would have to use randn() if you wanted negative values in a floating point image. Here is the corrected code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
originalImage = zeros(255);
noisyImage = imnoise(originalImage, 'Gaussian', 0, 0.01);
% Display the image.
subplot(2, 2, 1);
imshow(noisyImage, []);
title('Noisy Image with imnoise()', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
subplot(2, 2, 2);
histogram(noisyImage);
grid on;
title('Histogram of noisy image from imnoise()', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
noisyImage = originalImage + 0.01 * randn(size(originalImage));
% Display the image.
subplot(2, 2, 3);
imshow(noisyImage, []);
title('Noisy Image with randn()', 'FontSize', fontSize, 'Interpreter', 'None');
% Let's compute and display the histogram.
subplot(2, 2, 4);
histogram(noisyImage);
grid on;
title('Histogram of noisy image with randn()', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
  1 Comment
li yan
li yan on 3 Apr 2016
Thank you very much. Picture 4(bottom right) is the resulti want.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 2 Apr 2016
That is not a supported syntax for imnoise, which requires that you pass a string indicating the kind of noise to add.
  1 Comment
li yan
li yan on 3 Apr 2016
yes, I missed out the noise type gaussian when I put the code here.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!