Background

Salt and pepper noise was present in one of the noisy images from Laboratory 10a, and we were tasked with removing this noise by filtering. However, this page will demonstrate the opposite - how to create this kind of noise. Here is an example of salt and pepper noise from Laboratory 10a:

Example of salt and pepper noise


Overview

First, we will start with an image. For simplicity purposes, we will use another image from Laboratory 10a (this time of boats).

Two problems arise when trying to create the noise for a salt and pepper effect. Which pixels are to be changed with noise? How are these pixels changed?

To solve the first problem, a random number is generated between 1 and a final value. If the number is the final value, then the pixel will be changed with noise. If the final number is larger, fewer pixels will be changed. As the number decreases, more pixels will be changed, thus making a noisier picture.

To determine how the pixel is changed, a random number is generated between 1 and 256 (max for grayscale values). This algorithm is implemented when the given pixel is noted to be changed. Instead of the original value of the pixel, it is replaced by the random number between 1 and 256.

By randomizing the noise values, the pixels can change to a white, black, or gray value, thus adding the salt and pepper colors. By randomizing which pixels are changed, the noise is scattered throughout the image. The combination of these randomizations creates the "salt and pepper" effect throughout the image. Examples using various degrees of noise are displayed below in the "Pictures" section.


Pictures

Original image


Low amount of noise - 1% of pixels are changed


Medium amount of noise - 5% of pixels are changed


High amount of noise - 20% of pixels are changed


MATLAB Implementation

%loads the image and makes double precision representation for computations

A = imread('yacht.tif');

B = double(A);

[rows, columns] = size(B); %computes the dimensions of the image


%displays the original image with appropriate title

figure(1)

image(B);

colormap(gray(256));

axis('image');

title('Original image')


%makes a copy of the original image to be salted/peppered with noise

noisy_image = B;

noise_percent = 20;


for i = 1:rows %for loops iterate through every pixel

   for j = 1:columns
       noise_check = randi(noise_percent); %creates a random number between 1 and noise_percent
       if noise_check == noise_percent    %if the random number = noise_percent (1/noise_percent chance of any given pixel being noisy)
           noise_value = randi(256);    %creates a random noise value to replace the pixel
           noisy_image(i,j) = noise_value; %replaces the original pixel value with the random noise
       end
   end

end


%displays the noisy image with appropriate title

figure(2)

image(noisy_image);

colormap(gray(256));

axis('image');

title('Salt and pepper noise image')

Alumni Liaison

Prof. Math. Ohio State and Associate Dean
Outstanding Alumnus Purdue Math 2008

Jeff McNeal