# [SciPy-user] Efficient "windowing"

Zachary Pincus zpincus@stanford....
Thu Dec 6 10:58:43 CST 2007

```Hello,

You'll want to look into scipy.ndimage.

Definition:     scipy.ndimage.maximum_filter(input, size=None,
footprint=None, output=None, mode='reflect', cval=0.0, origin=0)
Docstring:
Calculates a multi-dimensional maximum filter.

Either a size or a footprint with the filter must be
provided. An output array can optionally be provided. The origin
parameter controls the placement of the filter. The mode parameter
determines how the array borders are handled, where cval is the
value when mode is equal to 'constant'.

It handles several different boundary conditions, and you can give it
a "footprint" mask to just select 4- or 8-connected neighbors for
each pixel. There's also a generic_filter that will call whatever
function you want for each pixel+neighborhood.

In the context of maximum filtering, if you want to just ignore the
boundary condition, just use mode="constant", cval=-numpy.Infinity so
that the filter ignores pixels outside of the image.

Zach

On Dec 5, 2007, at 11:18 AM, Jose Luis Gomez Dans wrote:

> Hi,
> 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.
>
> Thanks!
> Jose
> --
> Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten