简体   繁体   English

使用scipy.weave.inline进行快速2D中值滤波

[英]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. 我在用于大量图像的2D中值滤镜(3x3窗口)中有一个瓶颈,我想尝试对其进行优化。 I've tested scipy.ndimage median_filter, as well as PIL , scipy.signal and scikits-image . 我已经测试了scipy.ndimage过滤器以及PILscipy.signalscikits-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 ? 但是,在SO中浏览时,我了解到C中有一个快速的O(n)中值过滤器(恒定时间中值过滤,请参阅C中的滚动中值算法 ),我想知道是否可以使用scipy在Python中实现它。 weave.inline吗? Any suggestions on an alternative route? 关于替代路线有什么建议吗?

Try this: Rolling median in C - Turlach implementation 试试这个: 在C中滚动中位数-Turlach实现

http://ideone.com/8VVEa 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). 我相信这与R算法相同,但更加简洁(实际上令人惊奇的是)。

You can either wrap this, or port it and use Numba (or Cython). 您可以将其包装或移植并使用Numba(或Cython)。 I think I'd recommend Numba over Cython, if nothing else because it is plain old python code. 我想我推荐Numba胜过Cython,如果没有别的,因为它纯朴的python代码。

I suggest adding this to scikits, if it runs faster than the one in scikits already :) 我建议将其添加到scikits中,如果它比scikits中的运行速度快:)

If your still interested I'd try numpy's reshape and median: 如果您仍然感兴趣,我会尝试numpy的重塑和中位数:

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... 我不知道它与您的睾丸方法相比如何扩展,但是如果您想使用C进行优化,则可以在列表理解中固定for循环...

I don't know the underlying algorithm, but scikits-image has a rolling median filter . 我不知道底层算法,但是scikits-image具有滚动中值滤波器

Otherwise, I'd recommend writing it in Cython (C/Python pidgin language). 否则,建议使用Cython (C / Python pidgin语言)编写它。 Be sure to check out the convolution example/tutorial for working with numpy arrays. 确保检查出卷积示例/教程以使用numpy数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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