python - Scipy ndimage median_filter origin -
i have binary array, say, a = np.random.binomial(n=1, p=1/2, size=(9, 9))
. perform median filtering on using 3 x 3
kernel on it, say, b = nd.median_filter(a, 3)
. expect should perform median filter based on pixel , 8 neighbours. however, not sure placement of kernel. documentation says,
origin : scalar, optional.
the origin parameter controls placement of filter. default 0.0.
if default zero, should taking current pixel , 3 x 3
grid right , bottom, no? shouldn't default center of footprint
? in our 3 x 3
example correspond (1, 1)
opposed (0, 0)
?
thanks.
origin says accepts scalar, me accepts array-like input case scipy.ndimage.filters.convolve function. passing 0 indeed center of footprint. origin's value relative center. 3x3 footprint, can specify values -1.0 1.0. here examples. notice in example origin not specified filter centered expected.
import numpy np import scipy.ndimage a= np.zeros((5, 5)) a[1:4, 1:4] = np.arange(3*3).reshape((3, 3)) default_out = scipy.ndimage.median_filter(a, size=(3, 3)) shift_pos_x = scipy.ndimage.median_filter(a, size=(3, 3), origin=(0, 1)) shift_neg_x = scipy.ndimage.median_filter(a, size=(3, 3), origin=(0, -1)) print(a) print(default_out) print(shift_pos_x) print(shift_neg_x)
output:
input array:
[[ 0. 0. 0. 0. 0.] [ 0. 0. 1. 2. 0.] [ 0. 3. 4. 5. 0.] [ 0. 6. 7. 8. 0.] [ 0. 0. 0. 0. 0.]]
centered output:
[[ 0. 0. 0. 0. 0.] [ 0. 0. 1. 0. 0.] [ 0. 1. 4. 2. 0.] [ 0. 0. 4. 0. 0.] [ 0. 0. 0. 0. 0.]]
shifted right output:
[[ 0. 0. 0. 0. 0.] [ 0. 0. 0. 1. 0.] [ 0. 0. 1. 4. 2.] [ 0. 0. 0. 4. 0.] [ 0. 0. 0. 0. 0.]]
shifted left output:
[[ 0. 0. 0. 0. 0.] [ 0. 1. 0. 0. 0.] [ 1. 4. 2. 0. 0.] [ 0. 4. 0. 0. 0.] [ 0. 0. 0. 0. 0.]]
Comments
Post a Comment