[Numpy-discussion] Fastest binary threshold?

Mark Heslep mark at mitre.org
Wed Aug 2 15:51:07 CDT 2006


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

I'm thinking the conversion of  the min, max constants from python ints 
to n.uint8 might be slowing it down?  Is there a better way?  Scipy is 
also an option.  Ive search up list quickly and nothing jumps out.

For comparison Ive got some ctypes wrapped OpenCv code (that I'd like to 
avoid) doing the same thing in <  1 msec:

Cv images here are unsigned 8 bit as above:
python -m timeit  -n 50 -s "import 
cv;sz=cv.cvSize(512,512);a=cv.cvCreateImage(sz, 8, 1); 
a_bin=cv.cvCreateImage(sz,8,1)"  "cv.cvThreshold(a, a_bin, float(128), 
float(255), cv.CV_THRESH_BINARY )"
50 loops, best of 3: 348 usec per loop

And with the Intel IPP optimizations turned on < 0.1msec:
python -m timeit  -n 50 -s "import cv; sz=cv.cvSize(512,512);  
a=cv.cvCreateImage(sz, 8, 1);  a_bin=cv.cvCreateImage(sz,8,1)"  
"cv.cvThreshold(a, a_bin, float(128), float(255), cv.CV_THRESH_BINARY )"
50 loops, best of 3: 59.5 usec per loop

Regards,
Mark




More information about the Numpy-discussion mailing list