Clear Filters
Clear Filters

Write a custom MATLAB script that performs spatial filtering with a kernel of size 𝑁 × 𝑁 without using imfilter.

19 views (last 30 days)
implement a lowpass (smoothing) filter with three different kernel sizes, namely, 3 × 3, 5 × 5, and 7 × 7. Very importantly, the sum of kernel values must be equal to 1 for all three different cases. Manually implement zero-padding for each kernel size and iteratively produce a filtered image.
also use the same code to modify it so that it performs spatial filtering using zero-padding and the following 3 × 3 kernel:
[ -1 0 1
-1 0 1
-1 0 1]
Also perform Laplacian filtering in continuation to above code for 3x3 kernel:
[ 0 -1 0
-1 4 -1
0 -1 0]
  3 Comments
Priyansh Pathak
Priyansh Pathak on 27 Feb 2022
Edited: Rik on 28 Feb 2022
% Read images 1 and 2
img = imread('Xray_Bone.tiff');
X = im2gray(img);
X = double(X);
% Read Kernels
Kernel_1 = (1/9)*ones(3);
Kernel_2 = (1/25)*ones(5);
Kernel_3 = (1/49)*ones(7);
V=size(X);
for i=1:3
img_out_i=[];
U = size(Kernel_1);
X_padded=zeros([V(1) + (U(1)-1), V(2) + (U(2)-1)]);
disp(size(X_padded))
m=floor(U(1)/2);
n=floor(U(2)/2);
X_padded([m:V(1)+m-1],[n:V(2)+n-1]) = X;
% Perform convolution on image and selected kernel
img_out_i = convolution3dLayer (X_padded, Kernel_1);
end
%%Display output images
figure('color','w')
subplot(1,3,1); imshow(img_out_i,[]); title('filter 1 output')
subplot(1,3,2); imshow(img_out_2,[]); title('filter 2 output')
subplot(1,3,3); imshow(img_out_3,[]); title('filter 3 output')

Sign in to comment.

Answers (1)

Gyan Vaibhav
Gyan Vaibhav on 9 Nov 2023
Hi Priyansh,
I understand that you want to write MATLAB script that can perform spatial filtering on images with NxN filters also further use this perform Laplacian filtering without using MATLAB’s inbuilt “imfilter” function.
Your code looked alright but there were few areas of improvement.
  1. The function “convolution3dLayer” you're using is not suitable for this task. It's used for creating a 3-D convolutional layer for a Convolutional Neural Network (CNN), not for image convolution. It can be replaced with a custom convolution function.
  2. The loop is supposed to traverse over the different kernels, however due to a slight mistake it always uses the “kernel_1”.
  3. Further, the outputs “img_out_2” and “img_out_3” are undefined.
Here is the modified code as per your requirements:
% Read images
img = imread('image.png');
X = im2gray(img);
X = double(X);
% Define Kernels
Kernel_1 = (1/9)*ones(3);
Kernel_2 = (1/25)*ones(5);
Kernel_3 = (1/49)*ones(7);
Kernel_custom = [-1 0 1; -1 0 1; -1 0 1];
Kernel_laplacian = [0 -1 0; -1 4 -1; 0 -1 0];
% Store kernels in a cell array for easy access
kernels = {Kernel_1, Kernel_2, Kernel_3, Kernel_custom, Kernel_laplacian};
V = size(X);
img_out = cell(1, length(kernels)); % Initialize output cell array
for i=1:length(kernels)
kernel = kernels{i}; % Choose the kernel
% Perform convolution on image and selected kernel
img_out{i} = spatial_filter(X, kernel); % Use the previously defined function
end
% Display output images
figure('color','w')
subplot(2,3,1); imshow(img_out{1},[]); title('3x3 Lowpass Filtered Image')
subplot(2,3,2); imshow(img_out{2},[]); title('5x5 Lowpass Filtered Image')
subplot(2,3,3); imshow(img_out{3},[]); title('7x7 Lowpass Filtered Image')
subplot(2,3,4); imshow(img_out{4},[]); title('Custom Kernel Filtered Image')
subplot(2,3,5); imshow(img_out{5},[]); title('Laplacian Filtered Image')
% Define the spatial filter function
function filtered_img = spatial_filter(img, kernel)
[img_height, img_width] = size(img);
[kernel_height, kernel_width] = size(kernel);
pad_height = floor(kernel_height / 2);
pad_width = floor(kernel_width / 2);
padded_img = padarray(img, [pad_height, pad_width]);
filtered_img = zeros(size(img));
for i = 1:img_height
for j = 1:img_width
region = padded_img(i:i+kernel_height-1, j:j+kernel_width-1);
filtered_img(i, j) = sum(sum(double(region) .* kernel));
end
end
end
The changes I have made are:
  1. Specified a function that does the convolution and takes care of padding.
  2. Included the Laplacian and custom filters.
  3. Fixed your loop and your output.
Hope this helps and resolves your issue.
Thanks
Gyan

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!