[Numpy-discussion] Fastest binary threshold?

Stefan van der Walt stefan at sun.ac.za
Wed Aug 2 20:45:22 EDT 2006


On Wed, Aug 02, 2006 at 04:51:07PM -0400, Mark Heslep wrote:
> I need a binary threshold and numpy.where() seems very slow on numpy 
> 0.9.9.2800:
> 
>  python -m timeit  -n 10 -s "import numpy as n;a=n.ones((512,512), 
> n.uint8)*129"
> "a_bin=n.where( a>128, 128,0)"
> 10 loops, best of 3: 37.9 msec per loop

Using numpy indexing brings the time down by a factor of 10 or so:

In [46]: timeit b = N.where(a>128,128,0)
10 loops, best of 3: 27.1 ms per loop

In [47]: timeit b = (a > 128).astype(N.uint8) * 128
100 loops, best of 3: 3.45 ms per loop

Binary thresholding can be added to ndimage easily, if further speed
improvement is needed.

Regards
Stéfan




More information about the NumPy-Discussion mailing list