Python 使用numpy和scipy填充图像上的间隙
随附图像(test.tif)。 np.nan值是最白的区域。 如何使用一些使用邻域值的间隙填充算法来填充那些最白色的区域Python 使用numpy和scipy填充图像上的间隙,python,image-processing,numpy,scipy,Python,Image Processing,Numpy,Scipy,随附图像(test.tif)。 np.nan值是最白的区域。 如何使用一些使用邻域值的间隙填充算法来填充那些最白色的区域 如果需要来自最近邻居的值,可以使用from scipy.interpolate。你也可以考虑。 您可以通过以下方式找到NaN值的X、Y索引值: import numpy as np nan_locs = np.where(np.isnan(data)) 还有一些其他插值选项。一个选项是用a的结果替换NaN值(但是您的区域对于这个来说有点大)。另一个选择可能是。正确的插值
如果需要来自最近邻居的值,可以使用from scipy.interpolate。你也可以考虑。 您可以通过以下方式找到NaN值的X、Y索引值:
import numpy as np
nan_locs = np.where(np.isnan(data))
还有一些其他插值选项。一个选项是用a的结果替换NaN值(但是您的区域对于这个来说有点大)。另一个选择可能是。正确的插值取决于端域
如果以前没有使用过SciPy ND插值器,则需要提供X、Y和值数据,以使插值器与要插值的值的X和Y数据相匹配。您可以使用上面的where示例作为模板来完成此操作。我认为viena的问题与问题更相关
以下是一些想法:
- 为了填补黑白图像中的空白,您可以使用一些填充算法,如。但是你有一个灰度图像,所以你不能使用它
- 我想您不想使用复杂的修复算法。我的第一个建议是:不要尝试使用最近的灰度值(你不知道NaN像素的真实值)。使用最接近的值将生成脏算法。相反,我建议您使用其他值(例如行的平均值)来填补空白。您可以使用以下方法进行无需编码的操作:
- 使用最接近值的脏溶液可以是: 1) 查找NaN区域的周界点 2) 计算NaN点和周长之间的所有距离 3) 用最近点的灰度值替换NaN
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()
输出:
这会将我们拥有的所有值传递给插值器,而不仅仅是缺失值旁边的值(这可能有点低效)。它还插值输出中的每个点,而不仅仅是缺少的值(效率极低)。更好的方法是只插值缺失的值,然后将它们修补到原始图像中。这只是一个快速入门的示例:)OpenCV有一些可以使用的图像绘制算法。您只需要提供一个二进制掩码,它指示应该绘制哪些像素
导入cv2
将numpy作为np导入
导入scipy.ndimage
数据=ndimage.imread(“test.tif”)
掩码=np.isnan(数据)
inpainted_img=cv2.inpaint(img,mask,inpaintRadius=3,flags=cv2.inpaint_TELEA)
查看scipy插值库,找到适合您需要的函数。对于那些寻找sklearn.preprocessing.inputer
:功能已移至sklearn.inpute.simplemputer
下,请参阅文档。最近邻(“脏解决方案”)也已在sklearn.impute.knimputer
中实现,其文档可以找到。
>>> 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]])
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()