First try using vectorization calculation:
i, j = np.where(image > limit)
If your problem cannot be solved by vectorizing the calculation, you can speed up the for loop like:
for i in xrange(image.shape[0]): for j in xrange(image.shape[1]): pixel = image.item(i, j) if pixel > limit: pass
or
from itertools import product h, w = image.shape for pos in product(range(h), range(w)): pixel = image.item(pos) if pixel > limit: pass
The numpy.ndenumerate statement is slow, using the regular for loop and getting the value from the array using the item method, you can speed up the loop 4 times.
If you need a higher speed, try using Cython, it will make your code as fast as C code.
Hyry
source share