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
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
Stefan van der Walt wrote:
Binary thresholding can be added to ndimage easily, if further speed improvement is needed.
Regards Stéfan Yes Id like to become involved in that effort. Whats the status of ndimage now? Has it all been brought over from numarray and placed, where? Is there a template of some kind for adding new code?
Regards, Mark
Hi Mark On Thu, Aug 03, 2006 at 11:25:26AM -0400, Mark Heslep wrote:
Stefan van der Walt wrote:
Binary thresholding can be added to ndimage easily, if further speed improvement is needed.
Regards Stéfan Yes Id like to become involved in that effort. Whats the status of ndimage now? Has it all been brought over from numarray and placed, where? Is there a template of some kind for adding new code?
You can find 'ndimage' in scipy. Travis also recently added the STSCI image processing tools to the sandbox. Stéfan
participants (2)
-
Mark Heslep -
Stefan van der Walt