前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >遥感影像计算Jaccard 相似性系数

遥感影像计算Jaccard 相似性系数

作者头像
Twcat_tree
发布2023-10-16 19:03:40
1130
发布2023-10-16 19:03:40
举报
文章被收录于专栏:二猫の家二猫の家

遥感影像可以为矢量和栅格,这里以栅格为例。影响格式为二值图像

代码语言:javascript
复制
#import rasterio and sklearn
import rasterio
from rasterio.warp import reproject, Resampling, calculate_default_transform
from sklearn.metrics import jaccard_score

#read the first raster image
with rasterio.open("G:\\aa\\二值图像\\globeland301.tif") as src1:
  raster1 = src1.read(1)
  profile1 = src1.profile

#read the second raster image
with rasterio.open("G:\\aa\\二值图像\\CGLS-LC1001.tif") as src2:
  raster2 = src2.read(1)
  profile2 = src2.profile

#resample the first raster to 100m resolution
resampled_raster1 = rasterio.open("resampled_raster1.tif", "w", **profile1)
resampled_raster1.write(raster1, 1)
resampled_raster1.resample(100, Resampling.nearest)
resampled_raster1.close()

#resample the second raster to 100m resolution
resampled_raster2 = rasterio.open("resampled_raster2.tif", "w", **profile2)
resampled_raster2.write(raster2, 1)
resampled_raster2.resample(100, Resampling.nearest)
resampled_raster2.close()

#calculate the destination transform and crs for reprojecting
dst_transform, dst_width, dst_height = calculate_default_transform(
    resampled_raster1.crs, "EPSG:4326",
    resampled_raster1.width, resampled_raster1.height,
    *resampled_raster1.bounds)

#reproject the first raster to WGS-84
reprojected_raster1 = rasterio.open("reprojected_raster1.tif", "w", crs="EPSG:4326",
                                    transform=dst_transform,
                                    width=dst_width,
                                    height=dst_height,
                                    nodata=0) #set nodata value to 0
reproject(
    source=rasterio.band(resampled_raster1, 1),
    destination=rasterio.band(reprojected_raster1, 1),
    src_transform=resampled_raster1.transform,
    src_crs=resampled_raster1.crs,
    dst_transform=reprojected_raster1.transform,
    dst_crs=reprojected_raster1.crs,
    resampling=Resampling.nearest)
reprojected_raster1.close()

#reproject the second raster to WGS-84
reprojected_raster2 = rasterio.open("reprojected_raster2.tif", "w", crs="EPSG:4326",
                                    transform=dst_transform,
                                    width=dst_width,
                                    height=dst_height,
                                    nodata=0) #set nodata value to 0
reproject(
    source=rasterio.band(resampled_raster2, 1),
    destination=rasterio.band(reprojected_raster2, 1),
    src_transform=resampled_raster2.transform,
    src_crs=resampled_raster2.crs,
    dst_transform=reprojected_raster2.transform,
    dst_crs=reprojected_raster2.crs,
    resampling=Resampling.nearest)
reprojected_raster2.close()

#calculate Jaccard similarity coefficient between the two rasters
j_raster = jaccard_score(reprojected_raster1.read(1).flatten(), reprojected_raster2.read(1).flatten())

#print the result
print("Jaccard similarity coefficient between the two rasters:", j_raster)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-07-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档