# 基于深度学习识别湖泊，以洞庭湖区域为例

1、安装环境

2、识别影像准备

B2: Blue
B3: Green
B4: Red
B5: Near Infrared (NIR)
B6: Shortwave Infrared 1 (SWIR1)
B7: Shortwave Infrared 2 (SWIR2)

import numpy as np
from osgeo import gdal_array
from osgeo import gdal

"""存储文件的列表维度为：imgs*11，既每个影像的波段文件都存放在单个列表里"""
import os

"""保存landsat8不同波段的路径(共11个波段)
base_path: 存储了多张影像的文件夹
mid_path: 存储了不同波段的文件夹，对应着单张影像
final_path: 最后一层可以直接打开的单张波段文件

bands：包含了波段路径的列表
"""

# 用于存储不同波段路径,维度是影像数量*波段数（11）
bands = []
num_bands = 11

# 用于定位波段用的关键字列表
keys = []
for k in range(num_bands):
key = 'B{num}.TIF'.format(num = k + 1)
keys.append(key)

# 读取最外层文件
base_files = os.listdir(base_path)
for i in range(len(base_files)):
bands.append([])

# 读取中层文件
mid_path = base_path + '\' + base_files[i]
mid_file = os.listdir(mid_path)

# 得到最内层的波段文件
for final_file in mid_file:
final_path = mid_path + '\' + final_file

for j in range(num_bands):
if keys[j] in final_file:
bands[i].append(final_path)

# 原始列表排序是1,10,11,2,3，...
# 按照倒数第5个字符进行排序（XXXB1.TIF）
bands[i].sort(key=lambda arr: (arr[:-5], int(arr[-5])))

# 返回波段列表和影像数量
return bands

base_path = r'G:洞庭湖input'

print(bands[0][:11])

# 读取波段
B2 = bands[0][1]
B3 = bands[0][2]
B4 = bands[0][3]
B5 = bands[0][4]
B6 = bands[0][5]
B7 = bands[0][6]

# 转化成ndarray形式
#B1_np = np.array(B1_gdal)
B_merger = np.array([B2_np, B3_np, B4_np,B5_np, B6_np, B7_np])

# 获取tif遥感影像的数据与投影信息
dataset1 = gdal.Open(B2)
im_geotrans = dataset1.GetGeoTransform()
im_proj = dataset1.GetProjection()
im_width = dataset1.RasterXSize
im_height = dataset1.RasterYSize
#im_bands = B_merger.shape
im_bands =6
band1 = dataset1.GetRasterBand(1)
datatype = band1.DataType

# 生成tif遥感影像
driver = gdal.GetDriverByName('GTiff')  # 数据类型必须有，因为要计算需要多大内存空间
dataset = driver.Create(base_path+'merger.tif', im_width, im_height, im_bands, datatype)
dataset.SetProjection(im_proj)  # 写入投影
dataset.SetGeoTransform(im_geotrans)  # 写入仿射变换参数
if im_bands == 1:
dataset.GetRasterBand(1).WriteArray(B_merger)  # 写入数组数据
else:
for i in range(im_bands):
dataset.GetRasterBand(i + 1).WriteArray(B_merger[i])
del dataset

3、运行模型识别湖泊

\$ python inference.py --checkpoint_path checkpoints/cp.135.ckpt --image_path sample_data/sentinel2_example.tif --save_path water_map.png

THE END