python中的圆形中值过滤器 [英] circular median filter in python

查看:145
本文介绍了python中的圆形中值过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个具有给定半径的圆形中值滤波器,而不是数组中的正方形滤波器. 到目前为止,这是我的尝试:

I want to create a circular median filter with a given radius, rather than a square filter from an array. Here's my attempt so far:

#   Apply median filter to each image
import matplotlib.pyplot as plt
radius = 25
disk_filter = plt.fspecial('disk', radius)
w1_median_disk = plt.imfilter(w1data, disk_filter, 'replicate')

w2_median_disk = plt.imfilter(w2data, disk_filter, 'replicate')

w1dataw2data是我试图将过滤器应用到的二维numpy数组. fspecial模块来自Matlab,但是我想在我的Python代码中使用它(或等效的东西).有什么想法吗?

w1data and w2data are the 2-d numpy arrays I'm trying to apply the filter to. The fspecial module is from Matlab, but I want to use it (or something equivalent) in my Python code. Any ideas?

我收到错误消息"

disk_filter = plt.fspecial('disk',radius)
AttributeError:模块"对象没有属性"fspecial""

disk_filter = plt.fspecial('disk', radius)
AttributeError: 'module' object has no attribute 'fspecial'"

我想知道是否可以导入一个包含fspecial的模块,或者是Python中的等效命令.

I'm wondering if there's either a module I can import that contains fspecial, or an equivalent command in Python.

推荐答案

以下是我发现可以完成工作的内容:

Here's something I found that seems to do the job:

 from scipy.ndimage.filters import generic_filter as gf

 #   Define physical shape of filter mask
 def circular_filter(image_data, radius):
     kernel = np.zeros((2*radius+1, 2*radius+1))
     y, x = np.ogrid[-radius:radius+1, -radius:radius+1]
     mask = x**2 + y**2 <= radius**2
     kernel[mask] = 1                
     filtered_image = gf(image_data, np.median, footprint = kernel)
     return filtered_image

但是我不确定我完全理解发生了什么.特别是这些行到底是什么

But I'm not sure I understand perfectly what's going on. In particular, what exactly do the lines

     y, x = np.ogrid[-radius:radius+1, -radius:radius+1]
     mask = x**2 + y**2 <= radius**2
     kernel[mask] = 1

做什么?

这篇关于python中的圆形中值过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