均匀中值滤波在Python中的应用

2024-09-27 09:32:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是对图像应用大小为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()

Tags: import过滤器dataaspltmedopenfilter
3条回答

问题的关键在于内核的大小。scipy.signal.medfilt约束您使用奇数大小的内核。在网上搜索一下,你会发现很多关于为什么内核通常是奇数大小的信息。主要原因,我相信,是中心。在

例如,如果你用一个均匀大小的高斯核卷积一个包含高斯核的图像,你将得到一个与原始图像相比中心偏移(1/2像素)的高斯图像。在

具体地说,关于中值滤波器,可以考虑奇数大小内核的另外一个原因:具有奇数个像素产生唯一的中值,而具有偶数个像素将需要决定(例如)使用哪个像素作为结果:pixel[int(size/2)]pixel[int(size/2)+1],或两者的平均值。在

不能将scipy.signal.medfilt用于偶数sezed内核。但是,您可以始终编写一个循环来遍历输入图像的所有像素,并“围绕”每个像素提取一个大小相等的窗口,然后计算该窗口中像素的中值。我引用“around”是因为不清楚如何将窗口置于像素的中心:这将由您来决定。在

我看到了medfilt的定义:

Signature: sg.medfilt(volume, kernel_size=None)
Docstring:
Perform a median filter on an N-dimensional array.

Apply a median filter to the input array using a local window-size
given by `kernel_size`.

Parameters
     
volume : array_like
    An N-dimensional input array.
kernel_size : array_like, optional
    A scalar or an N-length list giving the size of the median filter
    window in each dimension.  Elements of `kernel_size` should be odd.
    If `kernel_size` is a scalar, then this scalar is used as the size in
    each dimension. Default size is 3 for each dimension.
    ....

你试过这个吗?在

sg.medfilt(w1data,kernel_size=50)

基于the docs我想你需要改变的就是

#   Apply median filter to each image
w1_med = sg.medfilt(w1data)
w2_med = sg.medfilt(w2data)

^{pr2}$

…对你有用吗?在

相关问题 更多 >

    热门问题