3

我已经能够使用 matplotlib 绘制和显示我的光栅图像。那部分是成功的。我坚持的部分能够以某种方式保存该情节。对于 rasterio,我发现了两个有用的教程:

https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html

https://www.earthdatascience.org/courses/earth-analytics-python/multispectral-remote-sensing-in-python/export-numpy-array-to-geotiff-in-python/

我已经计算了一个名为 NDVI 的函数,通过 matplotlib 我可以使用以下代码以我想要的方式显示它。但是当我将文件保存为 GeoTIFF 时,我桌面上的图像全是黑色的。我也计划重新投影数据,并且我已将代码注释掉。

这是我的代码:

import rasterio
import matplotlib.pyplot as plt
import numpy as np


nirband = r"LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF"

redband =r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF"


#rasterio.windows.Window(col_off, row_off, width, height)
window = rasterio.windows.Window(2000,2000,800,600)

with rasterio.open(nirband) as src:
    subset = src.read(1, window=window)

fig, ax = plt.subplots(figsize=(12,6))
plt.imshow(subset)
plt.title(f'Band 5 Subset')





with rasterio.open(nirband) as src:
    nir = src.read(1, window=window)

with rasterio.open(redband) as src:
    red = src.read(1, window=window)

red = red.astype(float)
nir = nir.astype(float)
np.seterr(divide='ignore', invalid='ignore')

ndvi = np.empty(nir.shape, dtype=rasterio.float32)
check = np.logical_or ( red > 0, nir > 0 )
naip_ndvi = np.where ( check,  (1.0*(nir - red )) / (1.0*( nir + red )),-2 )


fig, ax = plt.subplots(figsize=(12,6))
ndvi = ax.imshow(naip_ndvi)
ax.set(title="NDVI")



with rasterio.open("LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF") as src:
    naip_data_ras = src.read()
    naip_meta = src.profile


with rasterio.open('MyExample.tif', 'w',**naip_meta) as dst:
    dst.write(naip_ndvi, window=window)


# =============================================================================
# with rasterio.open('example.tif') as dataset:
# 
#     # Read the dataset's valid data mask as a ndarray.
#     mask = dataset.dataset_mask()
# 
#     # Extract feature shapes and values from the array.
#     for geom, val in rasterio.features.shapes(
#             mask, transform=dataset.transform):
# 
#         # Transform shapes from the dataset's own coordinate
#         # reference system to CRS84 (EPSG:4326).
#         geom = rasterio.warp.transform_geom(
#             dataset.crs, 'EPSG:4326', geom, precision=6)
# 
#         # Print GeoJSON shapes to stdout.
#         print(geom)
# =============================================================================

这是我使用 matplotlib 时 NDVI 的样子(我想将它作为 GeoTIFF 文件保存到我的桌面):

NDVI

感谢您的任何帮助!

4

1 回答 1

0

您如何查看输出图像?在图像查看器中,或者在可以向文件添加对比度拉伸的 GIS 或遥感软件中?NDVI 值从 -1 到 1 - 值的范围可能太小,您的软件无法自动显示。我最近在修改 PlanetScope 图像时遇到了类似的问题 - 它使用 matplotlib 按预期显示,但 tiff 显示为黑色。

您可以尝试通过将单元格值乘以 100 来缩放输出 - 这可能有助于解决显示问题。您还可以使用可以对图像应用对比度拉伸的软件(QGIS、esri 产品、ImageJ 或图像处理软件)来验证输出图像值

于 2019-06-19T16:15:27.863 回答