Noise Cleaning Techniques for Images with MATLAB Implementation

Noise Cleaning refers to removal of noise from an image etc,. In this article, noise cleaning using box filter, gaussian filter, unnormalized minimum variance weight mask, equal variance correlation mask and peak noise deletion for images are discussed with their MATLAB programs.

Noise Cleaning

When an image etc., is transferred from a source to destination, some unwanted variations (additive or multiplicative to the original pattern) occur. These unwanted variations are called noise. Removing noise from an image etc., is called noise cleaning. It is the first stage of image recognition methodology. Now, let us discuss about some of the noise cleaning techniques for images (by using box filter, gaussian filter, unnormalized minimum variance weight mask, equal variance correlation mask and peak noise deletion) and their corresponding MATLAB programs.

Peak Noise Deletion

This method performs noise cleaning as its name says i.e. by removing the peak noise. First, the mean pixel value of the image is calculated. A variable 'deviation' is taken such that if any pixel value of the image varies from the mean pixel value by more than 'deviation', it is replaced by the mean pixel value. Hence, the image is noise cleaned. Now, let us implement this technique using MATLAB.

Program

`a = imread('PATH');a = rgb2gray(a);[m n] = size(a);b = a;mean = sum(sum(a)) / (m * n);deviation = input('Enter deviation value :');for i = 1 : mfor j = 1 : nif((abs(mean - b(i, j)) > deviation)b(i, j) = mean;endendendimshow(a);figure,imshow(b);`

Program Explanation
Line 1 : Scan the input image 'a'. Note that PATH should be replaced with the path of the image (which should be noise cleaned) in your computer.
Line 2 : It is highly probable that you consider a color image (RGB). So, convert it into a black and white image (GRAY) and perform noise cleaning. If you consider a black and white image, this line is not needed.
Line 3 : Store the height and width of the considered image in 'm' and 'n' respectively.
Line 4 : Store a copy of the image 'a' in 'b' so that the original can be available even after noise cleaning.
Line 5 : Calculate the mean pixel value of the image.
Line 6 : Scan the variable 'deviation' to take.
Lines 7 to 8 : Consider all pixels of the image.
Lines 9 to 10 : Replace the pixel values highly deviating from their mean.
Lines 11 to 13 : End the 'if' statement and the two 'for' loops.
Lines 14 to 15 : Show the input image (before noise cleaning) and output image (after noise cleaning). We can observe the difference between both the images as a result of noise cleaning.

For the other four techniques i.e. by using box filter, gaussian filter, unnormalized minimum variance weight mask and equal variance correlation mask, the procedure is same but the masks are different. The procedure is as follows. For every pixel (except the border ones i.e. first, last rows and first, last columns) and its 8-neighborhood i.e. a 3 x 3 block of the image, apply the corresponding mask. Application of mask involves two steps. First, multiply the elements with same positions in the 3 x 3 block of the image and the matrix in the mask (right part). Second, add all the elements in the resultant 3 x 3 block and multiply it with the fraction in the mask (left part). Replace the value of the pixel to which we considered 8-neighborhood and formed a 3 x 3 block of the image is with the result thus obtained. The mask is applied only for non border pixels because 8- neighborhood is not present for border pixels. Hence, the image is noise cleaned.

Noise Cleaning using box filter

Now, let us implement this technique using MATLAB.

Program

`a = imread('PATH');a = rgb2gray(a);[m n] = size(a);b = a;for i = 2 : m - 1for j = 2 : n - 1g = [1 * a(i-1, j-1)   1 * a(i-1, j)   1 * a(i-1, j+1);     1 * a(i, j-1)     1 * a(i, j)     1 * a(i, j+1);     1 * a(i+1, j-1)   1 * a(i+1, j)   1 * a(i+1, j+1);];b(i, j) = sum(sum(g)) / 9;endendimshow(a);figure,imshow(b);`

Program Explanation
Line 1 : Scan the input image 'a'. Note that PATH should be replaced with the path of the image (which should be noise cleaned) in your computer.
Line 2 : It is highly probable that you consider a color image (RGB). So, convert it into a black and white image (GRAY) and perform noise cleaning. If you consider a black and white image, this line is not needed.
Line 3 : Store the height and width of the considered image in 'm' and 'n' respectively.
Line 4 : Store a copy of the image 'a' in 'b' so that the original can be available even after noise cleaning.
Lines 5 to 6 : Consider all pixels of the image except border ones (first, last rows and first, last columns).
Lines 7 to 10 : Apply the mask.
Lines 11 to 12 : End the two 'for' loops.
Lines 13 to 14 : Show the input image (before noise cleaning) and output image (after noise cleaning). We can observe the difference between both the images as a result of noise cleaning.

Noise Cleaning using gaussian filter

Now, let us implement this technique using MATLAB.

Program

