首页 > 编程语言 >Python|遥感影像语义分割:使用Python(GDAL)制作遥感影像语义分割数据集

Python|遥感影像语义分割:使用Python(GDAL)制作遥感影像语义分割数据集

时间:2024-07-07 11:43:01浏览次数:9  
标签:name img Python 语义 RepetitionRate int 遥感 im CropSize

遥感影像标注

使用ArcGIS Pro标注得到标签图标注对象以供深度学习使用—ArcGIS Pro | 文档,由于我的任务是二分类任务,因此我得到的标签图是一张二值图,如下图所示。

image-20240707112134453

使用python滑动裁剪图像及标签

采用分块裁剪策略,将大区域影像分割成256256像素的块,并采用滑动窗口技术,步长为窗口宽度的一半,块间存在50%的重叠。裁剪自影像左上角开始,按顺序进行,如下图所示。同时标注数据也按此策略同步处理,保证数据集的有效性和样本量的增加。

image-20240707111240437

Python代码

import os
from osgeo import gdal
import numpy as np


#  读取tif数据集
def readTif(fileName):
   dataset = gdal.Open(fileName)
   if dataset is None:
      print(fileName + "文件无法打开")
   return dataset


#  保存tif文件函数
def writeTiff(im_data, im_geotrans, im_proj, path):
   if 'int8' in im_data.dtype.name:
      datatype = gdal.GDT_Byte
   elif 'int16' in im_data.dtype.name:
      datatype = gdal.GDT_UInt16
   else:
      datatype = gdal.GDT_Float32
   if len(im_data.shape) == 3:
      im_bands, im_height, im_width = im_data.shape
   elif len(im_data.shape) == 2:
      im_data = np.array([im_data])
      im_bands, im_height, im_width = im_data.shape
   # 创建文件
   driver = gdal.GetDriverByName("GTiff")
   dataset = driver.Create(path, int(im_width), int(im_height), int(im_bands), datatype)
   if dataset is not None:
      dataset.SetGeoTransform(im_geotrans)  # 写入仿射变换参数
      dataset.SetProjection(im_proj)  # 写入投影
   for i in range(im_bands):
      dataset.GetRasterBand(i + 1).WriteArray(im_data[i])
   del dataset


def TifCrop(TifPath, SavePath, CropSize, RepetitionRate):
   """
    滑动窗口裁剪函数
    TifPath 影像路径
    SavePath 裁剪后保存目录
    CropSize 裁剪尺寸
    RepetitionRate 重复率
    """
   dataset_img = readTif(TifPath)
   width = dataset_img.RasterXSize
   height = dataset_img.RasterYSize
   proj = dataset_img.GetProjection()
   geotrans = dataset_img.GetGeoTransform()
   img = dataset_img.ReadAsArray(0, 0, width, height)  # 获取数据

   #  获取当前文件夹的文件个数len,并以len+1命名即将裁剪得到的图像
   new_name = len(os.listdir(SavePath))

   #  裁剪图片,重复率为RepetitionRate
   for i in range(int((height - CropSize * RepetitionRate) / (CropSize * (1 - RepetitionRate)))):
      for j in range(int((width - CropSize * RepetitionRate) / (CropSize * (1 - RepetitionRate)))):
         #  如果图像是单波段
         if len(img.shape) == 2:
            cropped = img[
                      int(i * CropSize * (1 - RepetitionRate)): int(i * CropSize * (1 - RepetitionRate)) + CropSize,
                      int(j * CropSize * (1 - RepetitionRate)): int(j * CropSize * (1 - RepetitionRate)) + CropSize]
         #  如果图像是多波段
         else:
            cropped = img[:,
                      int(i * CropSize * (1 - RepetitionRate)): int(i * CropSize * (1 - RepetitionRate)) + CropSize,
                      int(j * CropSize * (1 - RepetitionRate)): int(j * CropSize * (1 - RepetitionRate)) + CropSize]
         #  写图像
         writeTiff(cropped, geotrans, proj, SavePath + "/%d.tif" % new_name)
         #  文件名 + 1
         new_name = new_name + 1

   #  向前裁剪最后一列
   for i in range(int((height - CropSize * RepetitionRate) / (CropSize * (1 - RepetitionRate)))):
      if len(img.shape) == 2:
         cropped = img[int(i * CropSize * (1 - RepetitionRate)): int(i * CropSize * (1 - RepetitionRate)) + CropSize,
                   (width - CropSize): width]
      else:
         cropped = img[:,
                   int(i * CropSize * (1 - RepetitionRate)): int(i * CropSize * (1 - RepetitionRate)) + CropSize,
                   (width - CropSize): width]
      #  写图像
      writeTiff(cropped, geotrans, proj, SavePath + "/%d.tif" % new_name)
      new_name = new_name + 1

   #  向前裁剪最后一行
   for j in range(int((width - CropSize * RepetitionRate) / (CropSize * (1 - RepetitionRate)))):
      if len(img.shape) == 2:
         cropped = img[(height - CropSize): height,
                   int(j * CropSize * (1 - RepetitionRate)): int(j * CropSize * (1 - RepetitionRate)) + CropSize]
      else:
         cropped = img[:, (height - CropSize): height,
                   int(j * CropSize * (1 - RepetitionRate)): int(j * CropSize * (1 - RepetitionRate)) + CropSize]
      writeTiff(cropped, geotrans, proj, SavePath + "/%d.tif" % new_name)
      #  文件名 + 1
      new_name = new_name + 1

   #  裁剪右下角
   if len(img.shape) == 2:
      cropped = img[(height - CropSize): height, (width - CropSize): width]
   else:
      cropped = img[:, (height - CropSize): height, (width - CropSize): width]
   writeTiff(cropped, geotrans, proj, SavePath + "/%d.tif" % new_name)
   new_name = new_name + 1


