【问题标题】:Calculating NDVI using python results in all zeros使用 python 计算 NDVI 结果全为零
【发布时间】:2023-02-09 16:39:00
【问题描述】:

我想从 Sentinel-2 图像计算 ndvi。

import os
import numpy as np
import rasterio as rio

# suppress true divide warning numpy for 0 divide
np.seterr(divide='ignore', invalid='ignore')

red_f = absolute/path/to/band/4
nir_f = absolute/path/to/band/8

def calc_ndvi():
    
    with rio.open(red_f) as src:
        red = src.read()
        red = red.astype(np.float64)
    
    with rio.open(nir_f) as src:
        nir = src.read()
        nir = red.astype(np.float64)

    ndvi = np.divide((nir - red),(nir + red))
    
    return ndvi


ndvi = calc_ndvi()

“red”和“nir”最初作为“Array of uint16”加载,形状为 (1, 10980, 10980)。在使用 astype 进行计算之前,我将其转换为浮点数。据我所知,没有必要将数组展平为二维形状。我已经试过了,但这没有用。

不幸的是,结果是一个完全由 0 填充的数组。

我究竟做错了什么?

【问题讨论】:

  • 它应该是nir = nir.astype(np.float64)而不是nir = red.astype(np.float64)

标签: python arrays numpy rasterio


【解决方案1】:

你有一个错字:

nir = red.astype(np.float64)

应该:

nir = nir.astype(np.float64)

在:

ndvi = np.divide((nir - red),(nir + red))

你真的在做:

ndvi = np.divide((red - red),(red + red))

结果为 0 数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 2020-02-26
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    相关资源
    最近更新 更多