Search code examples
pythonimage-processingscipyndimage

How to apply a uniform filter using SciPy Image where the data outside the boundary is not tallied?


I have spent lots of time on this, and I know how to manually do it by slicing and indexing the boundary rows/cols, but there has to be a simpler way with SciPy.

I need to set the CVAL (values to fill past the edges when mode=constant) to NaN, however, this will return NaN.

I will explain it with code and figures:

import numpy as np
from scipy import ndimage
m = np.reshape(np.arange(0,100),(10,10)).astype(np.float)

picture of the input array Use SciPy ndimage uniform filter to calculate the mean using a 3x3 kernel:

filter = ndimage.uniform_filter(m, size=3, mode='constant')
print(filter[1][1]) # equal to 11
print(filter[9][9]) # I need 93.5, however it gets 41.55 due to zeros

As you can see, the first value comes out as 11, which is as expected, however, for any cell along the border, it will fill the values with zero (I have also tried all the other modes).

Here is what I need to achieve (left) vs mode=constant and CVAL=0 (default 0)

scipy avg and what I need


Solution

  • One simple approach is to use Normalized Convolution:

    import numpy as np
    from scipy import ndimage
    m = np.reshape(np.arange(0,100),(10,10)).astype(np.float)
    
    filter = ndimage.uniform_filter(m, size=3, mode='constant')    # normal filter result
    
    weights = ndimage.uniform_filter(np.ones(m.shape), size=3, mode='constant')
    filter = filter / weights    # normalized convolution result
    
    print(filter[1][1]) # equal to 11
    print(filter[9][9]) # equal to 93.49999999999994 -- rounding error! :)
    

    We computed the result of the filter if all data points were 1 (weights). This shows how many data elements there are in each filter window, and returns a value of 1 everywhere except near the boundary, where this value decreases proportionally. By dividing the filtering result with these weights, we correct for the averaging taking zeros into account that were outside the data domain.