# 训练集和验证集都要裁剪
# 裁剪图像特征。拿到影像数据增强中进行数据增强
# 将影像1裁剪为重复率为0.1的256×256的数据集


if __name__ == '__main__':
   TifCrop("D:\Dataset_Authoring\EnShi.tif",
           "D:\Dataset_Authoring\EnShi_data", 512, 0.5)

标签:name,img,Python,语义,RepetitionRate,int,遥感,im,CropSize
From: https://www.cnblogs.com/tangjielin/p/18288301

相关文章

  • Python 模块的制作,发布,安装
    在Python当中,一个Python文件就是一个模块,模块的名字就是Python文件的名字。例如:创建一个test.py文件,那么test.py就是一个模块模块的作用:可以使我们有逻辑的去组织我们的Python代码,以库的形式去封装功能,非常方便的去让调用者去使用模块中的功能可以定义函数,类,变量......
  • Java语言,MySQL数据库;基于springboot的阅读系统 86095(免费领源码)计算机毕业设计项目推
    摘 要从古至今,阅读都是人们学习新技能,新知识的一种方式。随着互联网时代的到来,传统的纸质阅读早已满足不了人们对于阅读的渴望。在这样的大背景下,电子阅读便快速崛起。越来越多的人们喜欢愿意通过这样方式来阅读书籍。基于以上种种,本文设计并实现了书籍阅读系统。目的在于......
  • Java语言,MySQL数据库;基于微信小程序的安全教育平台面向大学生 85871(免费领源码)计算机
    目 录摘要1绪论1.1研究背景1.2研究现状1.3论文结构与章节安排2 基于微信小程序的安全教育平台面向大学生系统分析2.1可行性分析2.2系统流程分析2.2.1数据增加流程2.2.2数据修改流程2.2.3数据删除流程2.3系统功能分析2.3.1功能性分析......
  • 【python】python母婴数据分析模型预测可视化(数据集+论文+PPT+源码)【独一无二】
    ......
  • 计算机毕业设计项目推荐:66945 同城信息网(开题答辩+程序定制+全套文案 )上万套实战教程
    摘要随着社会的发展,社会的方方面面都在利用信息化时代的优势。互联网的优势和普及使得各种系统的开发成为必需。本文以实际运用为开发背景,运用软件工程原理和开发方法,它主要是采SSM技术和mysql数据库来完成对系统的设计。整个开发过程首先对同城信息网进行需求分析,得出......
  • python基础
    Python基础一、前序查看Python版本pyhton-V或Python--version二、基础部分2.1基础语法编码默认情况下,Python3源码文件以UTF-8编码,所有字符串都是unicode字符串。当然你也可以为源码文件指定不同的编码:#-*-coding:cp-1252-*-标......
  • python pyqt5学习记录(三)
    一、布局在PyQt5中,可以使用QHBoxLayout来创建水平布局,使用QVBoxLayout来创建垂直布局。以下是一个简单的例子,展示了如何将两个按钮分别放置在水平和垂直布局中。importsysfromPyQt5.QtWidgetsimportQApplication,QWidget,QPushButton,QVBoxLayout,QHBoxLayoutcla......
  • python-graph-study2024-7
     1.先打基础1基础学习笔记 GraphCreationNetworkXgraphobjectscanbecreatedinoneofthreeways:Graphgenerators—standardalgorithmstocreatenetworktopologies.Importingdatafrompreexisting(usuallyfile)sources.Addingedgesan......
  • python 识别图片验证码/滑块验证码准确率极高的 ddddocr 库
    前言验证码的种类有很多,它是常用的一种反爬手段,包括:图片验证码,滑块验证码,等一些常见的验证码场景。识别验证码的python库有很多,用起来也并不简单,这里推荐一个简单实用的识别验证码的库ddddocr(带带弟弟ocr)库.环境准备python版本要求小于等于python3.9版本pip安装pipin......
  • python简单操作
    一.输出九九乘法表row=1whilerow<=9:  col=1  whilecol<=row:    print("%d*%d=%d"%(row,col,row*col),end="\t")    #\t在控制台输出一个制表符,协助文本垂直方向上对齐    #\n换行符\"可输出双引号 ......