NDVI 计算,c# 和 python 代码实现
以下代码为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
上一篇: Google Play 内部购买测试
下一篇: 基于 MATLAB 的人民币识别系统
推荐阅读
-
35 岁实现财务*,腾讯程序员手握2300万提前退休?-1000万房产、1000万腾讯股票、加上300万的现金,一共2300万的财产。有网友算了一笔账,假设1000万的房产用于自住,剩下1300万资产按照平均税后20-50万不等进行计算,大约花上26-60年左右的时间才能赚到这笔钱。也就是说,普通人可能奋斗一辈子,才能赚到这笔钱。在很多人还在为中年危机而惶惶不可终日的时候,有的人的35岁,就已经安全着陆,试问哪个打工人不羡慕?但问题是有这样财富积累必然有像样的实力做靠山。没有人可以不劳而获。 看到这里,肯定有人说,那么对于普通人来说,卷可能真就成了唯一的出路。但是卷也有轻松的卷,“偷懒”的卷法,对于程序员而言,刨除掉一时无法改掉的开会传统占用的大部分时间,如何把有限的时间和精力放在真正重要的架构设计、需求设计上,而不是重复的造*,编码、改bug、手动测试。因此在科技改变生活的今天,学会使用AI工具成为程序员们的必备技能。 以全栈式全自动的软件开发工具飞算SoFlu软件机器人为例,作为全球首款面向微服务架构设计和最佳实践的软件机器人,SoFlu软件机器人改变了原来手工编码的作业模式,通过可视化拖拽方式以及参数配置就能实现等同于编写复杂代码的业务逻辑,在设计业务逻辑时就完成了微服务应用开发,做到“业务即图,图即代码”,实现“软件开发,十倍提效”。 而作为飞算SoFlu软件机器人的一个重要组成部分,FuncGPT(慧函数)支持所有类型函数创建。通过自然语言描述Java函数需求,实时生成高质量、高可读性的Java函数代码。生成代码可直接复制到IDEA,或一键导入Java全自动开发工具函数库。 FuncGPT(慧函数)具有五大核心能力: ● 自然语言:通过自然语言即可生成函数,降低软件开发门槛。 ● 秒级函数开发:全自动秒级开发函数,秒级完成,效率千倍提升。 ● 一站式函数开发应用:函数生成即可直接应用,效率、安全有保障。 ● 质量可靠:生成的函数代码符合业界规范、具备良好的可读性,符合最优解。 ● 灵活生成:可以根据开发人员的具体需求生成并支持调整和修改。 以“将文件流转换为File对象”这个需求为例,以下是FuncGPT(慧函数)秒级生成的代码: / 类名:FileConverter // 函数名:convertStreamToFile // 函数功能:将文件流转换为File对象 // POM依赖包:无 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileConverter { /** * 将文件流转换为File对象 * @param inputStream 文件流 * @param filePath 文件路径 * @param fileName 文件名 * @return 转换后的File对象 * @throws IOException 如果发生I/O异常 */ public static File convertStreamToFile(InputStream inputStream, String filePath, String fileName) throws IOException { File file = new File(filePath + File.separator + fileName); // 创建File对象 try (FileOutputStream outputStream = new FileOutputStream(file)) { // 创建文件输出流 byte buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { // 从文件流读取数据并写入文件 outputStream.write(buffer, 0, bytesRead); } } return file; // 返回转换后的File对象 } } // 函数示例 // 将文件流转换为File对象示例 // 入参:inputStream,文件流 // 入参:filePath,文件路径 // 入参:fileName,文件名 // 出参:file,转换后的File对象 // 调用示例: // InputStream inputStream = new FileInputStream("example.txt"); // String filePath = "C:\\Users\\User\\Documents"; // String fileName = "example.txt"; // File file = FileConverter.convertStreamToFile(inputStream, filePath, fileName); // System.out.println(file.getAbsolutePath); // 输出结果:例如,将文件流转换为File对象后,文件的绝对路径为:C:\Users\User\Documents\example.txt // 则输出结果为:C:\Users\User\Documents\example.txt 通过分析,不难发现以上代码:
-
贪婪算法在 Python、JavaScript、Java、C++ 和 C# 中的多种实现及其在硬币变化、分数骑士、活动选择和使用哈夫曼编码的最小生成树问题中的应用实例
-
NDVI 计算,c# 和 python 代码实现
-
NW 小世界网络模型的 python 代码实现和平均路径聚类系数计算
-
用一行 Python 代码实现 for 循环和 if else 判断。
-
粒子群算法和鲸鱼算法的比较(Matlab 代码实现 粒子群算法(带约束处理)--Python 和 Matlab 鲸鱼优化算法的实现(Matlab 实现)
-
线性和非线性数据降维方法汇总(Python 代码实现)
-
Canny 和 Hough 算法的 Python 实现 代码示例分析
-
[姿势估计] 实践记录:使用 Dlib 和 mediapipe 进行人脸姿势估计 - 本文重点介绍方法 2):方法 1:基于深度学习的方法:。 基于深度学习的方法:基于深度学习的方法利用深度学习模型,如卷积神经网络(CNN)或递归神经网络(RNN),直接从人脸图像中学习姿势估计。这些方法能够学习更复杂的特征表征,并在大规模数据集上取得优异的性能。方法二:基于二维校准信息估计三维姿态信息(计算机视觉 PnP 问题)。 特征点定位:人脸姿态估计的第一步是通过特征点定位来检测和定位人脸的关键点,如眼睛、鼻子和嘴巴。这些关键点提供了人脸的局部结构信息,可用于后续的姿势估计。 旋转表示:常见的旋转表示方法包括欧拉角和旋转矩阵。欧拉角通过三个旋转角度(通常是俯仰、偏航和滚动)描述头部的旋转姿态。旋转矩阵是一个 3x3 矩阵,表示头部从一个坐标系到另一个坐标系的变换。 三维模型重建:根据特征点的定位结果,三维人脸模型可用于姿势估计。通过将人脸的二维图像映射到三维模型上,可以估算出人脸的旋转和平移信息。这就需要建立人脸的三维模型,然后通过优化方法将模型与特征点对齐,从而获得姿势估计结果。 特征点定位 特征点定位是用于检测人脸关键部位的五官基础部分,还有其他更多的特征点表示方法,大家可以参考我上一篇文章中介绍的特征点检测方案实践:人脸校正二次定位操作来解决人脸校正的问题,客户在检测关键点的代码上略有修改,坐标转换部分客户见上图 def get_face_info(image). img_copy = image.copy image.flags.writeable = False image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = face_detection.process(image) # 在图像上绘制人脸检测注释。 image.flags.writeable = True image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) box_info, facial = None, None if results.detections: for detection in results. for detection in results.detections: mp_drawing.Drawing.detection = 无 mp_drawing.draw_detection(image, detection) 面部 = detection.location_data.relative_keypoints 返回面部 在上述代码中,返回的数据是五官(6 个关键点的坐标),这是用 mediapipe 库实现的,下面我们可以尝试用另一个库:dlib 来实现。 使用 dlib 使用 Dlib 库在 Python 中实现人脸关键点检测的步骤如下: 确保已安装 Dlib 库,可使用以下命令: pip install dlib 导入必要的库: 加载 Dlib 的人脸检测器和关键点检测器模型: 读取图像并将其灰度化: 使用人脸检测器检测图像中的人脸: 对检测到的人脸进行遍历,并使用关键点检测器检测人脸关键点: 显示绘制了关键点的图像: 以下代码将参数 landmarks_part 添加到要返回的关键点坐标中。
-
Python 与 MySQL 的连接 - III.表结构和代码实现