欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

NDVI 计算,c# 和 python 代码实现

最编程 2024-04-29 19:29:53
...

以下代码为gpt自动生成

计算NDVI(归一化植被指数)需要计算红外波段和可见光波段的反射率,所以需要使用遥感图像处理软件或库来读取和处理遥感图像数据。以下是一个简单的C#代码示例,用于计算NDVI:

using System;
using System.Drawing;
using Accord.Imaging.Filters;

namespace NDVI
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load the input image
            Bitmap inputImage = new Bitmap("input_image.tif");

            // Extract the red and near-infrared bands
            Bitmap redBand = new ExtractChannel(RGB.R).Apply(inputImage);
            Bitmap nirBand = new ExtractChannel(RGB.G).Apply(inputImage);

            // Convert the images to float format
            Bitmap redBandFloat = new Grayscale(0.2125, 0.7154, 0.0721).Apply(redBand);
            Bitmap nirBandFloat = new Grayscale(0.2125, 0.7154, 0.0721).Apply(nirBand);

            // Calculate the NDVI map
            float[,] ndviMap = new float[inputImage.Width, inputImage.Height];
            for (int x = 0; x < inputImage.Width; x++)
            {
                for (int y = 0; y < inputImage.Height; y++)
                {
                    float red = redBandFloat.GetPixel(x, y).GetBrightness();
                    float nir = nirBandFloat.GetPixel(x, y).GetBrightness();
                    ndviMap[x, y] = (nir - red) / (nir + red);
                }
            }

            // Save the NDVI map as a grayscale image
            Bitmap ndviImage = new Bitmap(inputImage.Width, inputImage.Height);
            for (int x = 0; x < inputImage.Width; x++)
            {
                for (int y = 0; y < inputImage.Height; y++)
                {
                    byte ndviValue = (byte)(ndviMap[x, y] * 255);
                    ndviImage.SetPixel(x, y, Color.FromArgb(ndviValue, ndviValue, ndviValue));
                }
            }
            ndviImage.Save("ndvi_map.tif");
        }
    }
}

以上代码使用Accord.NET库中的滤镜来提取红色和近红外波段,然后使用灰度滤镜将它们转换为浮点格式。然后,它计算每个像素的NDVI值,并将结果保存为灰度图像。请注意,此代码仅适用于单波段图像,如果您有多波段图像,则需要根据具体情况进行修改。
备注:
// 加载多波段图像
var image = new Accord.Imaging.ColorImage("image.tif");

// 获取图像的红色通道
var redChannel = image.GetChannel(0);

// 对红色通道进行直方图均衡化
Accord.Imaging.Filters.HistogramEqualization equalization = new Accord.Imaging.Filters.HistogramEqualization();
equalization.ApplyInPlace(redChannel);

// 将处理后的红色通道重新组合成多波段图像
var processedImage = new Accord.Imaging.ColorImage(redChannel, image.GetChannel(1), image.GetChannel(2));

// 保存处理后的多波段图像
processedImage.Save("processed_image.tif");

using Accord.Imaging.Filters;

// Load an image
Bitmap image = new Bitmap("image.bmp");

// Compute the NDVI
var ndvi = new NDVIFilter();
Bitmap result = ndvi.Apply(image);

以下是另一种实现的代码
Accord.NET 是一个.NET框架下的机器学习和数字信号处理库,其中包含了一些用于多波段图像处理的工具。

NDVI(Normalized Difference Vegetation Index)是一种用于评估植被覆盖度的指标,可以通过计算红外波段和可见光波段的比值来得到。

在Accord.NET中,可以使用以下代码计算多波段图像的NDVI值:

// 加载多波段图像
var image = new Accord.Imaging.UnmanagedImage("path/to/image.tif");

// 定义红外波段和可见光波段的波长
double infraredWavelength = 800;
double visibleWavelength = 600;

// 计算NDVI
var ndvi = Accord.Imaging.Filters.SpectralIndices.NDVI(image, infraredWavelength, visibleWavelength);

关于Accord.NET多波段图像处理的更多信息,请参考官方文档:

https://accord-framework.net/docs/html/T_Accord_Imaging_Filters_SpectralIndices.htm

https://accord-framework.net/docs/html/T_Accord_Imaging_Formats_UnmanagedImage.htm

以下为python实现的支持三波段的代码
from osgeo import gdal

Open the input image and extract the red, green, and near-infrared bands

input_image = gdal.Open("input_image.tif")
red_band = input_image.GetRasterBand(1).ReadAsArray().astype(float)
green_band = input_image.GetRasterBand(2).ReadAsArray().astype(float)
nir_band = input_image.GetRasterBand(3).ReadAsArray().astype(float)

Calculate the NDVI map

ndvi_map = (nir_band - red_band) / (nir_band + red_band + green_band)

Save the NDVI map as a GeoTIFF image

driver = gdal.GetDriverByName("GTiff")
ndvi_image = driver.Create("ndvi_map.tif", input_image.RasterXSize, input_image.RasterYSize, 1, gdal.GDT_Float32)
ndvi_image.SetProjection(input_image.GetProjection())
ndvi_image.SetGeoTransform(input_image.GetGeoTransform())
ndvi_image.GetRasterBand(1).WriteArray(ndvi_map)
ndvi_image.FlushCache()
以上代码使用gdal库打开输入图像,并从中提取红色、绿色和近红外波段。然后,它计算每个像素的NDVI值,并将结果保存为GeoTIFF图像。注意,此代码假定输入图像是三波段图像。如果您的图像包含不同数量的波段,则需要相应地修改代码。

原文地址:https://www.cnblogs.com/wang2650/p/17330328.html

推荐阅读