Two things:
- use elementwise multiplication between the sample and the filter kernel
- sum() normally only takes the sum over the first non-singleton dimension. If you need the sum over more than one dimension, you'll have to do so in one way or another.
This is a start
inpict = imread('cameraman.tif');
inpict = im2double(inpict);
fk = ones(4,4)/16; % sum-normalized
b = 0; % offset?
outpict = conv2d(inpict,fk,b);
imshow(outpict)
function output = conv2d(thisimg,thisfk,offset)
[m,n] = size(thisfk);
[y,x] = size(thisimg);
y = y-m+1;
x = x-m+1;
output = zeros(y,x);
for l = 2:1:y
for j = 2:1:x
sample = thisimg(l:l+m-1,j:j+n-1);
output(l,j) = sum(sample.*thisfk,'all') + offset;
end
end
end
Depending on your goals, the window positioning and image padding may be issues of concern. See the related questions and answers.
general sliding/moving window filter base
same thing only with a gaussian mean setup
basic mean filter examples
basic convolution build (full size)
basic convolution build (same size)

