Segment Anything模型用于地理空间数据

原文地址:https://samgeo.gishub.org/examples/satellite/

此笔记本通过几行代码展示了如何使用 Segment Anything Model (SAM) 来使用分段卫星图像。确保为此jupyter notebook使用 GPU 运行时。

安装依赖


取消注释并运行以下单元格以安装所需的依赖项。

 # %pip install segment-geospatial leafmap localtileserver

导入库

import os
import leafmap
import torch
from samgeo import SamGeo, tms_to_geotiff

创建地图应用

m = leafmap.Map(center=[29.676840, -95.369222], zoom=19)
m.add_basemap('SATELLITE')
m
if m.user_roi_bounds() is not None:
    bbox = m.user_roi_bounds()
else:
    bbox = [-95.3704, 29.6762, -95.368, 29.6775]

下载地图图块

下载地图图块并将它们镶嵌到单个 GeoTIFF 文件中

image = 'satellite.tif'
# image = '/path/to/your/own/image.tif'
 tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')
 m.add_raster(image, layer_name='Image')

        9e28f0448f770ec4b14e91cfa8425771.png

初始化 SAM 类

out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
checkpoint = os.path.join(out_dir, 'sam_vit_h_4b8939.pth')
device = 'cuda' if torch.cuda.is_available() else 'cpu'
sam = SamGeo(
    checkpoint=checkpoint,
    model_type='vit_h',
    device=device,
    erosion_kernel=(3, 3),
    mask_multiplier=255,
    sam_kwargs=None,
)

分割图像

mask = 'segment.tiff'
sam.generate(image, mask)

多边形化栅格数据

#将分割结果保存为 GeoPackage 文件。
vector = 'segment.gpkg'
sam.tiff_to_gpkg(mask, vector, simplify_tolerance=None)
#您还可以将分割结果保存为 GeoPandas 支持的任何矢量数据格式。
shapefile = 'segment.shp'
sam.tiff_to_vector(mask, shapefile)

可视化结果

style = {
    'color': '#3388ff',
    'weight': 2,
    'fillColor': '#7c4185',
    'fillOpacity': 0.5,
}
m.add_vector(vector, layer_name='Vector', style=style)
m

9d19788b0463ebb7cf11d1bb1f30a142.png

32a6092724d4ecf212fc2fcafffec444.gif

1、ArcGIS Pro 3.0 软件破解版安装及教程

2、中国市级行政区shp(WGS84)【2022 · 最新数据共享】

3、中国县级行政区【2022 · 最新数据共享】‍

4、IntelliJ IDEA 2022.3.1 专业版破解

5、GIS开发者应考虑使用 Edge浏览器的 8 个理由

6、ArcGIS Pro中常用的空间分析功能

7、开源 Web GIS 制作3D建筑

8、ArcGIS Pro 和 ChatGPT集成思路

10a0a190424f47768b86de394cd70fef.gif

5492749aeb944978123e1c76d352e034.gif

点分享

e9d9dede180a4bb5f3bffb26f77eb2e9.gif

点点赞

e0fccf40fe95cb6e07af66b6a37720b0.gif

点在看

猜你喜欢

转载自blog.csdn.net/qq_35918970/article/details/131099044
今日推荐