From 2c425c06f30ac4be6b0b6d35aab6d390b169efa4 Mon Sep 17 00:00:00 2001 From: Jinwei Zhao Date: Thu, 21 Jan 2016 19:53:38 +0800 Subject: add MATLAB code --- MATLAB/boxfilter.m | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 MATLAB/boxfilter.m (limited to 'MATLAB/boxfilter.m') diff --git a/MATLAB/boxfilter.m b/MATLAB/boxfilter.m new file mode 100755 index 0000000..9990fa6 --- /dev/null +++ b/MATLAB/boxfilter.m @@ -0,0 +1,27 @@ +function imDst = boxfilter(imSrc, r) + +% BOXFILTER O(1) time box filtering using cumulative sum +% +% - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r))); +% - Running time independent of r; +% - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum); +% - But much faster. + +[hei, wid] = size(imSrc); +imDst = zeros(size(imSrc)); + +%cumulative sum over Y axis +imCum = cumsum(imSrc, 1); +%difference over Y axis +imDst(1:r+1, :) = imCum(1+r:2*r+1, :); +imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :); +imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :); + +%cumulative sum over X axis +imCum = cumsum(imDst, 2); +%difference over Y axis +imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1); +imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1); +imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1); +end + -- cgit v1.2.3