How can i perform manual convolution in an image MxNx3

140 views (last 30 days)
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);
result = imadd(I_conv1 , I_conv2);
resultFinal = imadd(result , I_conv3);
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 .
  1 Comment
Gn Gnk
Gn Gnk on 7 Apr 2021
clear all;
close all;
K=1/16.*[1 2 1 ; 2 4 2 ; 1 2 1]; %random filter
I = imread('lenaTest3.jpg');
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
%% Zero_padding
fprintf('\nZERO PADDING\n');
I_padded = padarray(I, [numLayers, numLayers]); %Zero-padding(zeros around the image)
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
title('Manual convolution (zero-padding)');
%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);
% Now we have the filtered value. Assign it to our output image.
convImage(row, col) = localSum;

Sign in to comment.

Accepted Answer

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);
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!