Write a custom MATLAB script that performs spatial filtering with a kernel of size 𝑁 × 𝑁 without using imfilter.
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Gyan Vaibhav
2023-11-9
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.
- 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.
- The loop is supposed to traverse over the different kernels, however due to a slight mistake it always uses the “kernel_1”.
- 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:
- Specified a function that does the convolution and takes care of padding.
- Included the Laplacian and custom filters.
- Fixed your loop and your output.
Hope this helps and resolves your issue.
Thanks
Gyan
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!