Skip to content

Commit 2b6d1c3

Browse files
committed
Added recursive filtering example.
1 parent 5da1950 commit 2b6d1c3

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

RecursiveFilter/IirBlur.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <Halide.h>
2+
3+
using namespace Halide;
4+
5+
class IirBlur : public Generator<IirBlur> {
6+
public:
7+
// The input is a 3D single precision float buffer, the first two
8+
// dimensions are the pixels, and the third is the color channel.
9+
ImageParam input{Float(32), 3, "input"};
10+
// This parameter is the strength of the blur.
11+
Param<float> A{"A"};
12+
13+
// Declare our free variables.
14+
Var x, y, c;
15+
16+
Func blur_transpose(Func f, Expr height) {
17+
// Define the blur: first, just copy f to blur.
18+
Func blur;
19+
blur(x, y, c) = f(x, y, c);
20+
21+
// Blur down the columns.
22+
RDom ry(1, height - 1);
23+
blur(x, ry, c) = A*blur(x, ry, c) + (1 - A)*blur(x, ry - 1, c);
24+
25+
// And back up the columns.
26+
Expr flip_ry = height - ry - 1;
27+
blur(x, flip_ry, c) =
28+
A*blur(x, flip_ry, c) + (1 - A)*blur(x, flip_ry + 1, c);
29+
30+
// Transpose the resulting blurred image.
31+
Func transpose;
32+
transpose(x, y, c) = blur(y, x, c);
33+
34+
// Schedule.
35+
// First, split transpose into groups of 8 rows of pixels.
36+
Var yo, yi;
37+
transpose.compute_root().split(y, yo, yi, 8);
38+
39+
// Parallelize the groups of rows.
40+
transpose.parallel(yo);
41+
42+
// Compute the blur at each strip of rows (columns before the
43+
// transpose).
44+
blur.compute_at(transpose, yo);
45+
46+
// Vectorize across x for all steps of blur.
47+
blur.vectorize(x, 8);
48+
blur.update(0).vectorize(x, 8);
49+
blur.update(1).vectorize(x, 8);
50+
51+
return transpose;
52+
}
53+
54+
Func build() {
55+
// Wrap the input image in a func.
56+
Func input_func;
57+
input_func(x, y, c) = input(x, y, c);
58+
59+
// Blur down the columns and transpose.
60+
Func blur_y = blur_transpose(input_func, input.height());
61+
62+
// Blur down the columns again (rows after the transpose
63+
// above), and then transpose back to the original
64+
// orientation.
65+
Func blur = blur_transpose(blur_y, input.width());
66+
67+
return blur;
68+
}
69+
};
70+
71+
auto gen = RegisterGenerator<IirBlur>("IirBlur");

RecursiveFilter/go.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
% Add the path to the Halide MATLAB tools.
2+
addpath(fullfile(pwd, '..', 'halide', 'tools'));
3+
4+
% Build the blur function.
5+
mex_halide('IirBlur.cpp');
6+
7+
% Load an input image.
8+
in = single(imread('rgb.png')) / 255;
9+
% Create an output buffer for the result.
10+
out = zeros(size(in), 'single');
11+
% This defines the amount of blur (the first order IIR filter coefficient).
12+
A = 0.05;
13+
14+
% Time how long it takes to run the blur 10 times.
15+
tic;
16+
for i = 1:10
17+
IirBlur(in, A, out);
18+
end
19+
toc;
20+
21+
imshow(out);
22+
23+

RecursiveFilter/rgb.png

5.95 MB
Loading

0 commit comments

Comments
 (0)