简体   繁体   中英

Using scipy.weave.inline for fast 2D median filtering

I have a bottleneck in a 2D median filter (3x3 window) I use on a very large set of images, and I'd like to try and optimize it. I've tested scipy.ndimage median_filter, as well as PIL , scipy.signal and scikits-image . However, browsing in SO I've learned that there's a fast O(n) median filter out there in C (Median Filtering in Constant Time see Rolling median algorithm in C ), and I wondered whether I can implement it in Python using scipy.weave.inline ? Any suggestions on an alternative route?

Try this: Rolling median in C - Turlach implementation

http://ideone.com/8VVEa

Usage:

Mediator* m = MediatorNew(9);
for (...)
{
      MediatorInsert(m, value);
      median = MediatorMedian(m);
}

I believe this is the same as the R algo, but cleaner (amazingly so, in fact).

You can either wrap this, or port it and use Numba (or Cython). I think I'd recommend Numba over Cython, if nothing else because it is plain old python code.

I suggest adding this to scikits, if it runs faster than the one in scikits already :)

If your still interested I'd try numpy's reshape and median:

a= some big array
a.reshape(N,3,3) #N being specific to your array
[numpy.median(m) for m in a]

I don't know how this scales compared to your testet methods but if you want to optimize with C you could fasten the for loop in the list comprehension...

I don't know the underlying algorithm, but scikits-image has a rolling median filter .

Otherwise, I'd recommend writing it in Cython (C/Python pidgin language). Be sure to check out the convolution example/tutorial for working with numpy arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM