-
Notifications
You must be signed in to change notification settings - Fork 5
/
bilateral.impala
136 lines (113 loc) · 7.02 KB
/
bilateral.impala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
fn @closeness(c_d: f32, xf: i32, yf: i32) -> f32 {
math_builtins::exp(-c_d * (xf*xf) as f32) * math_builtins::exp(-c_d * (yf*yf) as f32)
}
fn @similarity(c_r: f32, xi: f32, x: f32) -> f32 {
let diff = xi - x;
math_builtins::exp(-c_r * diff * diff)
}
fn @bilateral_filter(x: i32, y: i32, acc: Acc[f32], _mask: Mask, sigma_d: i32, c_d: f32, c_r: f32) -> f32 {
let mut k = 0:f32;
let mut p = 0:f32;
for yf in unroll(-2*sigma_d, 2*sigma_d+1) {
for xf in unroll(-2*sigma_d, 2*sigma_d+1) {
let c = closeness(c_d, xf, yf);
let s = similarity(c_r, acc.read(x + xf, y + yf), acc.read(x, y));
k += c * s;
p += c * s * acc.read(x + xf, y + yf);
}
}
p / k
}
fn @bilateral_filter_mask(x: i32, y: i32, acc: Acc[f32], mask: Mask, sigma_d: i32, _c_d: f32, c_r: f32) -> f32 {
let mut k = 0:f32;
let mut p = 0:f32;
for yf in unroll(-2*sigma_d, 2*sigma_d+1) {
for xf in unroll(-2*sigma_d, 2*sigma_d+1) {
let diff = acc.read(x + xf, y + yf) - acc.read(x, y);
let c = mask.data(xf + 2*sigma_d, yf + 2*sigma_d);
let s = math_builtins::exp(-c_r * diff*diff);
k += c * s;
p += c * s * acc.read(x + xf, y + yf);
}
}
p / k
}
#[export]
fn main() -> i32 {
let width = 1024;
let height = 1024;
let sigma_r = 5:f32;
let c_r = 1:f32 / (2:f32 * sigma_r*sigma_r);
let arr = create_img[f32](width, height, alloc_cpu);
let out = create_img[f32](width, height, alloc_cpu);
init_rand(bitcast[&mut[f32]](arr.buf.data), arr.stride, out.height);
init_zero(bitcast[&mut[f32]](out.buf.data), out.stride, out.height);
let mask = get_mask5([[0.018316, 0.082085, 0.135335, 0.082085, 0.018316],
[0.082085, 0.367879, 0.606531, 0.367879, 0.082085],
[0.135335, 0.606531, 1.000000, 0.606531, 0.135335],
[0.082085, 0.367879, 0.606531, 0.367879, 0.082085],
[0.018316, 0.082085, 0.135335, 0.082085, 0.018316]]);
//let mask = get_mask9([[0.018316, 0.043937, 0.082085, 0.119433, 0.135335, 0.119433, 0.082085, 0.043937, 0.018316],
// [0.043937, 0.105399, 0.196912, 0.286505, 0.324652, 0.286505, 0.196912, 0.105399, 0.043937],
// [0.082085, 0.196912, 0.367879, 0.535261, 0.606531, 0.535261, 0.367879, 0.196912, 0.082085],
// [0.119433, 0.286505, 0.535261, 0.778801, 0.882497, 0.778801, 0.535261, 0.286505, 0.119433],
// [0.135335, 0.324652, 0.606531, 0.882497, 1.000000, 0.882497, 0.606531, 0.324652, 0.135335],
// [0.119433, 0.286505, 0.535261, 0.778801, 0.882497, 0.778801, 0.535261, 0.286505, 0.119433],
// [0.082085, 0.196912, 0.367879, 0.535261, 0.606531, 0.535261, 0.367879, 0.196912, 0.082085],
// [0.043937, 0.105399, 0.196912, 0.286505, 0.324652, 0.286505, 0.196912, 0.105399, 0.043937],
// [0.018316, 0.043937, 0.082085, 0.119433, 0.135335, 0.119433, 0.082085, 0.043937, 0.018316]]);
//let mask = get_mask13([[0.018316, 0.033746, 0.055638, 0.082085, 0.108368, 0.128022, 0.135335, 0.128022, 0.108368, 0.082085, 0.055638, 0.033746, 0.018316],
// [0.033746, 0.062177, 0.102512, 0.151240, 0.199666, 0.235877, 0.249352, 0.235877, 0.199666, 0.151240, 0.102512, 0.062177, 0.033746],
// [0.055638, 0.102512, 0.169013, 0.249352, 0.329193, 0.388896, 0.411112, 0.388896, 0.329193, 0.249352, 0.169013, 0.102512, 0.055638],
// [0.082085, 0.151240, 0.249352, 0.367879, 0.485672, 0.573753, 0.606531, 0.573753, 0.485672, 0.367879, 0.249352, 0.151240, 0.082085],
// [0.108368, 0.199666, 0.329193, 0.485672, 0.641180, 0.757465, 0.800737, 0.757465, 0.641180, 0.485672, 0.329193, 0.199666, 0.108368],
// [0.128022, 0.235877, 0.388896, 0.573753, 0.757465, 0.894839, 0.945959, 0.894839, 0.757465, 0.573753, 0.388896, 0.235877, 0.128022],
// [0.135335, 0.249352, 0.411112, 0.606531, 0.800737, 0.945959, 1.000000, 0.945959, 0.800737, 0.606531, 0.411112, 0.249352, 0.135335],
// [0.128022, 0.235877, 0.388896, 0.573753, 0.757465, 0.894839, 0.945959, 0.894839, 0.757465, 0.573753, 0.388896, 0.235877, 0.128022],
// [0.108368, 0.199666, 0.329193, 0.485672, 0.641180, 0.757465, 0.800737, 0.757465, 0.641180, 0.485672, 0.329193, 0.199666, 0.108368],
// [0.082085, 0.151240, 0.249352, 0.367879, 0.485672, 0.573753, 0.606531, 0.573753, 0.485672, 0.367879, 0.249352, 0.151240, 0.082085],
// [0.055638, 0.102512, 0.169013, 0.249352, 0.329193, 0.388896, 0.411112, 0.388896, 0.329193, 0.249352, 0.169013, 0.102512, 0.055638],
// [0.033746, 0.062177, 0.102512, 0.151240, 0.199666, 0.235877, 0.249352, 0.235877, 0.199666, 0.151240, 0.102512, 0.062177, 0.033746],
// [0.018316, 0.033746, 0.055638, 0.082085, 0.108368, 0.128022, 0.135335, 0.128022, 0.108368, 0.082085, 0.055638, 0.033746, 0.018316]]);
let sigma_d = mask.size_x / 4;
let c_d = 1:f32 / (2:f32 * (sigma_d*sigma_d) as f32);
let lower = clamp_lower[f32];
let upper = clamp_upper[f32];
let iteration_fun = iteration[f32]; // SS
//let iteration_fun = iteration_bounds[f32]; // SS + BH
//let iteration_fun = iteration_advanced[f32]; // SS + SM
//let bilateral_fun = bilateral_filter;
let bilateral_fun = bilateral_filter_mask;
for x, y, out_acc, accs in iteration_fun(out, make_img_list1(arr, (mask.size_x / 2, mask.size_y / 2)), lower, upper) {
out_acc.write(x, y, bilateral_fun(x, y, accs.get(0), mask, sigma_d, c_d, c_r));
}
print_total_timing();
// compare results
fn reference() -> i32 {
let mut passed = 0;
let mut rms_err = 0:f32;
let EPS = 0.02:f32;
let arr_acc = get_acc_bh[f32](arr, |idx, val| bitcast[&mut[f32]](arr.buf.data)(idx) = val, |idx| bitcast[&[f32]](arr.buf.data)(idx), (Boundary::Unknown, Boundary::Unknown), lower, upper);
let out_acc = get_acc [f32](out, |idx, val| bitcast[&mut[f32]](out.buf.data)(idx) = val, |idx| bitcast[&[f32]](out.buf.data)(idx));
for y in range(0, out.height) {
for x in range(0, out.width) {
let ref = bilateral_filter_mask(x, y, arr_acc, mask, sigma_d, c_d, c_r);
let mut err = ref - out_acc.read(x, y);
rms_err += err*err;
if err < 0:f32 { err = -err; }
if err > EPS { passed = 42; }
}
}
rms_err = math_builtins::sqrt(rms_err / (out.width*out.height) as f32);
if passed == 0 {
print_string("Test PASSED!\n");
} else {
print_string("Test FAILED!\n");
}
passed
}
let result = reference();
release(arr.buf);
release(out.buf);
result
}