在蒙版图像

时间:2017-08-23 04:40:11

标签: python image scikit-image

我试图过滤图像,使每个像素的值等于围绕它的50x50平方内像素的中值,不包括任何蒙板像素。这是我最近的尝试:

  1. 从FITS文件中读取图像(看起来像这样......) enter image description here
  2. 从其他FITS文件应用遮罩
  3. 通过屏蔽图像(下面的蒙版图像)传递一个50x50像素的窗口(我认为这是最好的方法...对建议开放) enter image description here
  4. 创建蒙版图像的过滤副本,每个像素的值等于围绕它的50x50平方内像素的中位数值,不包括任何蒙版像素
  5. 在此处的代码中,我使用了skimage.util.view_as_windows文档中的一些方法 生成过滤后的图像: enter image description here

    在我看来,它忽略了蒙面像素。我的问题是双重的: 这是最好的方法吗? 如果是这样,为什么它看起来像忽略了面具?

    import numpy as np
    from astropy.io import fits
    from skimage.util.shape import view_as_windows
    
    #   Use the fits files as input image and mask
    hdulist = fits.open('xbulge-w1.fits')
    image = hdulist[0].data
    hdulist3 = fits.open('xbulge-mask.fits')
    mask = 1 - hdulist3[0].data
    imagemasked = np.ma.masked_array(image, mask = mask)
    
    side = 50
    window_shape = (side, side)
    
    Afiltered = view_as_windows(imagemasked, window_shape)
    
    # collapse the last two dimensions in one
    flatten_view = Afiltered.reshape(Afiltered.shape[0], Afiltered.shape[1], -1)
    
    # resampling the image by taking median
    median_view = np.ma.median(flatten_view, axis=2)
    

    注意:使用' side = 50'导致相当长的运行时间,因此出于测试目的,我倾向于将其减少到10到25个。

1 个答案:

答案 0 :(得分:0)

在python中有许多过滤器具有不同的行为,例如对于平均过滤器:

x=np.array([[0.1,0.8,.2],
            [0.5,0.2,np.nan],
            [0.7,0.2,0.9],
            [0.4,0.7,1],
            [np.nan,0.14,1]])

print(uniform_filter(x, size=3, mode='constant'))

[[ 0.17777778         nan         nan]
 [ 0.27777778         nan         nan]
 [ 0.3                nan         nan]
 [        nan         nan         nan]
 [        nan         nan         nan]]

from skimage.filters.rank import mean
from skimage.morphology import square
from skimage import img_as_float
x=np.array([[0.1,0.8,.2],
        [0.5,0.2,np.nan],
        [0.7,0.2,0.9],
        [0.4,0.7,1],
        [np.nan,0.14,1]])

print(mean(x, square(3)))

[[102  76  76]
 [106 102  97]
 [114 130 127]
 [ 90 142 167]
 [ 79 137 181]]

print(img_as_float(mean(x, square(3))))

[[ 0.4         0.29803922  0.29803922]
 [ 0.41568627  0.4         0.38039216]
 [ 0.44705882  0.50980392  0.49803922]
 [ 0.35294118  0.55686275  0.65490196]
 [ 0.30980392  0.5372549   0.70980392]]

skimage剂量不支持nan和masking:refrence

import numpy as np
# from scipy.signal import convolve
from scipy.signal import convolve2d

x=np.array([[0.1,0.8,.2],
            [0.5,0.2,np.nan],
            [0.7,0.2,0.9],
            [0.4,0.7,1],
            [np.nan,0.14,1]])

core = np.full((3,3),1/3**2)

# convolve(x, core, mode='same')
convolve2d(x, core, mode='same')

[[ 0.17777778         nan         nan]
 [ 0.27777778         nan         nan]
 [ 0.3                nan         nan]
 [        nan         nan  0.43777778]
 [        nan         nan  0.31555556]]