DIGITAL IMAGE PROCESSING-SMOOTHING: LOW PASS FILTER

Vineethvasu
5 min readNov 26, 2020

--

Filtering

The filtering of images can be grouped into two according to the effects:

1. Low pass filters (Smoothing):
In order to remove high spatial frequency noise from a digital image, low pass filtering (also known as smoothing) is used. Low-pass filters usually use a moving window operator that affects one pixel of the image at a time, modifying its value with some local pixel area (window) functionality. To impact all the pixels in the image, the operator moves over the image.

2. High pass filters (Edge Detection, Sharpening):
You can use high-pass filters to sharpen an image. Fine information in the picture is highlighted by this filter — the opposite of the low-pass filter. High-pass Filtering operates the same way as filtering low-pass; it just uses another convolution kernel. When filtering an image, each pixel is affected by its neighbors, and the net effect of filtering is moving information around the image.

Input image

In this chapter, we’ll use this image:

Mean Filter:

It is easy to implement median filtering. It is used as a smoothing method for images, reducing the amount of variation in intensity between one pixel and the next, resulting in image noise reduction. The idea of mean filtering is simply to substitute each pixel value in an image, including itself with the mean (‘average value of its neighbors. This has the effect of eliminating the values of pixels that are not representative of their environment. Mean filtering is generally thought of as a convolution filter. It is based around a kernel, like other convolutions, which represents the shape and size of the neighborhood to be sampled when the mean is calculated. A square kernel of 3 x33 x 3 is often used, as shown below:

Mean Filter:

It is easy to implement median filtering. It is used as a smoothing method for images, reducing the amount of variation in intensity between one pixel and the next, resulting in image noise reduction. The idea of mean filtering is simply to substitute each pixel value in an image, including itself with the mean (‘average value of its neighbors. This has the effect of eliminating the values of pixels that are not representative of their environment. Mean filtering is generally thought of as a convolution filter. It is based around a kernel, like other convolutions, which represents the shape and size of the neighborhood to be sampled when the mean is calculated. A square kernel of 3 x33 x 3 is often used, as shown below:

img = imread('hawk.png');
mf = ones(3,3)/9;

The mf is the mean filter:

>> mf = ones(3,3)/9
mf =

0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111

filter2()

The filter2() is defined as:

Y = filter2(h,X)

Y = filter2(h,X) uses a two-dimensional FIR filter in the h matrix to filter the X data. Using two-dimensional correlation, it computes the outcome, Y, and returns the central part of the correlation that is the same size as X.

Y = filter2(h,X,shape)

This returns the part of Y specified by the parameter of the shape. The shape is a string of one of the following values:

1. ‘full’: The full two-dimensional correlation is returned. Y is larger than X in this case.

2. ‘same’: (default) The central part of the correlation is returned. Y is the same size as X in this case.

3. ‘Valid’: Returns only those parts of the correlation without zero-padded edges that are computed. Y is smaller than X in this instance.

Now we want to use filter2() to apply the kernel defined in the previous section:

img = imread('cameraman.tif');
imgd = im2double(img); % imgd in [0,1]
f = ones(3,3)/9;
img1 = filter2(f, imgd);
subplot(121);imshow(img);
subplot(122);imshow(img1);

Compared to the original input, we can see the filtered image (right) has been blurred a bit (left).

The low pass filter can be used for denoising, as stated earlier. Let’s test that. First, we spray some pepper and salt on the image to make the input a bit dirty, and then apply the average filter:

img = imread('cameraman.tif');
imgd = im2double(img); % imgd in [0,1]
imgd = imnoise(imgd,'salt & pepper',0.02);
f = ones(3,3)/9;
img1 = filter2(f, imgd);
subplot(121);imshow(imgd);
subplot(122);imshow(img1);

It has some effect on the salt and pepper noise but not much. It just made them blurred.

How about trying Matlab’s built-in median filter?

Median filter — medfilt2()

Here is the script:

I = imread('cameraman.tif');
J = imnoise(I,'salt & pepper',0.02);
K = medfilt2(J);
subplot(121);imshow(J);
subplot(122);imshow(K);

A lot better. Unlike the previous filter, which only uses the mean value, we used a median this time. Median filtering is a nonlinear operation often used to decrease “salt and pepper” noise in image processing.

Note also that the 2-D filter is medfilt2(), so it only works for grayscale images.

Removing noise in RGB image:

Medfilt2 was the filter that we used to remove the “salt & pepper” type noise. However as the “2” in the name indicates it is for 2-D array, unless we decompose each RGB channel and concatenate each channel after filtering, it will not work for RGB image. The following script does exactly that:

I = imread('hawk.png');
J = imnoise(I,'salt & pepper',0.2);

% filter each channel separately
r = medfilt2(J(:, :, 1), [3 3]);
g = medfilt2(J(:, :, 2), [3 3]);
b = medfilt2(J(:, :, 3), [3 3]);

% reconstruct the image from r,g,b channels
K = cat(3, r, g, b);

figure
subplot(121);imshow(J);
subplot(122);imshow(K);

--

--

Vineethvasu
Vineethvasu

Written by Vineethvasu

I seek new challenges and try to think out-of-the-box while looking for creative solutions to a given problem.

No responses yet