Assuming that the following array A
is the result of reading a GeoTIFF image, for example with rasterio where nodata values are masked which is the array B
.
I would like to apply a boxcar average smoothing over a square neighbourhood. The first problem is that I am not sure which scipy function represents a boxcar average?
I thought it might be the ndimage.uniform_filter. However, in contrast to scipy.signal, ndimage is not applicable to masked arrays.
from scipy.signal import medfilt
from scipy.ndimage import uniform_filter
import numpy as np
A = np.array([[-9999, -9999, -9999, -9999, -9999, -9999, -9999, -9999],
[-9999, -9999, -9999, -9999, -9999, -9999, -9999, -9999],
[-9999, -9999, -9999, -9999, -9999, -9999, -9999, -9999],
[-9999, -9999, -9999, 0, 300, 400, 200, -9999],
[-9999, -9999, -9999, -9999, 200, 0, 400, -9999],
[-9999, -9999, -9999, 300, 0, 0, -9999, -9999],
[-9999, -9999, -9999, 300, 0, -9999, -9999, -9999],
[-9999, -9999, -9999, -9999, -9999, -9999, -9999, -9999]])
B = np.ma.masked_array(A, mask=(A == -9999))
print(B)
filtered = medfilt(B, 3).astype('int')
result = np.ma.masked_array(filtered, mask=(filtered == -9999))
print(result)
boxcar = ndimage.uniform_filter(B)
print(boxcar)
So, how can I apply a boxcar average that accounts for nodata values such as scipy.signal.medfilt?