Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't understand the working of uniform_filter1d() function (imported from scipy.ndimage.filters)

Tags:

python

scipy

I read the scipy docs for the function here : scipy.ndimage.uniform_filter1d. However, when I tried using it, I couldn't wrap around my head on it's working. I read the docs, ran the example over there in the Python Shell, used my own example but still no progress. For eg:

>>> from scipy.ndimage import uniform_filter1d
>>> uniform_filter1d([2, 8, 0, 4, 1, 9, 9, 0], size=3)
array([4, 3, 4, 1, 4, 6, 6, 3])
>>> uniform_filter1d([1, 2, 3, 4, 5, 6, 7, 8], size=3)
array([1, 2, 3, 4, 5, 6, 7, 7])

When I saw the output of the second array, it felt like the function retained most of the array's elements. However in the second example it felt like barring 4 & 1 all the other elements in the output array were completely new.

Thus I would like you to help me understand the working and the use of this function.

like image 510
Argon Avatar asked Dec 18 '22 18:12

Argon


1 Answers

What this filter does is, according to size, to take the arithmetic average of each pixel with its neighbor. Size is the size of the sub-array to calculate arithmetic average. The standard for pixels without enough neighbors is to reflect. Let us go its process:

uniform_filter1d([1,2,3,4,5,6], size=3)

[1,2,3,4,5,6] # index 0, Reflect 1 : [1,1,2] -> average: 4/3 = 1
[1,2,3,4,5,6] # index 1, [1,2,3] -> average: 6/3 = 2
[1,2,3,4,5,6] # index 2, [2,3,4] -> average: 9/3 = 3
[1,2,3,4,5,6] # index 3, [3,4,5] -> average: 12/3 = 4
[1,2,3,4,5,6] # index 4, [4,5,6] -> average: 15/3 = 5
[1,2,3,4,5,6] # index 5, Reflect 6 : [5,6,6] -> average: 17/3 = 5

Result: [1,2,3,4,5,5]
like image 176
Ozgur Bagci Avatar answered Dec 26 '22 11:12

Ozgur Bagci