numpy 图片填充,使用numpy和scipy填充图像上的空白

The image (test.tif) is attached.

The np.nan values are the whitest region.

How to fill those whitest region using some gap filling algorithms that uses values from the neighbours?

62e02c96c26e5b6b9375ab2797b1bd07.png

import scipy.ndimage

data = ndimage.imread('test.tif')

解决方案

I think viena's question is more related to an inpainting problem.

Here are some ideas:

In order to fill the gaps in B/W images you can use some filling algorithm like scipy.ndimage.morphology.binary_fill_holes. But you have a gray level image, so you can't use it.

I suppose that you don't want to use a complex inpainting algorithm. My first suggestion is: Don't try to use Nearest gray value (you don't know the real value of the NaN pixels). Using the NEarest value will generate a dirty algorithm. Instead, I would suggest you to fill the gaps with some other value (e.g. the mean of the row). You can do it without coding by using scikit-learn:

>>> from sklearn.preprocessing import Imputer

>>> imp = Imputer(strategy="mean")

>>> a = np.random.random((5,5))

>>> a[(1,4,0,3),(2,4,2,0)] = np.nan

>>> a

array([[ 0.77473361, 0.62987193, nan, 0.11367791, 0.17633671],

[ 0.68555944, 0.54680378, nan, 0.64186838, 0.15563309],

[ 0.37784422, 0.59678177, 0.08103329, 0.60760487, 0.65288022],

[ nan, 0.54097945, 0.30680838, 0.82303869, 0.22784574],

[ 0.21223024, 0.06426663, 0.34254093, 0.22115931, nan]])

>>> a = imp.fit_transform(a)

>>> a

array([[ 0.77473361, 0.62987193, 0.24346087, 0.11367791, 0.17633671],

[ 0.68555944, 0.54680378, 0.24346087, 0.64186838, 0.15563309],

[ 0.37784422, 0.59678177, 0.08103329, 0.60760487, 0.65288022],

[ 0.51259188, 0.54097945, 0.30680838, 0.82303869, 0.22784574],

[ 0.21223024, 0.06426663, 0.34254093, 0.22115931, 0.30317394]])

The dirty solution that uses the Nearest values can be this:

1) Find the perimeter points of the NaN regions

2) Compute all the distances between the NaN points and the perimeter

3) Replace the NaNs with the nearest's point gray value

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值