提问人:hex93 提问时间:11/13/2023 最后编辑:hex93 更新时间:11/14/2023 访问量:48
如何使用高斯在 python 的 2D 图像中插入缺失区域?[已关闭]
How do I interpolate a missing area in a 2D image in python using Gaussians? [closed]
问:
我一直在很多地方寻找这个问题的答案,但我觉得索引只是让我变得更好。我有一个 2D 高斯图像,它的灰度值如下所示
#visual 检查显示,有狭缝的区域是第 169 至 185 行,第 0 至 179 列。以下是从 TIF 文件 ('Cropped.tif') 加载图像的代码,以及从 RGB 转换为灰度的脚本。
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
#plot the grayscale image
plt.title("Image Grayscale")
plt.xlabel("X pixels")
plt.ylabel("Y Pixels Label")
image_unprocessed=mpimg.imread('Cropped.tif')
plt.imshow(rgb2gray(image_unprocessed))
plt.show()
image_grayscale=rgb2gray(image_unprocessed)
现在我想插入出现在中间的那个小缝隙。.我基本上所做的是将狭缝中的值设置为 nan manally。图像本身是image_grayscale的,这是一个 354x342 numpy 数组,我想在区域 [169:195,0:195] 中插值它。我的mwe如下
from scipy import interpolate
image_grayscale[169:195,0:195]=np.nan
x = np.arange(0, image_grayscale.shape[1])
y = np.arange(0, image_grayscale.shape[0])
image_grayscale=np.ma.masked_invalid(image_grayscale)
xx,yy=np.meshgrid(x,y)
x1=xx[~image_grayscale.mask]
y1=yy[~image_grayscale.mask]
new_imagegreyscale=image_grayscale[~image_grayscale.mask]
GD1 = interpolate.griddata((x1, y1), new_imagegreyscale.ravel(),
(xx, yy),
method='linear')
plt.title("Image interpolated")
plt.xlabel("X pixels")
plt.ylabel("Y Pixels Label")
plt.imshow(GD1)
plt.show()
这返回了下图,这还不错,但绝对不完美,你可以看到中间附近有我想摆脱的瑕疵
但是,我觉得如果我可以让插值器知道使用高斯拟合而不是线性或三次拟合,那么插值图像可能会更好,但我不确定这将如何改变语法。我可以看到 scipy.interpolate 有很多不同的插值器,但我不确定 1) 哪种算法最适合这种情况 2) 实现该算法的语法与我上面的语法有何不同。我看到 RBFInterpolator 可以选择使用高斯基函数,但是当我如上所述尝试 RBF 插值器时
from scipy.interpolate import RBFInterpolator
image_grayscale[169:195,0:195]=np.nan
x = np.arange(0, image_grayscale.shape[1])
y = np.arange(0, image_grayscale.shape[0])
image_grayscale=np.ma.masked_invalid(image_grayscale)
xx,yy=np.meshgrid(x,y)
x1=xx[~image_grayscale.mask]
y1=yy[~image_grayscale.mask]
new_imagegreyscale=image_grayscale[~image_grayscale.mask]
GD1 = RBFInterpolator((x1, y1), new_imagegreyscale.ravel(),
(xx, yy))
plt.title("Image interpolated")
plt.xlabel("X pixels")
plt.ylabel("Y Pixels Label")
plt.imshow(GD1)
plt.show()
我收到值错误“预计第一个轴的长度为 2.”。我可以看到这是一个语法问题,但我一直在努力理解正确的语法。很抱歉,如果之前有人问过这个问题,但我在 2D 插值上发现的其他线程并没有完全给我带来我想要的清晰度。d
答: 暂无答案
评论
NameError: name 'image_grayscale' is not defined