遥感语义分割数据集中的切图策略

简介: 该脚本用于遥感图像的切图处理,支持大尺寸图像按指定大小和步长切割为多个小图,适用于语义分割任务的数据预处理。通过设置剪裁尺寸(cs)和步长(ss),可灵活调整输出图像的数量和大小。此外,脚本还支持标签图像的转换,便于后续模型训练使用。

遥感语义分割数据集中的切图策略

# 切图脚本
import argparse
import glob
import math
import os
import os.path as osp
import tempfile
import zipfile

import mmcv
import numpy as np
from mmengine.utils import ProgressBar, mkdir_or_exist

def clip_big_image(image_path, clip_save_dir, to_label=False):
    # Original image of Vaihingen dataset is very large, thus pre-processing
    # of them is adopted. Given fixed clip size and stride size to generate
    # clipped image, the intersection of width and height is determined.
    # For example, given one 5120 x 5120 original image, the clip size is
    # 512 and stride size is 256, thus it would generate 20x20 = 400 images
    # whose size are all 512x512.
    image = mmcv.imread(image_path)

    h, w, c = image.shape
    cs = 512 # todo 分割的大小
    ss = 256 # todo 分割的步长

    num_rows = math.ceil((h - cs) / ss) if math.ceil(
        (h - cs) / ss) * ss + cs >= h else math.ceil((h - cs) / ss) + 1
    num_cols = math.ceil((w - cs) / ss) if math.ceil(
        (w - cs) / ss) * ss + cs >= w else math.ceil((w - cs) / ss) + 1

    x, y = np.meshgrid(np.arange(num_cols + 1), np.arange(num_rows + 1))
    xmin = x * cs
    ymin = y * cs

    xmin = xmin.ravel()
    ymin = ymin.ravel()
    xmin_offset = np.where(xmin + cs > w, w - xmin - cs, np.zeros_like(xmin))
    ymin_offset = np.where(ymin + cs > h, h - ymin - cs, np.zeros_like(ymin))
    boxes = np.stack([
        xmin + xmin_offset, ymin + ymin_offset,
        np.minimum(xmin + cs, w),
        np.minimum(ymin + cs, h)
    ],
                     axis=1)

    # to_label是用来转化label使用的
    if to_label:
        color_map = np.array([[0, 0, 0], [85, 85, 85]])
        flatten_v = np.matmul(
            image.reshape(-1, c),
            np.array([2, 3, 4]).reshape(3, 1))
        out = np.zeros_like(flatten_v)
        for idx, class_color in enumerate(color_map):
            value_idx = np.matmul(class_color,
                                  np.array([2, 3, 4]).reshape(3, 1))
            out[flatten_v == value_idx] = idx
        image = out.reshape(h, w)

    for box in boxes:
        start_x, start_y, end_x, end_y = box
        clipped_image = image[start_y:end_y,
                              start_x:end_x] if to_label else image[
                                  start_y:end_y, start_x:end_x, :]
        # area_idx = osp.basename(image_path).split('_')[3].strip('.tif')
        # area_idx = osp.basename(image_path).strip('.')
        # print(area_idx)
        # area_idx = osp.basename(image_path).strip('.png')
        mmcv.imwrite(
            clipped_image.astype(np.uint8),
            osp.join(clip_save_dir,
                     # f'{area_idx}_{start_x}_{start_y}_{end_x}_{end_y}.png'))
                     f'img1_{start_x}_{start_y}_{end_x}_{end_y}.png'))

if __name__ == '__main__':
    # 切图搞定,还需要标签转换,和数据集分离
    clip_big_image(image_path="E:/EEEE-COM/toUser/toUser/train/img1/img1.tif", clip_save_dir="E:/EEEE-COM/toUser/toUser/train/split_data/training_images")
    # clip_big_image(image_path="E:/EEEE-COM/toUser/toUser/train/img2/img2.tif", clip_save_dir="E:/EEEE-COM/toUser/toUser/train/split_data/training_images")
    # clip_big_image(image_path="E:/EEEE-COM/toUser/toUser/train/train_labels_png/img1.png", clip_save_dir="E:/EEEE-COM/toUser/toUser/train/split_data/training_labels")
    # clip_big_image(image_path="E:/EEEE-COM/toUser/toUser/train/img2/img2.tif", clip_save_dir="E:/EEEE-COM/toUser/toUser/train/split_data/training_images")
