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

简介: 该脚本用于遥感图像的切图处理,支持大尺寸图像按指定大小和步长切割为多个小图,适用于语义分割任务的数据预处理。通过设置剪裁尺寸(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")
目录
相关文章
|
人工智能 数据可视化 API
语义分割笔记(一):基于PaddleSeg使用Transfomer模型对航空遥感图像分割
这篇博客介绍了如何使用PaddleSeg和Transformer模型SegFormer B3对航空遥感图像进行语义分割,包括项目背景、数据集处理、训练步骤和代码实现。
1614 1
语义分割笔记(一):基于PaddleSeg使用Transfomer模型对航空遥感图像分割
|
定位技术
ArcGIS中ArcMap栅格图层0值设置为NoData值的简便方法
ArcGIS中ArcMap栅格图层0值设置为NoData值的简便方法
1179 1
|
Windows Python
Windows下pip设置国内源阿里云镜像加速
Windows下pip设置国内源阿里云镜像加速
5815 0
Windows下pip设置国内源阿里云镜像加速
|
机器学习/深度学习 自然语言处理 算法
【NPL自然语言处理】带你迅速了解传统RNN模型
【NPL自然语言处理】带你迅速了解传统RNN模型
|
存储 小程序 安全
微信小程序的红包功能是如何实现的?
注意:微信小程序红包功能一定记得用企业付款到钱包功能,别用微信的现金红包接口,否则你就有踩不完的坑。
1333 0
|
JavaScript 程序员 iOS开发
|
存储 设计模式 Java
Java 期末考试不挂科必背基础知识点复习笔记整理
这是一份全面的Java基础知识点复习笔记,涵盖核心特性、数据类型、流程控制、数组、异常处理、JVM原理、多线程、设计模式及Java 8+新特性等内容。结合买飞机票、验证码生成和评委打分等应用实例,助你掌握考试重点,轻松应对Java期末考试,避免挂科!附带代码资源,供深入学习使用。链接:[https://pan.quark.cn/s/14fcf913bae6](https://pan.quark.cn/s/14fcf913bae6)
653 0
|
机器学习/深度学习 人工智能 自然语言处理
深度学习基础入门篇-序列模型[11]:循环神经网络 RNN、长短时记忆网络LSTM、门控循环单元GRU原理和应用详解
深度学习基础入门篇-序列模型[11]:循环神经网络 RNN、长短时记忆网络LSTM、门控循环单元GRU原理和应用详解
深度学习基础入门篇-序列模型[11]:循环神经网络 RNN、长短时记忆网络LSTM、门控循环单元GRU原理和应用详解
|
存储
汉字和数字站几个字节,估算内存占用情况
该文内容讲述了字符和字节的关系:中文标点占3字节,英文字母或数字占1字节,英文标点也占1字节。1字节等于8字位,1字位是1个二进制数。此外,还介绍了存储单位的换算:1B=8b,1KB=1024B,1MB=1024KB,1GB=1024MB。其中,b代表字位,B代表字节,KB是千字节,MB是兆字节,GB是吉字节。
1101 2
|
前端开发
微前端-qiankun:vue3-vite 接入 vue3-vite
微前端-qiankun:vue3-vite 接入 vue3-vite
1373 0