Skip to content

Instantly share code, notes, and snippets.

@mlzxy
Created September 18, 2016 21:06
Show Gist options
  • Save mlzxy/ef2060c96b5cd605da50928652399682 to your computer and use it in GitHub Desktop.
Save mlzxy/ef2060c96b5cd605da50928652399682 to your computer and use it in GitHub Desktop.
function result = conv2d_by_partial_sum_grid(image, filter)
[is1, is2] = size(image);
[fs1, fs2] = size(filter);
r1 = is1-(fs1-1);
r2 = is2-(fs2-1);
result = zeros(r1, r2);
psum_result = cell(is1, fs1);
for ir = 1:is1
for fr = 1:fs1
irow = image(ir,:);
frow = filter(fr,:);
psum_row = zeros(1, r2);
for pr = 1:r2
psum_row(pr) = dot_1d(irow(pr:pr+fs2-1), frow);
end
psum_result{ir, fr} = psum_row;
end
end
for i = 1:r1
for fj = 1:fs1
result(i,:) = result(i,:) + psum_result{i+fj-1, fj};
end
end
function r = dot_1d(x1, x2)
r = sum(x1.*x2);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment