【问题标题】:Fill masked array values with reflect padding in python efficiently用python中的反射填充有效地填充掩码数组值
【发布时间】:2019-01-23 05:55:25
【问题描述】:

我在一个区域中有一个带有掩码值的方阵(图像)。掩码对应于靠近边界的区域中的缺失值(总是)。我想创建一个新图像,其中掩蔽值替换为实际图像值存在的值的反射填充。我怎么能在python中这样做?

在下面的示例中,我想用中间面板的反射图像替换(左侧面板)黄色区域,其中存在数据。结果应该类似于右面板图像(这里的填充不是我所追求的适当反射填充,因为我只是使用了简单的配方right_image = np.where(temp.mask,middle_image[::-1,::-1],middle_image)

编辑:我尝试将图像逐行横向,并使用numpy反射填充,代码如下,但它不起作用:

# Row Reflect 
# Here out_image is the original image, where -2 is no data value
xx = []
for i in range(6000): # Number of rows
    temp1 = np.ma.array(out_image[0,i,:],mask= out_image[0,i,:]==-2) # Create mask per row, -2 is the masked value
    edges = ma.flatnotmasked_edges(temp1) # Find edges
    #if edges is not None:
    xx += [np.expand_dims(np.pad(temp1.data[edges[0]:edges[1]],((edges[0],temp1.shape[0]-edges[1])),mode='reflect'),0)]
out = np.concatenate(xx)
#out = out.reshape(2000,2000)

结果不正确。

【问题讨论】:

    标签: python python-3.x numpy data-science


    【解决方案1】:

    如果我理解正确,您想向蒙版区域添加反射(180° 旋转)。您可以使用scipy.ndimage.rotate (docs) 并添加两个图像。

    这样的东西有用吗?:

    from scipy.ndimage import rotate
    # sample mask for a 768 * 1024 image
    line = lambda x: 400 - 200 * x/800   
    mask = np.indices(im.shape[:2])
    mask = line(mask[0])<mask[1]
    
    plt.imshow(im*mask[:,:,None])
    plt.show()
    
    im2=rotate(im, 180) 
    plt.imshow(im*mask[:,:,None] + im2*~mask[:,:,None])
    plt.show()
    

    【讨论】:

    • 谢谢你的回答,不,反射不等于180旋转。这里的要点是,对缺失部分的反思是从左到右的平滑过渡,并且可以用作数据增强——满足我的需要——在我遇到的深度学习训练问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多