-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathbilateral.py
More file actions
333 lines (277 loc) · 10.1 KB
/
bilateral.py
File metadata and controls
333 lines (277 loc) · 10.1 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""Bilateral filter for feature-preserving smoothing.
Smooths a raster while preserving edges by weighting neighbors based on
both spatial distance and value similarity.
Reference
---------
Tomasi & Manduchi, "Bilateral Filtering for Gray and Color Images," ICCV 1998.
"""
from __future__ import annotations
from functools import partial
from math import ceil, exp, isnan
import numpy as np
import xarray as xr
from numba import cuda
from xarray import DataArray
try:
import dask.array as da
except ImportError:
da = None
try:
import cupy
except ImportError:
class cupy(object):
ndarray = False
from xrspatial.utils import (
ArrayTypeFunctionMapping,
_boundary_to_dask,
_pad_array,
_validate_boundary,
_validate_raster,
_validate_scalar,
cuda_args,
ngjit,
)
from xrspatial.dataset_support import supports_dataset
# ---------------------------------------------------------------------------
# helpers
# ---------------------------------------------------------------------------
def _kernel_radius(sigma_spatial):
"""Derive kernel radius from sigma_spatial: ceil(2 * sigma)."""
return int(ceil(2.0 * sigma_spatial))
# ---------------------------------------------------------------------------
# NumPy backend
# ---------------------------------------------------------------------------
@ngjit
def _bilateral_numpy(data, radius, sigma_spatial, sigma_range):
rows, cols = data.shape
out = np.empty_like(data)
inv_2_ss = 1.0 / (2.0 * sigma_spatial * sigma_spatial)
inv_2_sr = 1.0 / (2.0 * sigma_range * sigma_range)
for y in range(rows):
for x in range(cols):
center = data[y, x]
if isnan(center):
out[y, x] = np.nan
continue
w_sum = 0.0
v_sum = 0.0
y0 = max(y - radius, 0)
y1 = min(y + radius + 1, rows)
x0 = max(x - radius, 0)
x1 = min(x + radius + 1, cols)
for ny in range(y0, y1):
for nx in range(x0, x1):
val = data[ny, nx]
if isnan(val):
continue
dy = ny - y
dx = nx - x
dist2 = dy * dy + dx * dx
diff = val - center
range2 = diff * diff
w = exp(-dist2 * inv_2_ss - range2 * inv_2_sr)
w_sum += w
v_sum += w * val
if w_sum > 0.0:
out[y, x] = v_sum / w_sum
else:
out[y, x] = np.nan
return out
def _bilateral_numpy_boundary(data, radius, sigma_spatial, sigma_range,
boundary='nan'):
if boundary == 'nan':
return _bilateral_numpy(data, radius, sigma_spatial, sigma_range)
padded = _pad_array(data, radius, boundary)
result = _bilateral_numpy(padded, radius, sigma_spatial, sigma_range)
return result[radius:-radius, radius:-radius]
# ---------------------------------------------------------------------------
# Dask + NumPy backend
# ---------------------------------------------------------------------------
def _bilateral_dask_numpy(data, radius, sigma_spatial, sigma_range,
boundary='nan'):
_func = partial(
_bilateral_numpy, radius=radius,
sigma_spatial=sigma_spatial, sigma_range=sigma_range,
)
out = data.map_overlap(
_func,
depth=(radius, radius),
boundary=_boundary_to_dask(boundary),
meta=np.array(()),
)
return out
# ---------------------------------------------------------------------------
# CuPy (GPU) backend
# ---------------------------------------------------------------------------
@cuda.jit
def _bilateral_gpu(data, radius, inv_2_ss, inv_2_sr, out):
x, y = cuda.grid(2)
rows, cols = data.shape
if 0 <= x < cols and 0 <= y < rows:
center = data[y, x]
if isnan(center):
out[y, x] = center
return
w_sum = 0.0
v_sum = 0.0
y0 = max(y - radius, 0)
y1 = min(y + radius + 1, rows)
x0 = max(x - radius, 0)
x1 = min(x + radius + 1, cols)
for ny in range(y0, y1):
for nx in range(x0, x1):
val = data[ny, nx]
if not isnan(val):
dy = ny - y
dx = nx - x
dist2 = dy * dy + dx * dx
diff = val - center
range2 = diff * diff
w = exp(-dist2 * inv_2_ss - range2 * inv_2_sr)
w_sum += w
v_sum += w * val
if w_sum > 0.0:
out[y, x] = v_sum / w_sum
else:
out[y, x] = center
def _bilateral_cupy(data, radius, sigma_spatial, sigma_range):
data_cu = cupy.asarray(data, dtype=cupy.float64)
inv_2_ss = 1.0 / (2.0 * sigma_spatial * sigma_spatial)
inv_2_sr = 1.0 / (2.0 * sigma_range * sigma_range)
griddim, blockdim = cuda_args(data_cu.shape)
out = cupy.empty_like(data_cu)
_bilateral_gpu[griddim, blockdim](
data_cu, radius, inv_2_ss, inv_2_sr, out,
)
return out
# ---------------------------------------------------------------------------
# Dask + CuPy backend
# ---------------------------------------------------------------------------
def _bilateral_dask_cupy(data, radius, sigma_spatial, sigma_range,
boundary='nan'):
_func = partial(
_bilateral_cupy, radius=radius,
sigma_spatial=sigma_spatial, sigma_range=sigma_range,
)
out = data.map_overlap(
_func,
depth=(radius, radius),
boundary=_boundary_to_dask(boundary, is_cupy=True),
meta=cupy.array(()),
)
return out
# ---------------------------------------------------------------------------
# dispatcher
# ---------------------------------------------------------------------------
def _bilateral(data, radius, sigma_spatial, sigma_range, boundary='nan'):
agg = xr.DataArray(data)
mapper = ArrayTypeFunctionMapping(
numpy_func=partial(
_bilateral_numpy_boundary,
boundary=boundary,
),
cupy_func=_bilateral_cupy,
dask_func=partial(
_bilateral_dask_numpy,
boundary=boundary,
),
dask_cupy_func=partial(
_bilateral_dask_cupy,
boundary=boundary,
),
)
out = mapper(agg)(
agg.data,
radius=radius,
sigma_spatial=sigma_spatial,
sigma_range=sigma_range,
)
return out
# ---------------------------------------------------------------------------
# public API
# ---------------------------------------------------------------------------
@supports_dataset
def bilateral(agg, sigma_spatial=1.0, sigma_range=10.0,
name='bilateral', boundary='nan'):
"""Apply a bilateral filter for feature-preserving smoothing.
Smooths a raster while preserving edges. Each pixel is replaced by
a weighted average of its neighbours, where the weight depends on
both the spatial distance and the value difference between the
neighbour and the center pixel. Neighbours that are far away *or*
very different in value contribute little, so edges stay sharp.
Parameters
----------
agg : xarray.DataArray or xr.Dataset
2D (or 3D multi-band) array of input values.
Supports NumPy, CuPy, Dask+NumPy, and Dask+CuPy backends.
sigma_spatial : float, default 1.0
Standard deviation of the spatial Gaussian. Controls the size
of the neighbourhood: kernel radius = ceil(2 * sigma_spatial).
Must be > 0.
sigma_range : float, default 10.0
Standard deviation of the range (value-similarity) Gaussian.
Larger values allow more smoothing across value differences;
smaller values preserve more edges. Must be > 0.
name : str, default 'bilateral'
Name for the output DataArray.
boundary : str, default 'nan'
How to handle edges where the kernel extends beyond the raster.
``'nan'`` -- fill missing neighbours with NaN (default).
``'nearest'`` -- repeat edge values.
``'reflect'`` -- mirror at boundary.
``'wrap'`` -- periodic / toroidal.
Returns
-------
out : xarray.DataArray or xr.Dataset
Filtered array of the same shape, dtype, dims, and coords as
the input.
Examples
--------
.. sourcecode:: python
>>> import numpy as np
>>> import xarray as xr
>>> from xrspatial import bilateral
>>> data = np.array([
... [0., 0., 0., 100., 100.],
... [0., 0., 0., 100., 100.],
... [0., 0., 0., 100., 100.],
... [0., 0., 0., 100., 100.],
... [0., 0., 0., 100., 100.]])
>>> raster = xr.DataArray(data)
>>> smoothed = bilateral(raster, sigma_spatial=1.0, sigma_range=5.0)
References
----------
Tomasi & Manduchi, "Bilateral Filtering for Gray and Color Images,"
ICCV 1998.
"""
_validate_raster(agg, func_name='bilateral', name='agg', ndim=(2, 3))
_validate_scalar(sigma_spatial, func_name='bilateral',
name='sigma_spatial', dtype=(int, float),
min_val=0, min_exclusive=True)
_validate_scalar(sigma_range, func_name='bilateral',
name='sigma_range', dtype=(int, float),
min_val=0, min_exclusive=True)
_validate_boundary(boundary)
sigma_spatial = float(sigma_spatial)
sigma_range = float(sigma_range)
radius = _kernel_radius(sigma_spatial)
if agg.ndim == 3:
from xrspatial.focal import _apply_per_band
return _apply_per_band(
bilateral, agg,
sigma_spatial=sigma_spatial,
sigma_range=sigma_range,
name=name,
boundary=boundary,
)
out = _bilateral(
agg.data.astype(float),
radius, sigma_spatial, sigma_range, boundary,
)
return DataArray(
out,
name=name,
dims=agg.dims,
coords=agg.coords,
attrs=agg.attrs,
)