How to calculate effective local average for each coordinate of a large matrix (middle filter) in python

I have a large matrix, 4000x4000 I need to calculate the local average window value of 11x11 for each x, y of this matrix. Usually it should be something like

for x in range(4000)
  for y in range(4000)
    b[x,y]=mean(a[x-5:x+5,y-5:y+5]

But it will last a lot of time. Is this a more efficient way to do this? Thank you

+3
source share
2 answers

You essentially need a two-dimensional convolution. Scipy can do this for you: http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html

, SO: 2d python numpy

+6

, . , -, , ( ), ( ), , .

for x in xrange(4000):
    for y in xrange(4000):
        c[x,y] = a[x,y]
        if x > 0:
            c[x,y] += c[x-1,y]
        if y > 0:
            c[x,y] += c[x,y-1]

, : . 11x11

c[x+5,y+5]-c[x-5,y-5]

, :

b[x,y] = (c[x+5+,y+5]-c[x-5,y-5])/121

121 2 .

+3

Source: https://habr.com/ru/post/1772670/


All Articles