[SciPy-user] Efficient "windowing"

Jose Luis Gomez Dans josegomez@gmx....
Wed Dec 5 10:18:09 CST 2007

I am trying to apply a simple algorithm to a 2D matrix (an image). What I want to do is, for each pixel, choose the highest (... lowest) value in its 8- or 4-connected neighbours. I have done this using weave.inline, and using a couple of loops, but I was curious if there's some way of doing this using numpy slice syntax? My (allegedly, unelegant) attempts have been versions of the following:

b[1:-1,1:-1] = scipy.array([a[0:-2,1:-1] , a[2:,1:-1] , a[1:-1,0:-2] ,\
      a[1:-1,2:],a[0:-2,0:-2], a[0:-2,2:], a[2:,0:-2], a[2:,2:]],'f').max()

They don't work, because the max() call at the end refers to the whole array, so you are given a constant value array, equal to the max. value of a. Using for loops is very slow when dealing with large arrays.

Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

More information about the SciPy-user mailing list