Image processing tutorial -- Basic Kernel-based Filters – By Deepan Gupta
A Quick Summary and outline of topics introduced in the tutorial
In the following tutorial we will analyse basic kernel based filters used in image processing
and understand their usability. We will test these filters on the images and study the
outputs.
Kernel-based Filters
Kernel basically refers to a filter function applied to the input in order to obtain the filtered
output. For different kind of filters we have different filters. In case of images filtering can
of various types e.g. Noise removal, Boundary Detection, Averaging, Contrast Stretching
etc.
As we know that Images are represented in a form of 2-Dimensional Matrix. Therefore the
filtering operation mainly uses convolution of input image with a small mask (matrix) which
is called as Kernel.
The output of the filtering operation also depends on the size of the kernel.
Band-Pass Filters: Band Pass Filters allow certain range of frequencies to pass through. They are used for
various Image enhancement operations like edge enhancement, sharpening. High pass
filter and Low pass filters are the examples of band-pass filters.
Low-Pass Filter: Low pass filter is one of the most basic kernel based filters. It is called so because it
passes the lower frequencies ie, it passes the lower contrasting regions. In other
words, it focuses on the homogeneous regions.
High-Pass Filter: High-pass filters are just the opposite of Low-pass filter. They allow high frequncies to
pass. Only regions having high rate of change in pixel values are retained. It is used
for Boundary detection.
High-Boost Filter: High Boost filter is used for sharpening of the images. In high-boost filter we enhance the
high frequencies regions output the enhanced image.
Sobel Filter: Sobel Filter is one of the most commonly used filters for edge detection. The results
obtained are very accurate and can be manipulated according to various images detailing.
We make use of two kernel masks and obtain two resultant images. Then final output is
obtained with the help of both the images.
The Tutorial document is provided here. After this document, we also provide you with the Matlab Scripts for running some of the filters discussed.
Low Pass Filter in Matlab
function[] = LowPass(InputImageName, maskSize, outputImageName)
ImageMatrix = imread(InputImageName);
InputImageSize = size(ImageMatrix);
OutputImageMatrix = uint8(zeros(InputImageSize));
for InputImageRow = 1:InputImageSize(1)
for InputImageColumn = 1:InputImageSize(2)
sum = 0;
for MaskRow = - floor(maskSize / 2):floor(maskSize / 2)
for MaskColumn = - floor(maskSize / 2):floor(maskSize / 2)
if ((InputImageRow + MaskRow) > 0 && InputImageRow + MaskRow <= InputImageSize(1) && (InputImageColumn + MaskColumn) > 0 && InputImageColumn + MaskColumn <= InputImageSize(2))
sum = sum + floor(int16(ImageMatrix(InputImageRow + MaskRow, InputImageColumn + MaskColumn)));
end
end
end
sum = floor(sum / (maskSize * maskSize));
OutputImageMatrix(InputImageRow, InputImageColumn) = uint8(sum);
end
end
imwrite(OutputImageMatrix, outputImageName);
end
High Pass Filter in Matlab
function[] = HighBoost(InputImageName, A, maskSize, outputImageName, IntermediateImageName)
InputImageMatrix = imread(InputImageName);
InputImageSize = size(InputImageMatrix);
OutputImageMatrix = uint8(zeros(InputImageSize));
LowPass(InputImageName, maskSize, IntermediateImageName);
InputImageMatrixl = imread(IntermediateImageName);
for InputImageRow = 1:InputImageSize(1)
for InputImageColumn = 1:InputImageSize(2)
temp = double(InputImageMatrix(InputImageRow, InputImageColumn)) * A - double(InputImageMatrixl(InputImageRow, InputImageColumn));
if (temp < 0)
OutputImageMatrix(InputImageRow, InputImageColumn) = 0;
elseif (temp > 255)
OutputImageMatrix(InputImageRow, InputImageColumn) = 255;
else
OutputImageMatrix(InputImageRow, InputImageColumn) = uint8(temp);
end
end
end
imwrite(OutputImageMatrix, outputImageName);
end
Sobel Filter in Matlab
function []=Sobel(InputImageName,OutputImageName,Threshold)
imwrite(edge(imread(InputImageName),'sobel',Threshold),OutputImageName);
end
High Pass Filter in Matlab
function[]=HighBoost(InputImageName,maskSize,outputImageName,IntermediateImageName)
HighBoost(InputImageName,1,maskSize,outputImageName,IntermediateImageName)
end