Skip to main content

Split Raster

·448 words·3 mins

GitHub 项目仓库:https://github.com/cuicaihao/split_raster

介绍 #

Split Raster 是一个开源且高度通用的 Python 软件包,旨在轻松地将大图分解为更小、更易于管理的瓦片(tiles)。虽然该软件包特别适用于深度学习和计算机视觉任务,但它也可以应用于广泛的其他应用场景。

最初由作者开发,旨在为深度学习和计算机视觉任务提供最佳支持,Split Raster 专门针对卫星图像以及遥感方法的图像分割任务进行了设计。通过从输入栅格(raster)数据集中生成瓦片图像样本,Split Raster 能够对大图进行更高效、更实用的分析。该软件包还包含随机采样功能,可生成固定数量的瓦片以便进行早期实验。

例如,假设您有一组 RGB 图像和真值(Ground Truth, GT)图像,每个图像的尺寸为 1000x1000 像素。利用 Split Raster,您可以轻松生成 16 张瓦片,每个瓦片的尺寸为 256x256 像素,且边缘自动进行填充(padding)。该软件包还允许自定义瓦片大小和重叠度(overlap),以更好地满足个人项目需求。此外,Split Raster 会自动处理输出图像的填充和命名,从而节省时间和精力(例如:0001.png, 0002.png, …, 9999.png)。

生成上述示例图像的教程 #

打开 Jupyter Notebook 标准图像分块

本教程将向您展示如何使用该软件包将大图拆分为小瓦片。这样您就可以将这些小瓦片用于您的深度学习和计算机视觉任务。

本教程中使用的是 pytorch。您也可以使用 tensorflow 或其他深度学习框架。

安装软件包 #

环境要求 #

  • Python >= 3.10
  • 核心依赖:
    • numpy >= 1.25.0
    • tqdm >= 4.64.0
    • scikit-image >= 0.21.0

安装方式 #

uv pip install splitraster

或通过 pip:

pip install splitraster

如需 GIS 和 GeoTIFF 支持(需要 GDAL):

uv pip install "splitraster[geo]"

或通过 pip:

pip install "splitraster[geo]"

尝试示例代码 #

示例图像可以在 GitHub 仓库的 tests/data 目录中找到。

from splitraster import io

input_image_path = "./tests/data/raw/RGB.png"
gt_image_path = "./tests/data/raw/GT.png"

save_path = "./tests/data/processed/RGB"
save_path_gt = "./tests/data/processed/GT"

crop_size = 256
repetition_rate = 0.5
overwrite = False

n = io.split_image(input_image_path, save_path, crop_size,
                   repetition_rate=repetition_rate, overwrite=overwrite)
print(f"{n} tiles sample of {input_image_path} are added at {save_path}")


n = io.split_image(gt_image_path, save_path_gt, crop_size,
                   repetition_rate=repetition_rate, overwrite=overwrite)
print(f"{n} tiles sample of {gt_image_path} are added at {save_path_gt}")

可能输出的结果:

Successfully installed splitraster-0.*.*
❯ pytest tests/ -v
Input Image File Shape (H, W, D):(1000, 1000, 3)
crop_size=256, stride=128
Padding Image File Shape (H, W, D):(1024, 1024, 3)
There are 49 files in the ./tests/data/processed/RGB
New image name will start with 50
Generating: 100%|█████████████████████████████████████████████████████████████| 49/49 [00:00<00:00, 50.65img/s]
49 tiles sample of ./tests/data/raw/RGB.png are added at ./tests/data/processed/RGB
Input Image File Shape (H, W, D):(1000, 1000)
crop_size=256, stride=128
Padding Image File Shape (H, W, D):(1024, 1024)
There are 49 files in the ./tests/data/processed/GT
New image name will start with 50
Generating: 100%|████████████████████████████████████████████████████████████| 49/49 [00:00<00:00, 139.72img/s]
49 tiles sample of ./tests/data/raw/GT.png are added at ./tests/data/processed/GT

查看 Notebook 了解详情:标准图像分块

GIS TIFF 图像 #

您还可以处理具有更多波段或通道的遥感 (GeoTIFF) 卫星图像(如多光谱图像)。所有代码都基本相同,只有微小的区别。只需将 io 替换为 geo 模块即可。

此功能还需要您在 Python 环境中安装 gdal 软件包。

由于许多用户可能不需要此功能,因此该软件包未包含在核心依赖中。

不过,如果您需要此功能,请考虑为您的应用创建如下所示的 Conda 环境。

conda create -n split_raster_py310 python=3.10 -y
conda activate split_raster_py310
conda install gdal -y
conda install ipykernel -y
pip install --upgrade pip
pip install splitraster

在 Mac 上,您可以使用 Homebrew 进行安装:

brew install gdal

然后,安装 Python GDAL 软件包:

pip install GDAL

请注意,由于系统依赖关系,安装 GDAL 可能比较复杂。如果遇到问题,您可能需要查阅 GDAL 文档或寻求社区帮助。

示例代码:

from splitraster import geo
input_image_path = "./tests/data/raw/Input.tif"
gt_image_path = "./tests/data/raw/GT.tif"

save_path = "./tests/data/processed/Input"
crop_size = 256
repetition_rate = 0.5
overwrite = False

n = geo.split_image(input_image_path, save_path, crop_size,
                   repetition_rate=repetition_rate, overwrite=overwrite)
print(f"{n} tiles sample of {input_image_path} are added at {save_path}")

查看 Notebook 了解详情:Tutorial_II

随机采样代码 #

基本实现依然与上述相同。只需将 ‘split_image’ 方法替换为 ‘random_crop_image’ 即可。

from splitraster import io
input_image_path = "./tests/data/raw/RGB.png"
gt_image_path = "./tests/data/raw/GT.png"

input_save_path = "./tests/data/processed/Rand/RGB"
gt_save_path = "./tests/data/processed/Rand/GT"

n = io.random_crop_image(input_image_path, input_save_path,  gt_image_path, gt_save_path, crop_size=256, crop_number=20, img_ext='.png', label_ext='.png', overwrite=True)

print(f"{n} sample paris of {input_image_path, gt_image_path} are added at {input_save_path, gt_save_path}.")
from splitraster import geo
input_tif_image_path = "./tests/data/raw/TIF/RGB5k.tif"
gt_tif_image_path = "./tests/data/raw/TIF/GT5k.tif"

input_save_image_path = "./tests/data/processed/Rand/RGB_TIF"
gt_save_image_path = "./tests/data/processed/Rand/GT_TIF"

n = geo.random_crop_image(input_tif_image_path, input_save_image_path,  gt_tif_image_path, gt_save_image_path, crop_size=500, crop_number=20, overwrite=True)

print(f"{n} sample paris of {input_tif_image_path, gt_tif_image_path} are added at {input_save_image_path, gt_save_image_path}.")

贡献指南 #

如果您遇到问题或有任何疑问,请提交 issue提交 pull request

如果您有兴趣为 splitraster 做出贡献,请参阅项目仓库

thumb