波段分离 单波段数据制造

该代码段主要利用Python的os、pathlib、collections和rasterio库,对特定目录下的多波段图像数据进行处理。它首先创建目标文件夹,然后按顺序读取landsat和modis的波段,将每个波段拆分成单独的文件,重命名为指定格式,并保存到对应目录。处理过程涉及图像读取、写入、文件路径管理和目录创建。
摘要由CSDN通过智能技术生成
import os
from pathlib import Path
from collections import OrderedDict
import rasterio

#需要提前在final_data_path中创造band1,band2,band3,band4,band5,band6文件夹

NUM_BAND = 6
current_processing = 'val'  #‘train’,  ‘val’,  ‘test’
ini_data_path = Path('D:\\STFdata\\zdd_data1\\lgc2400').joinpath(current_processing)
final_data_path ='D:\\STFdata\\zdd_data1\\单波段数据\\lgc2400\\'
image_dirs = [p for p in ini_data_path.glob('*') if p.is_dir()]

order = OrderedDict()
order[0] = '00_landsat'
order[1] = '00_modis'
order[2] = '01_landsat'
order[3] = '01_modis'

# path = 'E:\\STF_note\\08.时空融合神经网络代码\\EDCSTFN\\data_cia\\train\\2001_290_1017\\00_landsat.tif'
# file_name = Path('E:\\STF_note\\08.时空融合神经网络代码\\EDCSTFN\\data_cia\\train\\2001_290_1017\\1')
# file_name.mkdir(exist_ok=True)
# img = rasterio.open(path)
# current_band = img.read(1)
# profile = img.profile
# profile.update(count=1)
# result = rasterio.open(file_name, 'w',**profile)
# result.write(current_band, 1)
# result.close()

for img_path in image_dirs:
    for img in Path(img_path).glob('*.tif'):
        img_info = rasterio.open(img)
        profile = img_info.profile
        profile.update(count=1)
        for prefix in order.values():
            if img.name.startswith(prefix):
                for i in range(NUM_BAND):
                    current_band = img_info.read(i+1)
                    new_path = final_data_path + 'band'+ str(i+1)+'\\'+current_processing
                    Path(new_path).mkdir(exist_ok=True)
                    new_path = new_path + '\\'+img_path.name
                    Path(new_path).mkdir(exist_ok=True)
                    file_name = new_path+'\\'+prefix+'.tif'
                    result = rasterio.open(file_name, 'w', **profile)
                    result.write(current_band, 1)
                    result.close()
        img_info.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值