惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
B
Blog RSS Feed
I
InfoQ
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
H
Help Net Security
L
LangChain Blog
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏

博客园 - 过错

vscode的continue中配置第三方模型。以agnes-ai为例 在 Docker 容器中运行 .NET 6 并使用 libgdiplus(通常是为了支持 System.Drawing.Common 进行图片处理),你需要完成以下两个核心步骤 vs code调试netcore 为Ubuntu24生成netcore10的镜像, linux安装netcore nginx postgresql ssh 各种加速 不使用Debezium,记录PostgreSQL中的数据的数据前后变化 docker加速 python的一些设置 netcore 发布命令 一些代码库 The database cluster initialisation failed but was not the same version as initdb的解决办法(postgresql) 使用c#调用chatgpt 。以下代码由ai自动生成。 vs Commit message的使用 Fiddler 替换资源 docker 部署 rabbitmq(持久化) 和postgresql redis mysql 油猴子常用脚本 通过nginx做身份验证网关的方法 ngnix 常用配置 Nginx配置之实现多台服务器负载均衡 Nginx配置优化详解 ngnix代理grpc
NDVI计算 ,c#和python代码实现
过错 · 2023-04-18 · via 博客园 - 过错

以下代码为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图像。注意,此代码假定输入图像是三波段图像。如果您的图像包含不同数量的波段,则需要相应地修改代码。