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

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

问题描述

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

<块引用>

导入scipy.ndimage数据 = ndimage.imread('test.tif')

解决方案

我认为 viena 的 问题与 修复 问题.

这里有一些想法:

  • 为了填补黑白图像中的空白,您可以使用一些填充算法,例如 scipy.ndimage.morphology.binary_fill_holes.但是你有一个灰度图像,所以你不能使用它.

  • 我想您不想使用复杂的修复算法.我的第一个建议是:不要尝试使用 Nearest 灰度值(您不知道 NaN 像素的真实值).使用 Nearest 值将生成脏算法.相反,我建议您用一些其他值(例如行的平均值)填补空白.您可以使用 scikit-learn 无需编码即可完成此操作:

来源:

<预><代码>>>>from sklearn.preprocessing import Imputer>>>imp = Imputer(策略=平均值")>>>a = np.random.random((5,5))>>>a[(1,4,0​​,3),(2,4,2,0)] = np.nan>>>一个数组([[0.77473361,0.62987193,南,0.11367791,0.17633671],[ 0.68555944, 0.54680378, 南, 0.64186838, 0.15563309],[ 0.37784422, 0.59678177, 0.08103329, 0.60760487, 0.65288022],[南, 0.54097945, 0.30680838, 0.82303869, 0.22784574],[ 0.21223024, 0.06426663, 0.34254093, 0.22115931, 南]])>>>a = imp.fit_transform(a)>>>一个数组([[ 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]])

  • 使用 Nearest 值的脏解决方案可以是这样的:1) 找到 NaN 区域的周长点2) 计算 all NaN 点与周长之间的距离3)用最近的点灰度值替换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')

解决方案

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:

Source:

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

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

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