diff options
author | Jinwei Zhao <[email protected]> | 2016-01-21 19:53:38 +0800 |
---|---|---|
committer | Jinwei Zhao <[email protected]> | 2016-01-21 19:53:38 +0800 |
commit | 2c425c06f30ac4be6b0b6d35aab6d390b169efa4 (patch) | |
tree | 8aaa2389c0c733f083024a6dc7311b5a07f26442 /MATLAB/boxfilter.m | |
parent | ba1633c59d98341b378ca3da61e9531b5f253c22 (diff) | |
download | GuidedFilter-2c425c06f30ac4be6b0b6d35aab6d390b169efa4.tar.gz |
add MATLAB code
Diffstat (limited to 'MATLAB/boxfilter.m')
-rwxr-xr-x | MATLAB/boxfilter.m | 27 |
1 files changed, 27 insertions, 0 deletions
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 @@ | |||
1 | function imDst = boxfilter(imSrc, r) | ||
2 | |||
3 | % BOXFILTER O(1) time box filtering using cumulative sum | ||
4 | % | ||
5 | % - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r))); | ||
6 | % - Running time independent of r; | ||
7 | % - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum); | ||
8 | % - But much faster. | ||
9 | |||
10 | [hei, wid] = size(imSrc); | ||
11 | imDst = zeros(size(imSrc)); | ||
12 | |||
13 | %cumulative sum over Y axis | ||
14 | imCum = cumsum(imSrc, 1); | ||
15 | %difference over Y axis | ||
16 | imDst(1:r+1, :) = imCum(1+r:2*r+1, :); | ||
17 | imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :); | ||
18 | imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :); | ||
19 | |||
20 | %cumulative sum over X axis | ||
21 | imCum = cumsum(imDst, 2); | ||
22 | %difference over Y axis | ||
23 | imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1); | ||
24 | imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1); | ||
25 | imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1); | ||
26 | end | ||
27 | |||