Python遥感图像处理指南(5)-投影、镶嵌、裁剪和叠加

        在前几章中,我们可以提取到水体掩膜,但我们的算法有时候不是很准确,需要其他数据源来进行对比分析,本章我们将利用全球水面数据集来进行教学操作,该数据提供了过去三十六年全球范围内水面的位置和时间分布图,并提供了有关水面范围和变化的统计数据,这是一个非常实用的全球水资源研究数据库。

1、 数据下载

        进入网站(自己想办法),找到之前哨兵影像对应的区块,我之前下载的是位于巴西的70W 0N and 60W 0N两个图幅,只需点击相应的数据块,然后选择下方出现的出现链接即可。

Python遥感图像处理指南(5)-投影、镶嵌、裁剪和叠加_第1张图片

2、加载数据

        下载数据后,我们就可以使用 rasterio 打开这两张图。此外,我们还将使用之前文章中定义的 load_landsat_image 加载图像,但只需做一个非常简单的修改。我们将返回一个包含数据集的字典,而不是仅仅以数组形式返回包含波段的图像字典,使用 matplotlib 的 subplots 函数来显示最近加载的图像

import rasterio
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np

def load_landsat_image(img_folder, bands):
    # initialize the dictionaries
    image = {}
    ds = {}
    
    path = Path(img_folder)
    
    # loop through the given bands to load
    for band in bands:
        # considering the landsat images end with *_SR_B#.TIF, we will use it to locate the correct file
        file = next(path.glob(f'*{band}.tif'))
        print(f'Opening file {file}')
        ds.update({band: rasterio.open(file)})
        image.update({band: ds[band].read(1)})

    return image, ds

# load the image
img, image_ds = load_landsat_image('D:/Images/Input/Landsat/LC08_L2SP_231062_20201026_20201106_02_T1/', ['B2', 'B3', 'B4'])

# create the RGB for the image
rgb = np.stack([img['B4'], img['B3'], img['B2']], axis=2)
rgb = rgb / rgb.max()
               
# load the water masks
water1_ds = rasterio.open('D:/Images/Input/Landsat/occurrence_70W_0Nv1_1_2019.tif')
water2_ds = rasterio.open('D:/Images/Input/Landsat/occurrence_60W_0Nv1_1_2019.tif')

water1_array = water1_ds.read(1)
water2_array = water2_ds.read(1)

# Plot the 3 images
fig, ax = plt.subplots(1, 3, figsize=(20, 6))
ax[0].imshow(rgb*3)

ax[1].i

你可能感兴趣的:(Python数据处理,python,图像处理,开发语言)