## Thursday, 19 June 2014

### How to apply Average filter, Weighted filter and Median Filter to Noisy Image?

Some neighborhood operations work with the values of the image pixels in the neighborhood and the corresponding values of a sub image that has the same dimensions as the neighborhood. The sub image is called a filter, mask, kernel, template, or window, with the first three terms being the most prevalent terminology. The values in a filter sub image are referred to as coefficients, rather than pixels. The process consists simply of moving the filter mask from point to point in an image. At each point (x, y), the response of the filter at that point is calculated using a predefined relationship.

Smoothing Spatial Filters divided into two types ------
1. Smoothing Linear Filters -------
a) Average Filter
b) Weighted Filter
2. Smoothing Non-Linear Filters -------
a) Median Filter

I. Average Filtering:
The output of a smoothing, linear spatial filter is simply the average of the pixels contained in the neighborhood of the filter mask. These filters sometimes are called averaging filters. For reasons explained in they also are referred to a low pass filters. The idea behind smoothing filters is straightforward. By replacing the value of every pixel in an image by the average of the gray levels in the neighborhood defined by the filter mask, this process results in an image with reduced “sharp” transitions in gray levels. Because random noise typically consists of sharp transitions in gray levels, the most obvious application of smoothing is noise reduction.
Average Filter mask is as follows:
MATLAB Code:
 % Read Image for Noise Addition img=imread('lena.bmp'); % Add Noise Noi_img = imnoise(img,'salt & pepper', 0.02); % Mask Definition f=1/9*[1,1,1;1,1,1;1,1,1]; % Apply filter2 function de_noi=filter2(f,Noi_img); figure; subplot(1,3,1);imshow(img);title('Original image') subplot(1,3,2);imshow(Noi_img);title('Noisy image') subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Code Output:

II. Weighted Filtering:
The second mask is a little more interesting. This mask yields a so-called weighted average, terminology used to indicate that pixels are multiplied by different coefficients, thus giving more importance (weight) to some pixels at the expense of others. In the mask the pixel at the center of the mask is multiplied by a higher value than any other, thus giving this pixel more importance in the calculation of the average.
Weighted Filter mask is as follows:
MATLAB Code:
 % Read Image for Noise Addition img=imread('lena.bmp'); % Add Noise Noi_img = imnoise(img,'salt & pepper', 0.02); % Mask Definition f=1/16*[1,2,1;2,4,2;1,2,1]; % Apply filter2 function de_noi=filter2(f,Noi_img); figure; subplot(1,3,1);imshow(img);title('Original image') subplot(1,3,2);imshow(Noi_img);title('Noisy image') subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Code Output:

III. Median Filtering:
The best-known example in this category is the median filter, which, as its name implies, replaces the value of a pixel by the median of the gray levels in the neighborhood of that pixel (the original value of the pixel is included in the computation of the median). Median filters are quite popular because, for certain types of random noise, they provide excellent noise-reduction capabilities, with considerably less blurring than linear smoothing filters of similar size. Median filters are particularly effective in the presence of impulse noise, also called salt-and-pepper noise because of its appearance as white and black dots superimposed on an image.
MATLAB code:
 % Read Image for Noise Addition img=imread('lena.bmp'); % Add Noise Noi_img = imnoise(img,'salt & pepper', 0.02); % Apply medfilt2 function de_noi=medfilt2(Noi_img,[3 3]); figure; subplot(1,3,1);imshow(img);title('Original image') subplot(1,3,2);imshow(Noi_img);title('Noisy image') subplot(1,3,3);imshow(uint8(de_noi));title('Denoised image')
Code Output:
Comparison Between Filters:
 Parameters Average Filter Weighted Filter Median Filter Noise Reduction Reduces Noise but it introduces blurring effect at edges. Blurring effect is less as compared with Average filter Blurring effect is less as compared with Average filter Percentage of noise Reduction 100% noise Not Reduced 100% noise Not Reduced Almost 100% noise reduced. Size of Filter As we increase the size of the filter mask, Noise reduces but blurring effect increases. As we increase the size of the filter mask, Noise reduces but blurring effect increases. As we increase the size of the filter mask, 100% of Noise reduces but blurring effect at edges increases. Mask 1/9x[1,1,1;1,1,1;1,1,1] 1/16x[1,2,1;2,4,4;1,2,1] Pixel value is replaced by median value of neighborhood. MATLAB Function filter2(mask,Noisy_img) filter2(mask,Noisy_img) medfilt2(Noisy_img,[3 3])