# How can i perform manual convolution in an image MxNx3

140 views (last 30 days)
Gn Gnk on 7 Apr 2021
Commented: Gn Gnk on 8 Apr 2021
Hello ,
i want to perform manual convolution in an image with a filter (not using conv() or some matlab function) .The problem here that i was used to perform this convolution to images MxN , but in this case the image is MxNx3 . I thougth that maybe i should perform convolution 3 times like this :
%lets say that my image is I
I_conv1 = convolution(I(:,:,1),filter);
I_conv2 = convolution(I(:,:,2),filter);
I_conv3 = convolution(I(:,:,3),filter);
But i dont get the result i want .
Can someone help me here ?
If you want to check my code for the manual convolution let me know in the comments . I am not providing it here because the question is going to be very long .
Gn Gnk on 7 Apr 2021
clear all;
close all;
K=1/16.*[1 2 1 ; 2 4 2 ; 1 2 1]; %random filter
figure(1);
imshow(I);
title('Original image');
filterSize = length(K); % Filter window's full width.
% Compute the number of layers of pixels with value 0 to surround the image with.
numLayers = floor(filterSize/2) % Will be 1
filteredImage=convolution(I_padded , K , numLayers , filterSize); %add 2 extra parameters numLayers,filterSize
filteredImage = cast(filteredImage, 'like', I); %turn it to uint8 form instead of double
figure(2);
imshow(filteredImage);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%my manual convolution function
function convImage = convolution( image,kernel,numLayers , filterSize )
[rows, columns] = size(image);
filteredImage = zeros(size(image)); % Initialize to same size as original.
for col = numLayers + 1 : columns - numLayers
for row = numLayers + 1 : rows - numLayers
% Now for a window with the center pixel situated at (row, col,
% scan the filter window, multiplying its values by the values of the original image underneath it.
localSum = 0; % Initialize sum to zero for this (row, column) location.
for c = 1 : filterSize
% Get the column index of the original image underneath the corresponding pixel in the filter window
ic = col + c - numLayers - 1;
for r = 1 : filterSize
% Get the row index of the original image underneath the corresponding pixel in the filter window
ir = row + r - numLayers - 1;
% Sum up the product into our running total for this window location.
localSum = localSum + double(image(ir, ic)) * kernel(r, c);
end
end
% Now we have the filtered value. Assign it to our output image.
convImage(row, col) = localSum;
end
end
end

DGM on 7 Apr 2021
Edited: DGM on 7 Apr 2021
I'm not sure why you expect the convolution of a MxNx3 image with an IxJx1 filter to be a MxNx1 image. It would be a MxNx3 image. You'd just concatenate the three passes into one image
Rf = convolution(I(:,:,1),filter);
Gf = convolution(I(:,:,2),filter);
Bf = convolution(I(:,:,3),filter);
output=cat(3,Rf,Gf,Bf);
Are you intending on using 3D filters (e.g. IxJx2 or IxJx3)? If so, all you would need to do is pad the image along dim3 and add another outer loop to the convolution routine so that you traverse dim3 just the same as you did the others.
Gn Gnk on 8 Apr 2021
Thank you so much i was so confused