Linear Filtering without using Imfilter Function in MATLAB


In MATLAB, "linear filtering" is a technique used to apply a linear filter to a digital image. The fundamental principle behind the working of linear filtering is convolution.

This article is meant for explaining the concept of linear filtering and how to perform linear filtering without using the "imfilter" function in MATLAB.

What is Linear Filtering?

In digital image processing, the process of applying a linear filter (also called convolution kernel) to a digital image is called linear filtering. The working of linear filtering is based on the concept of convolution.

  • In the process of linear filtering, a convolution kernel is applied over the input digital image.

  • Linear filtering is a widely used process in various applications like noise reduction, edge sharpening, edge detection, blurring an image, and more.

  • In MATLAB, we can perform linear filtering using either the "imfilter" function or the manual method.

In this tutorial, we will learn to apply linear filtering without using the "imfilter" function in MATLAB.

Linear Filtering Without Using Imfilter Function in MATLAB

We can use MATLAB to apply linear filtering without using the "imfilter" function. Here is the step-by-step process.

  • Step (1) − Read the input image. Use the "imread" function for this.

  • Step (2) − Convert the input image to double precision for calculations.

  • Step (3) − Create a filter kernel.

  • Step (4) − Determine the sizes of input image and the kernel filter.

  • Step (5) − Initialize a zero filled array to store the filtered image.

  • Step (6) − Compute the padding size based on the filter kernel to handle the convolution close to borders of the image.

  • Step (7) − Pad the input image with zeros to ensures that the filter kernel is applied properly in regions close to borders of the image.

  • Step (8) − Perform the convolution of regions of interest (ROI) and the filter kernel and store this result in a variable.

  • Step (9) − Display the linearly filtered image.

Example

Let us now take an example to understand how to perform linear filtering of an image without using the "imfilter" function in MATLAB.

% MATLAB code to perform linear filtering without using imfilter function
% Read the input image
I = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425269.jpg');

% Convert it to double for calculations
I = double(I);

% Define the desired filter kernel
fil_knl = ones(3, 3) / 9;			% Averaging filter kernel

% Determine the sizes of image and filter kernel
[I_h, I_w] = size(I);	% Image size
[fil_h, fil_w] = size(fil_knl);		% Filter size

% Create an output image with zero initials
out_img = uint8(zeros(I_h, I_w));

% Calculate the padding size as per filter
p1 = floor(fil_h / 2);
p2 = floor(fil_w / 2);

% Perform padding of the input image
pad_img = padarray(I, [p1, p2], 0);	% 0 is background color

% Perform convolution between ROI and filter
for i = 1 : I_h
   for j = 1 : I_w
      % Declare the ROI in the padded image
      roi = pad_img(i : i + fil_h - 1, j : j + fil_w - 1);
        
      % Convolution of ROI and filter kernel
      out_img(i, j) = sum(sum(roi .* fil_knl));
   end
end

% Display the original and filtered images
figure;
subplot(2, 1, 1);
imshow(uint8(I));
title('Original Image');

subplot(2, 1, 2);
imshow(out_img);
title('Filtered Image');

Output

When your run this code, it will produce the following output

Code Explanation

In this MATLAB example, we start by reading the input image using the "imread" function. Then, we convert the input image to double precision to calculations.

After that, we create a desired filter kernel, as in this example we created a (3 × 3) averaging filter.

Then, we determine the sizes of the input image and the filter kernel using the "size" function. Next, we create a zero-filled array to store the filtered output image.

Then, we calculate the padding sizes based on the filter kernel and apply this padding to the input image to ensure that the kernel is properly applied near borders of the image.

After that, we declare a nested loop to determine the region of interest (ROI) and perform convolution between ROI and the filter kernel to produce the filtered image.

Finally, we use the "imshow" function to display the input image and the filtered output image.

Conclusion

In conclusion, linear filtering is a process in digital image processing used to apply a linear filter to an image and is used in various image processing tasks such as edge sharpening, edge detection, blurring an image, noise reduction, etc. In this tutorial, I explained the step-by-step process to perform linear filtering without using the "imfilter" function in detail with the help of an example in MATLAB programming.

Updated on: 09-Oct-2023

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements