Search code examples
matlabimage-processingnoise

Adding Impulse Noise in Image manually


How can salt & pepper noise be added to an image separately depending on the PROBABILITY of salt and pepper. imnoise would take ONE value for the noise density as a whole and that value is the measure for both salt (white dots) and pepper (black dots). I want to know if we have just to add white (salt) and then black (pepper) noise separately with two different probabilities. What equation would have to be used?


Solution

  • clc;
    close all;
    originalImage = imread('Cameraman.tif');
    [rows cols] = size(originalImage);
    totalPixels = int32(rows * cols);
    subplot(1, 2, 1);
    imshow(originalImage);
    percentage = str2double(cell2mat(inputdlg('Enter the percent noise: ', 'Enter answer', 1, {'2'}))) / 100.;
    numberOfNoisePixels = int32(percentage * double(rows) * double(cols));
    locations = randi(totalPixels, [numberOfNoisePixels, 1]);
    noisyImage = originalImage;
    noisyImage(locations) = 255;
    subplot(1, 2, 2);
    imshow(noisyImage, []);
    

    Source

    https://groups.google.com/forum/#!topic/comp.soft-sys.matlab/YcF2xZwnq1o

    That does Salt Noise, Pepper noise would be

    noisyImage(locations) = 0;
    

    instead of

    noisyImage(locations) = 255;