# 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 : m

for j = 1 : n

if((abs(mean - b(i, j)) > deviation)

b(i, j) = mean;

end

end

end

imshow(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

The mask used here is:

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 - 1

for j = 2 : n - 1

g = [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;

end

end

imshow(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

The mask used here is:

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 - 1

for j = 2 : n - 1

g = [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;

end

end

imshow(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

The mask used here is:

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 - 1

for j = 2 : n - 1

g = [(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));

end

end

imshow(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

The mask used here is:

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 - 1

for j = 2 : n - 1

g = [ (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));

end

end

imshow(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.