首页 > 解决方案 > matplotlib.imshow() 复制图像

问题描述

我正在使用 Python3、earthpy、rasterio、numpy 和 matplotlib 处理 Sentinel-2 卫星图像和分析。你可以参考这里的完整代码。我正在尝试分别使用红色、绿色和蓝色波段来创建 RGB 复合图像。

如果您参考代码,您可以看到以下代码行生成了黑白图像,而不是彩色图像-

rgb = ep.plot_rgb( arr = stacked_s2_sentinel_img, rgb = (1, 2, 3), figsize =(20, 10) #, title = titles )
plt.show()

为了缓解这个问题,我采用了使用 numpy 的 'nanpercentile()' 函数来计算对比度拉伸边界的方法,然后使用这些边界和 numpy 的 'interp()' 函数在 0 和 1 之间使用这些函数动态拉伸图像界限。支持代码为:

blue_stretch.shape, green_stretch.shape, red_stretch.shape
# ((1830, 1830), (1830, 1830), (1830, 1830))
rgb_img = np.stack([red_stretch, green_stretch, blue_stretch])
rgb_img.shape
# (3, 1830, 1830)
rgb_img = rgb_img.reshape(1830, 1830, 3)
rgb_img.shape
# (1830, 1830, 3)
plt.figure(figsize = (10, 8))
plt.imshow(rgb_img, interpolation='nearest')
plt.show()

但这显示了相同的图像在 3 x 3 网格中重复了 9 次。怎么了?此外,图像看起来完全错误。

生成的图像如下所示 - 结果图像

怎么了?

标签: pythonmatplotlibrasteriosatellite-image

解决方案


推荐阅读