GEE中如何使用JS代码创建水体蒙版
时间: 2023-12-09 07:03:49 浏览: 48
在GEE中创建水体蒙版的一种常见方法是使用NDWI指数。NDWI(归一化水体指数)是一种基于近红外和绿色波段的指数,可以用于检测水体。下面是一个使用JS代码创建水体蒙版的示例:
```
// 选择Landsat 8 OLI图像集
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(geometry)
.filterDate('2018-01-01', '2018-12-31');
// 创建NDWI图像
var ndwi = collection.map(function(image) {
return image.normalizedDifference(['B3', 'B5']).rename('NDWI');
});
// 创建水体蒙版
var waterMask = ndwi.gt(0.2);
// 可视化水体蒙版
Map.addLayer(waterMask, {palette: 'blue'}, 'Water Mask');
```
在这个示例中,我们选择Landsat 8 OLI图像集,并使用`normalizedDifference()`函数计算NDWI。然后,我们使用`.gt()`函数创建一个水体蒙版,其中NDWI值大于0.2的像素被标记为水体。最后,我们使用`Map.addLayer()`函数将水体蒙版可视化。你可以根据你的需要调整NDWI阈值来生成更准确的水体蒙版。
相关问题
GEE计算NDVI并让水体显示为零的JS代码
以下是计算 NDVI 并将水体显示为零的 Google Earth Engine(GEE)的 JavaScript 代码:
```
// 选择需要处理的图像集合
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2019-01-01', '2019-12-31')
.filterBounds(geometry);
// 定义函数来计算 NDVI
function addNDVI(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
}
// 应用 NDVI 函数到图像集合
var withNDVI = collection.map(addNDVI);
// 定义阈值来将水体显示为零
var waterThreshold = 0.1;
// 创建水体蒙版
var waterMask = withNDVI.select('NDVI').lt(waterThreshold);
// 将水体蒙版应用到 NDVI 图像上
var maskedNDVI = withNDVI.updateMask(waterMask.not());
// 可视化 NDVI 图像
Map.centerObject(geometry);
Map.addLayer(maskedNDVI.select('NDVI'), {min: -1, max: 1, palette: ['blue', 'white', 'green']}, 'NDVI masked');
```
在上面的代码中,首先选择了需要处理的图像集合,并定义了一个函数来计算 NDVI。然后将 NDVI 函数应用到图像集合中,并根据阈值创建了水体蒙版。最后将水体蒙版应用到 NDVI 图像上并进行可视化。
在gee中对提取的水体进行精度验证的代码
对提取的水体进行精度验证的主要方法是与真实水体分布进行对比,常用的统计指标包括准确度、精度、召回率、F1值等。在gee中,可以利用混淆矩阵来计算这些指标。
以下是一个简单的示例代码,假设我们已经从一幅遥感影像中提取了水体,并且有一个真实水体分布的矢量文件:
```javascript
// 读取影像和真实水体分布
var image = ee.Image("image_id");
var water_truth = ee.FeatureCollection("water_truth_id");
// 提取水体,假设水体像元值为1
var water_extracted = image.eq(1);
// 计算混淆矩阵
var confusion_matrix = water_extracted
.and(water_truth)
.rename('true_positive').addBands(ee.Image(1).where(water_extracted.not().and(water_truth), 1).rename('false_negative'))
.addBands(ee.Image(1).where(water_extracted.and(water_truth.not()), 1).rename('false_positive')).reduceRegion({
reducer: ee.Reducer.sum().forEachBand(['true_positive', 'false_negative', 'false_positive']),
geometry: image.geometry(),
scale: image.projection().nominalScale(),
});
// 计算准确度、精度、召回率、F1值
var accuracy = confusion_matrix.getNumber('true_positive').add(confusion_matrix.getNumber('true_negative')).divide(confusion_matrix.getNumber('true_positive').add(confusion_matrix.getNumber('true_negative').add(confusion_matrix.getNumber('false_positive').add(confusion_matrix.getNumber('false_negative')))));
var precision = confusion_matrix.getNumber('true_positive').divide(confusion_matrix.getNumber('true_positive').add(confusion_matrix.getNumber('false_positive')));
var recall = confusion_matrix.getNumber('true_positive').divide(confusion_matrix.getNumber('true_positive').add(confusion_matrix.getNumber('false_negative')));
var f1 = precision.multiply(recall).multiply(2).divide(precision.add(recall));
// 输出结果
print('Accuracy: ', accuracy);
print('Precision: ', precision);
print('Recall: ', recall);
print('F1: ', f1);
```
需要注意的是,以上代码仅为示例,实际应用中可能需要根据具体情况进行修改和完善。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![js](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)