[SciPy-User] downsampling with max in 2d

Jerome Kieffer Jerome.Kieffer at esrf.fr
Fri Feb 7 04:08:23 EST 2014


On Thu, 06 Feb 2014 20:54:11 +0000
Dan Stowell <dan.stowell at eecs.qmul.ac.uk> wrote:

> Hi all,
> 
> I would like to downsample 2D arrays using max-pooling. In other words,
> to downsample by a factor of 2 would reduce
> 

This is very close to binning ... Here are a couple of pure numpy implementations:

if your binning is small (up to 8x8): go for a double loop like this:
        out = numpy.zeros(tuple(outputSize))
        for i in xrange(binsize[0]):
            for j in xrange(binsize[1]):
                out = numpy.maximum(input_img[i::binsize[0], j::binsize[1]], out)

If you order is large
        temp = input_img.copy()
        temp.shape = (outputSize[0], binsize[0], outputSize[1], binsize[1])
        out = temp.max(axis=3).max(axis=1)

Anyway the size of the input array does not matter.
HTH.
-- 
Jérôme Kieffer
tel +33 476 882 445



More information about the SciPy-User mailing list