影像的滑动窗口裁切与拼接(附代码)
遥感影像通常尺寸较大,需要裁切成小影像块进行使用;再将处理后的小影像块拼接为原始尺寸。
这里使用python进行影像的裁剪与拼接。(单通道影像,多通道修改即可)
影像的自动裁切
按照窗口进行滑动裁切,往往最后一行或一列裁切不到,这里考虑最后无法完整裁切的情况,最后一行或一列的裁切不满足窗口的大小则向上或向左取像素行或列。
如图,待裁剪图像尺寸56,裁剪窗口22,步长为0;
行可以完全裁切,列剩最后一列不能完全裁切,此时裁切窗口如绿色框所示。
'''
读取遥感影像可以使用gdal,也可以使用cv。
'''
# 读取tif数据集 方法1
def readTif(fileName):dataset = gdal.Open(fileName)if dataset == None:print(fileName + "文件无法打开")return dataset
# 读取tif数据集 方法2
def get_data(path_name):img_size = 24img_names = os.listdir(path_name)img_names.sort(key=lambda x: int(x[:-4]))T1 = []num = len(img_names)for i in range(num):img_name = img_names[i]img = cv.imread(path_name+img_name, cv.IMREAD_UNCHANGED)img = img.reshape([img_size, img_size,1])T1.append(img)T1 = np.array(T1)return T1
'''
滑动窗口裁剪函数
TifPath 影像路径
SavePath 裁剪后保存目录
CropSize 裁剪尺寸
RepetitionSize 步长
'''
def TifCrop(TifPath, SavePath, CropSize, RepetitionSize):dataset_img = readTif(TifPath)width = dataset_img.RasterXSizeheight = dataset_img.RasterYSizeproj = dataset_img.GetProjection()geotrans = dataset_img.GetGeoTransform()img = dataset_img.ReadAsArray(0, 0, width, height) # 获取数据,ndarray# 获取当前文件夹的文件个数len,并以len+1命名即将裁剪得到的图像new_name = len(os.listdir(SavePath))for i in range(int((height - RepetitionSize) / (CropSize - RepetitionSize))):for j in range(int((width - RepetitionSize) / (CropSize - RepetitionSize))):# 如果图像是单波段if (len(img.shape) == 2):cropped = img[int(i * (CropSize - RepetitionSize)): int(i * (CropSize - RepetitionSize)) + CropSize,int(j * (CropSize - RepetitionSize)): int(j * (CropSize - RepetitionSize)) + CropSize]# 如果图像是多波段else:cropped = img[:,int(i * (CropSize - RepetitionSize)): int(i * (CropSize - RepetitionSize)) + CropSize,int(j * (CropSize - RepetitionSize)): int(j * (CropSize - RepetitionSize)) + CropSize]# 写图像writeTiff(cropped, geotrans, proj, SavePath + "/%d.tif" % new_name)# 文件名 + 1new_name = new_name + 1# 裁剪最后一列for i in range(int((height - RepetitionSize) / (CropSize - RepetitionSize))):if (len(img.shape) == 2):cropped = img[int(i * (CropSize - RepetitionSize)): int(i * (CropSize - RepetitionSize)) + CropSize,(width - CropSize): width]else:cropped = img[:,int(i * (CropSize - RepetitionSize)): int(i * (CropSize - RepetitionSize)) + CropSize,(width - CropSize): width]# 写图像writeTiff(cropped, geotrans, proj, SavePath + "/%d.tif" % new_name)new_name = new_name + 1# 裁剪最后一行for j in range(int((width - RepetitionSize) / (CropSize - RepetitionSize))):if (len(img.shape) == 2):cropped = img[(height - CropSize): height,int(j * (CropSize - RepetitionSize)): int(j * (CropSize - RepetitionSize)) + CropSize]else:cropped = img[:,(height - CropSize): height,int(j * (CropSize - RepetitionSize)): int(j * (CropSize - RepetitionSize)) + CropSize]writeTiff(cropped, geotrans, proj, SavePath + "/%d.tif" % new_name)# 文件名 + 1new_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)
影像拼接,过程与裁剪相反。这里给出单波段的代码
def TifMosaic(TifPath, CropSize, RepetitionSize):height = 143width = 248result = np.zeros((height,width),float)imgs = get_data(TifPath)imgs = np.squeeze(imgs)n = 0for i in range(int((height - RepetitionSize) / (CropSize - RepetitionSize))):for j in range(int((width - RepetitionSize) / (CropSize - RepetitionSize))):result[int(i * (CropSize - RepetitionSize)): int(i * (CropSize - RepetitionSize)) + CropSize,int(j * (CropSize - RepetitionSize)): int(j * (CropSize - RepetitionSize)) + CropSize] = imgs[n]n = n+1# 拼接最后一列for i in range(int((height - RepetitionSize) / (CropSize - RepetitionSize))):result[int(i * (CropSize - RepetitionSize)): int(i * (CropSize - RepetitionSize)) + CropSize,(width - CropSize): width] = imgs[n]n = n+1# 拼接最后一行for j in range(int((width - RepetitionSize) / (CropSize - RepetitionSize))):result[(height - CropSize): height,int(j * (CropSize - RepetitionSize)): int(j * (CropSize - RepetitionSize)) + CropSize] = imgs[n]n = n+1# 拼接最后一个小块result[(height - CropSize): height,(width - CropSize): width] = imgs[n]return result
影像的滑动窗口裁切与拼接(附代码)相关推荐
- DataScience:数据处理技术之针对时间序列数据衍变—构造时间滑动窗口数据的简介、代码实现、案例应用之详细攻略
DataScience:数据处理技术之针对时间序列数据衍变-构造时间滑动窗口数据的简介.代码实现.案例应用之详细攻略 目录 时间滑动窗口数据的简介
- GEE(Google earth engine)中的Landsat影像的选择和去云(附代码)
1.获取校正过的Landsat 影像 在这里可以看到GEE提供的全部Landsat数据:Landsat Collections in Earth Engine | Earth Engine Dat ...
- QT不规则窗口的移动原理和证明/QT窗口背景透明(附代码实现)
有些情况下,我们需要实现不规则窗口,比如在软件的加载窗口,可以使用背景透明的图片填充窗口,从而创建炫酷的不规则窗口.下面介绍使用背景透明图片设计不规则窗口以及实现鼠标拖拽不规则窗口的方法. 首先,我们 ...
- 七十四、滑动窗口最值问题
@Author:Runsen 编程的本质来源于算法,而算法的本质来源于数学,编程只不过将数学题进行代码化. ---- Runsen 滑动问题包含一个滑动窗口,它是一个运行在一个大数组上的子列表,该数组 ...
- 【LeetCode笔记】剑指 Offer 57- II. 和为 s 的连续正数序列(Java、滑动窗口、二刷)
文章目录 题目描述 思路 & 代码 二刷 题目描述 花了不少时间来优化= =,很好的一道用来理解滑动窗口的题- 思路 & 代码 要点:为了 O(n) 复杂度,左右边界都只能往右走 滑动 ...
- 嗯,查询滑动窗口最大值的这4种方法不错....
作者 | 王磊 来源 | Java中文社群(ID:javacn666) 转载请联系授权(微信ID:GG_Stone) 本文已收录至 Github<小白学算法>系列:https://gith ...
- 你还在为查询滑动窗口最大值发愁吗?点开看最高效率解法!
作者 | 王磊 来源 | Java中文社群(ID:javacn666) 头图 | CSDN 下载自东方IC 本文已收录至 Github<小白学算法>系列:https://github.c ...
- 滑动窗口切割图片并重定位标注框
在进行目标检测时候,尤其是小目标检测,将图片放大后再进行检测是一个常用的手段,但是方法的后的图像在输入网络的时候,有会被resize带一定得尺寸,这样的我们的放大就没有起到作用.所以有时候我们需要将图 ...
- c语言 滑窗法_滑动窗口算法(一)
某日事不多,点开sentinel-core代码学习,想看看qps.rt等是怎么统计的. 点开StatisticSlot类,发现里面是用DefaultNode增加qps,然后尝试点开 DefaultNo ...
最新文章
- Python 标准库 —— zipfile(读取 zip 文件)
- linux目录挂载到内存,Linux中内存挂载到目录下
- SpringBoot接口幂等性实现的4种方案!
- pthread属性使用(转)
- 科普:算法岗是什么?我适不适合算法岗?选什么方向的算法岗?
- Saas与传统软件对比
- 2021 CSP-S 游记
- php调用airtestide,raw device
- Linux : 文件处理命令
- java的写法作文,RxJava系列文章(二) - 网络图片添加水印RxJava写法
- solr 5.0.0 bin/start脚本详细解析
- Oracle之外键(Foreign Key)使用方法具体解释(二)- 级联删除(DELETE CASCADE)
- MySQL语法一:数据定义语句
- Android 腾讯地图定位
- 卡特兰数 Catalan number
- PMP之采购管理过程组中的合同类型辨析
- 8421 5421 2421 余3码
- 数字藏品的价值是什么?
- 文件夹的隐藏选项为灰色勾选,无法更改或删除
- 计算机专业的学习方法
热门文章
- LinuxC TCP实现简易聊天室
- Vue 富文本wangEditor3 (自动保存 快捷保存 激活工具栏...
- nginx 如何查看访问ip和对应访问路径
- 考试科目C语言缩写,全国计算机专业统考 考试科目都有什么
- android的版本电视怎么投屏,三星电视怎么投屏 三星电视投屏设置方法
- 苹果ppt_熬夜看完苹果发布会,我居然学会了用PPT做霓虹灯特效?!
- js点击网页背景特效和js打字状态特效代码
- 梅赛德斯-奔驰获得世界上第一个有条件自动驾驶的国际有效系统批准
- 【小程序专栏】个人及企业资质该如何注册小程序?
- Vue【有与无】【F3】【问题】vue 2.x 动态路由刷新后空白