我的任务是对图像应用大小为50x50像素的中值滤波器。我知道如何应用过滤器,但是当过滤器是偶数时,如何指定过滤器的大小?到目前为止我的代码如下。在
import matplotlib.pyplot as plt
from astropy.io import fits
import scipy.signal as sg
# Open data files
hdulist = fits.open('xbulge-w1.fits')
w1data = hdulist[0].data
hdulist2 = fits.open('xbulge-w2.fits')
w2data = hdulist2[0].data
# Apply median filter to each image
w1_med = sg.medfilt(w1data)
w2_med = sg.medfilt(w2data)
# Set maximum sampled galactic lat (b) and long (l)
l_max = 15
b_max = 15
# Plot median filtered images, rescaled to galactic coordinates
plt.subplot2grid((2,1), (0,0))
plt.imshow(w1_med, origin='lower',
extent=[l_max, -l_max, -b_max, b_max],
cmap = 'gray')
plt.title('W1 median filter')
plt.subplot2grid((2, 1), (1,0))
plt.imshow(w2_med, origin='lower',
extent=[l_max, -l_max, -b_max, b_max],
cmap = 'gray')
plt.title('W2 median filter')
plt.tight_layout()
plt.show()
问题的关键在于内核的大小。
scipy.signal.medfilt
约束您使用奇数大小的内核。在网上搜索一下,你会发现很多关于为什么内核通常是奇数大小的信息。主要原因,我相信,是中心。在例如,如果你用一个均匀大小的高斯核卷积一个包含高斯核的图像,你将得到一个与原始图像相比中心偏移(1/2像素)的高斯图像。在
具体地说,关于中值滤波器,可以考虑奇数大小内核的另外一个原因:具有奇数个像素产生唯一的中值,而具有偶数个像素将需要决定(例如)使用哪个像素作为结果:
pixel[int(size/2)]
,pixel[int(size/2)+1]
,或两者的平均值。在不能将
scipy.signal.medfilt
用于偶数sezed内核。但是,您可以始终编写一个循环来遍历输入图像的所有像素,并“围绕”每个像素提取一个大小相等的窗口,然后计算该窗口中像素的中值。我引用“around”是因为不清楚如何将窗口置于像素的中心:这将由您来决定。在我看到了medfilt的定义:
你试过这个吗?在
基于the docs我想你需要改变的就是
到
^{pr2}$…对你有用吗?在
相关问题 更多 >
编程相关推荐