使用numpy和scipy填充图像上的间隙

2024-05-16 01:21:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

import scipy.ndimage

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

Tags: test图像import算法区域datanpscipy
3条回答

如果需要最近邻居的值,可以使用scipy.interpolate中的NearestNDInterpolator。也有other interpolators,你也可以考虑。

可以使用以下命令查找NaN值的X、Y索引值:

import numpy as np

nan_locs = np.where(np.isnan(data))

还有其他一些插值选项。一种选择是用median filter的结果替换NaN值(但是您的区域对于这个来说有点大)。另一个选项可能是grayscale dilation。正确的插值取决于您的端域。

如果以前没有使用过SciPy ND插值器,则需要提供X、Y和值数据以使插值器适合,然后再提供X和Y数据以使值适合在其上进行插值。可以使用上面的where示例作为模板来执行此操作。

我认为维耶纳的问题更与一个inpainting问题有关。

以下是一些想法:

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

  • 我想你不想用复杂的修补算法。我的第一个建议是:不要尝试使用最接近的灰度值(您不知道NaN像素的实际值)。使用最接近的值将生成脏算法。相反,我建议您用其他值(例如行的平均值)来填补空白。您可以使用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]])
  • 使用最接近值的脏溶液可以是: 1) 找到南部地区的周界点 2) 计算NaN点和周长之间的距离 3) 用最近的点灰度值替换NaNs

正如其他人所建议的,可以使用scipy.interpolate。然而,它需要相当广泛的索引操作才能使其工作。

完整示例:

from pylab import *
import numpy
import scipy.ndimage
import scipy.interpolate
import pdb

data = scipy.ndimage.imread('data.png')

# a boolean array of (width, height) which False where there are missing values and True where there are valid (non-missing) values
mask = ~( (data[:,:,0] == 255) & (data[:,:,1] == 255) & (data[:,:,2] == 255) )

# array of (number of points, 2) containing the x,y coordinates of the valid values only
xx, yy = numpy.meshgrid(numpy.arange(data.shape[1]), numpy.arange(data.shape[0]))
xym = numpy.vstack( (numpy.ravel(xx[mask]), numpy.ravel(yy[mask])) ).T

# the valid values in the first, second, third color channel,  as 1D arrays (in the same order as their coordinates in xym)
data0 = numpy.ravel( data[:,:,0][mask] )
data1 = numpy.ravel( data[:,:,1][mask] )
data2 = numpy.ravel( data[:,:,2][mask] )

# three separate interpolators for the separate color channels
interp0 = scipy.interpolate.NearestNDInterpolator( xym, data0 )
interp1 = scipy.interpolate.NearestNDInterpolator( xym, data1 )
interp2 = scipy.interpolate.NearestNDInterpolator( xym, data2 )

# interpolate the whole image, one color channel at a time    
result0 = interp0(numpy.ravel(xx), numpy.ravel(yy)).reshape( xx.shape )
result1 = interp1(numpy.ravel(xx), numpy.ravel(yy)).reshape( xx.shape )
result2 = interp2(numpy.ravel(xx), numpy.ravel(yy)).reshape( xx.shape )

# combine them into an output image
result = numpy.dstack( (result0, result1, result2) )

imshow(result)
show()

输出:

enter image description here

这将传递给插值器我们拥有的所有值,而不仅仅是丢失值旁边的值(这可能有点低效)。它还插值输出中的每个点,而不仅仅是缺少的值(这是非常低效的)。一个更好的方法是插入缺少的值,然后将它们修补到原始图像中。这只是一个快速工作的例子开始:)

相关问题 更多 >