`a = imread('PATH');a = rgb2gray(a);[m n] = size(a);b = a;for i = 2 : m - 1for j = 2 : n - 1g = [1 * a(i-1, j-1)   2 * a(i-1, j)   1 * a(i-1, j+1);     2 * a(i, j-1)     4 * a(i, j)     2 * a(i, j+1);     1 * a(i+1, j-1)   2 * a(i+1, j)   1 * a(i+1, j+1);];b(i, j) = sum(sum(g)) / 16;endendimshow(a);figure,imshow(b);`

Program Explanation
Line 1 : Scan the input image 'a'. Note that PATH should be replaced with the path of the image (which should be noise cleaned) in your computer.
Line 2 : It is highly probable that you consider a color image (RGB). So, convert it into a black and white image (GRAY) and perform noise cleaning. If you consider a black and white image, this line is not needed.
Line 3 : Store the height and width of the considered image in 'm' and 'n' respectively.
Line 4 : Store a copy of the image 'a' in 'b' so that the original can be available even after noise cleaning.
Lines 5 to 6 : Consider all pixels of the image except border ones (first, last rows and first, last columns).
Lines 7 to 10 : Apply the mask.
Lines 11 to 12 : End the two 'for' loops.
Lines 13 to 14 : Show the input image (before noise cleaning) and output image (after noise cleaning). We can observe the difference between both the images as a result of noise cleaning.

Noise Cleaning using unnormalized minimum variance weight mask

Now, let us implement this technique using MATLAB.

Program

`a = imread('PATH');a = rgb2gray(a);[m n] = size(a);b = a;p = input('Enter a value :');q = input('Enter b value :');for i = 2 : m - 1for j = 2 : n - 1g = [(1 / q) * a(i-1, j-1)   (1 / p) * a(i-1, j)   (1 / q) * a(i-1, j+1);     (1 / p) * a(i, j-1)         (1) * a(i, j)     (1 / p) * a(i, j+1);     (1 / q) * a(i+1, j-1)   (1 / p) * a(i+1, j)   (1 / q) * a(i+1, j+1);];b(i, j) = sum(sum(g)) / (1 + (4 / p) + (4 / q));endendimshow(a);figure,imshow(b);`

Program Explanation
Line 1 : Scan the input image 'a'. Note that PATH should be replaced with the path of the image (which should be noise cleaned) in your computer.
Line 2 : It is highly probable that you consider a color image (RGB). So, convert it into a black and white image (GRAY) and perform noise cleaning. If you consider a black and white image, this line is not needed.
Line 3 : Store the height and width of the considered image in 'm' and 'n' respectively.
Line 4 : Store a copy of the image 'a' in 'b' so that the original can be available even after noise cleaning.
Lines 5 to 6 : Scan the variables 'a' and 'b' of the mask.
Lines 7 to 8 : Consider all pixels of the image except border ones (first, last rows and first, last columns).
Lines 9 to 12 : Apply the mask.
Lines 13 to 14 : End the two 'for' loops.
Lines 15 to 16 : Show the input image (before noise cleaning) and output image (after noise cleaning). We can observe the difference between both the images as a result of noise cleaning.

Noise Cleaning using equal variance correlation mask

Now, let us implement this technique using MATLAB.

Program

`a = imread('PATH');a = rgb2gray(a);[m n] = size(a);b = a;p = input('Enter p value :');for i = 2 : m - 1for j = 2 : n - 1g = [    (1) * a(i-1, j-1)             (1 - p) * a(i-1, j)       (1) * a(i-1, j+1);     (1 - p) * a(i, j-1)     (1 - p) * (1 - p) * a(i, j)     (1 - p) * a(i, j+1);         (1) * a(i+1, j-1)             (1 - p) * a(i+1, j)       (1) * a(i+1, j+1);];b(i, j) = sum(sum(g)) / ((3 - p) * (3 - p));endendimshow(a);figure,imshow(b);`

Program Explanation
Line 1 : Scan the input image 'a'. Note that PATH should be replaced with the path of the image (which should be noise cleaned) in your computer.
Line 2 : It is highly probable that you consider a color image (RGB). So, convert it into a black and white image (GRAY) and perform noise cleaning. If you consider a black and white image, this line is not needed.
Line 3 : Store the height and width of the considered image in 'm' and 'n' respectively.
Line 4 : Store a copy of the image 'a' in 'b' so that the original can be available even after noise cleaning.
Line 5 : Scan the variable 'p' of the mask.
Lines 6 to 7 : Consider all pixels of the image except border ones (first, last rows and first, last columns).
Lines 8 to 11 : Apply the mask.
Lines 12 to 13 : End the two 'for' loops.
Lines 14 to 15 : Show the input image (before noise cleaning) and output image (after noise cleaning). We can observe the difference between both the images as a result of noise cleaning.