使用numpy和scipy填充图像上的空白 [英] filling gaps on an image using numpy and scipy

查看:701
本文介绍了使用numpy和scipy填充图像上的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附上图像(test.tif)。
np.nan值是最白的区域。
如何使用一些使用邻居值的间隙填充算法来填充这些最白的区域?

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?




import scipy.ndimage

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


推荐答案

我想 viena的问题与修复问题更为相关。

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

以下是一些想法:

  • 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.

我想你不想使用复杂的修复算法。我的第一个建议是:不要尝试使用最近的灰度值(你不知道NaN像素的实际值)。使用NEarest值将生成一个脏算法。相反,我建议你用其他值填补空白(例如行的平均值)。您可以使用 scikit-learn 进行编码而无需编码:

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]])




  • 使用最近值的脏解决方案可以是:
    1)查找周长点NaN区域
    2)计算所有 NaN点与周长之间的距离
    3)用最接近的点灰度值替换NaN

    • 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
    • 这篇关于使用numpy和scipy填充图像上的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