[SciPy-User] downsampling with max in 2d
Dan Stowell
dan.stowell at eecs.qmul.ac.uk
Thu Feb 6 15:54:11 EST 2014
Hi all,
I would like to downsample 2D arrays using max-pooling. In other words,
to downsample by a factor of 2 would reduce
a b c d
e f g h
i j k l
m n o p
to
max(a,b,e,f) max(c,d,g,h)
max(i,j,m,n) max(k,l,o,p)
I've searched through numpy and scipy and not found a method for this,
but I'd be grateful for pointers if I've missed it.
In the meantime, I wrote the following function. If you can spot a
faster/better ways to achieve this, please do say:
def maxdownsample2d(data, factor):
"""Supply a 2D numpy array, and an integer factor by which to
downsample (by nonoverlapping maxpooling) in both directions"""
# factor might not be exact fit, so we trim to this.
trimrows = int(np.floor(data.shape[0] / float(factor))) * factor
trimcols = int(np.floor(data.shape[1] / float(factor))) * factor
first = True
for coff in range(factor):
for roff in range(factor):
hopped = data[roff:trimrows:factor, coff:trimcols:factor]
if first:
maxsofar = hopped
first = False
else:
maxsofar = np.maximum(maxsofar, hopped)
return maxsofar
Best
Dan
--
Dan Stowell
Postdoctoral Research Assistant
Centre for Digital Music
Queen Mary, University of London
Mile End Road, London E1 4NS
http://c4dm.eecs.qmul.ac.uk/people/dans.htm
http://www.mcld.co.uk/
More information about the SciPy-User
mailing list