[Numpy-discussion] A faster median (Wirth's method)

Sturla Molden sturla at molden.no
Thu Sep 3 02:14:30 EDT 2009


Chad Netzer skrev:
> That's right, Robert. Basically, I meant doing a median on a square
> (or rectangle) "view" of an array, without first having to ravel(),
> thus generally saving a copy.  But actually, since my selection based
> median overwrites the source array, it may not save a copy anyway.
>   
Avoiding copies of timy buffers is futile optimization.

QuickSelect is overkill for tiny buffers like common filter kernels. 
Insertion sort is fine.

Getting rid of loops and multiple function calls in Python helps a lot.

If memory is not an issue, with np.median you can actully create an 
quite an efficient median filter using a 3D ndarray. For example if you 
use an image of 640 x 480 pixels and want a 9 pixel median filter, you 
can put shifted images in an 640 x 480 x 9 ndarray, and call  median 
with axis=2.

 
Sturla Molden





More information about the NumPy-Discussion mailing list