目录
相关文章
|
3月前
|
人工智能 JavaScript API
保姆级指南:OpenClaw阿里云及本地部署最佳实践:抓取行业调研资料+三维提效法,7天吃透陌生赛道
行业调研的核心痛点从不是“缺资料”,而是“资料过载与认知碎片化”——麦肯锡研报、行业媒体分析、竞品动态、政策文件等信息杂乱无章,不同来源的数据矛盾、统计口径不一,手动梳理往往耗时数月,却难形成系统认知。
1683 5
|
Windows Python
Windows下pip设置国内源阿里云镜像加速
Windows下pip设置国内源阿里云镜像加速
5752 0
Windows下pip设置国内源阿里云镜像加速
|
8月前
|
物联网 数据挖掘 BI
RFID重塑学校资产管理新格局
RFID技术革新学校资产管理,通过快速盘点、流程简化、数据精准、实时追踪等功能,实现资产全生命周期管理,提升效率与透明度,助力智慧校园建设。
|
人工智能 数据可视化 API
语义分割笔记(一):基于PaddleSeg使用Transfomer模型对航空遥感图像分割
这篇博客介绍了如何使用PaddleSeg和Transformer模型SegFormer B3对航空遥感图像进行语义分割,包括项目背景、数据集处理、训练步骤和代码实现。
1534 1
语义分割笔记(一):基于PaddleSeg使用Transfomer模型对航空遥感图像分割
|
11月前
|
存储 设计模式 Java
Java 期末考试不挂科必背基础知识点复习笔记整理
这是一份全面的Java基础知识点复习笔记,涵盖核心特性、数据类型、流程控制、数组、异常处理、JVM原理、多线程、设计模式及Java 8+新特性等内容。结合买飞机票、验证码生成和评委打分等应用实例,助你掌握考试重点,轻松应对Java期末考试,避免挂科!附带代码资源,供深入学习使用。链接:[https://pan.quark.cn/s/14fcf913bae6](https://pan.quark.cn/s/14fcf913bae6)
543 0
|
10月前
|
数据可视化 开发者
中小型研发团队一样适用:轻量级研发流程整合工具使用策略解析
研发流程整合工具通过统一任务、文档、代码等流程节点,提升团队协作效率,解决沟通碎片化、任务模糊等问题,实现可视化管理与自动化协作,助力团队从“人力推动”转向“系统驱动”。
|
机器学习/深度学习 数据采集 算法
机器学习在医疗诊断中的前沿应用,包括神经网络、决策树和支持向量机等方法,及其在医学影像、疾病预测和基因数据分析中的具体应用
医疗诊断是医学的核心,其准确性和效率至关重要。本文探讨了机器学习在医疗诊断中的前沿应用,包括神经网络、决策树和支持向量机等方法,及其在医学影像、疾病预测和基因数据分析中的具体应用。文章还讨论了Python在构建机器学习模型中的作用,面临的挑战及应对策略,并展望了未来的发展趋势。
1146 1
|
机器学习/深度学习 自然语言处理 算法
【NPL自然语言处理】带你迅速了解传统RNN模型
【NPL自然语言处理】带你迅速了解传统RNN模型
|
运维 监控 网络协议
在Linux中,有哪些系统管理和配置工具?
在Linux中,有哪些系统管理和配置工具?
|
定位技术
ArcGIS中ArcMap栅格图层0值设置为NoData值的简便方法
ArcGIS中ArcMap栅格图层0值设置为NoData值的简便方法
1121 1

热门文章

最新文章