Python遥感变化监测

图像来源于LEVIR建筑物变化检测数据集(LEVIR-CD)

# 参考《python地理空间分析指南》
import numpy as np
import matplotlib.pyplot as plt
from osgeo import gdal, gdal_array
import cv2
# 加载两幅图像
im_1 = 'levir2/A.tif'
im_2 = 'levir2/B.tif'
# 载入数组
arr_1 = gdal_array.LoadFile(im_1).astype(np.int8)
arr_2 = gdal_array.LoadFile(im_2).astype(np.int8)
# 显示两幅图像
plt.figure(figsize=(20, 40))
plt.subplot(121);plt.imshow(cv2.merge((arr_1[0], arr_1[1], arr_1[2])).astype(np.uint8))
plt.subplot(122);plt.imshow(cv2.merge((arr_2[0], arr_2[1], arr_2[2])).astype(np.uint8))
plt.show()

在这里插入图片描述

# 进行变化检测
diff = arr_1 - arr_2
# 建立类别架构并将变化特征隔离
classes = np.histogram(diff, bins=5)[1]
# 用黑色遮罩遮住不明显的特征
lut = [[0,0,0], [0,0,0], [0,0,0], [255,0,0], [0,255,0], [100,50,255]]
# 类别初始值
start = 1
# 创建输出图片
rgb = np.zeros((3, diff.shape[1], diff.shape[2]), np.int8)
# 处理并分配颜色
for i in range(len(classes)):
    mask = np.logical_and(start <= diff, diff <= classes[i])
    masked = ((mask[0] == mask[1]) == mask[2])
    for j in range(len(lut[i])):
        rgb[j] = np.choose(masked, (rgb[j], lut[i][j]))
    strat = classes[i] + 1
# 保存
out = gdal_array.SaveArray(rgb, 'levir2/change.tif', format='GTiff', prototype=im_2)
out = None
# 查看差值图片
ima = 'levir2/change.tif'
arr = gdal_array.LoadFile(ima).astype(np.uint8)
plt.figure(figsize=(8, 8))
plt.imshow(cv2.merge((arr[0], arr[1], arr[2])))
plt.show()

在这里插入图片描述

  • 2
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值