import rasterio
from rasterio.plot import show
# 打开遥感影像文件
image_path = 'E:/.tif' # 替换为你的遥感影像文件路径
dataset = rasterio.open(image_path)
# 读取红光波段(B4波段)和红外光波段(B8波段)
red_band = dataset.read(3 - 1) # 红光波段索引为3(从1开始计数),减一得到Python的索引
infrared_band = dataset.read(8 - 1) # 红外光波段索引为8(从1开始计数),减一得到Python的索引
# 显示红光波段和红外光波段
show(red_band, cmap='Reds', title='Red Band')
show(infrared_band, cmap='Greys', title='Infrared Band')
# 保存红光波段和红外光波段为图像文件
# 保存红光波段为图像文件
output_dir = 'E:/'
red_band_output_path = output_dir + '/red_band.tif'
with rasterio.open(
red_band_output_path,
'w',
driver='GTiff',
height=red_band.shape[0],
width=red_band.shape[1],
count=1,
dtype=red_band.dtype,
crs=dataset.crs,
transform=dataset.transform
) as red_band_output:
red_band_output.write(red_band, 1)
# 保存红外光波段为图像文件
infrared_band_output_path = output_dir + '/infrared_band.tif'
with rasterio.open(
infrared_band_output_path,
'w',
driver='GTiff',
height=infrared_band.shape[0],
width=infrared_band.shape[1],
count=1,
dtype=infrared_band.dtype,
crs=dataset.crs,
transform=dataset.transform
) as infrared_band_output:
infrared_band_output.write(infrared_band, 1)
# 关闭数据集
dataset.close()
用于读取并显示遥感影像文件中的红光波段和红外光波段,并将它们保存为图像文件
最新推荐文章于 2024-07-22 16:24:23 发布