So I wrote this:
function y = convolution(u,v)
[m,n] = size(u);
[w,z] = size(v);
y = zeros(m-w+1,n-z+1);
for i = 1:m-w+1
for j = 1:n-z+1
y(i,j) = sum(sum(u(i:i+w-1,j:j+z-1).*v));
end
end
end
And then I compared it to conv2() in matlab with this:
function timedConv
a = im2double(rgb2gray(imread('picture.png')));
tic
convolution(a,[4 5 6;0 0 0;3 2 1]);
toc
tic
conv2(a,[4 5 6; 0 0 0; 3 2 1]);
toc
end
and found that mine takes over 4 seconds to run, while matlab's conv2 takes about 0.01 seconds. What's more, mine is outputting a m-w+1 x n-z+1 matrix, while matlab's outputs a m+w-1 x n+z-1, so it's assuming zero rows and columns outside of the image to do convolutions on the edges of the image. When I display the results, they look the same, so my function must be working. It's just so much slower and I have no idea why.. Can I get rid of the for loops somehow?