使用C++_GDAL剪切大型tif图
全视野图像WSI都比较大,通常为10w×10w左右。为了能用他来训练算法需要将其剪切为瓦片(patch)。GDAL是google处理遥感卫星地图的一个工具,我用来试试剪切WSI。后来找到了python的openslide剪切WSI的方法,效果没GDAL好,或许是我的打开方式不对。
效果:
目的:将11w×8.5w像素、1.5G的tif图切成512*512大小,切出来应该有3.8w左右个patch。
C++_GDAL:
把WSI(patch文件夹与WSI在同一目录)放在不同盘处理的速度不同,经过尝试得到数据(初始patch生成速度和最后patch生成速度数值上等于patch文件夹中按patch修改日期排序的第二分钟的文件数量和倒数第二分钟的文件数量):
WSI及patch位置 | 初始patch生成速度 | 最后patch生成速度 | 全程用时 |
---|---|---|---|
C盘 | 3552张/分钟 | 3919张/分钟 | 12分钟 |
D盘 | 3378张/分钟 | 2213张/分钟 | 17分钟 |
移动硬盘 | 2086张/分钟 | 505张/分钟 | 47分钟 |
切出来有19.8G,
上代码:
头文件:
GetMask.h
#pragma once
#include"gdal_priv.h"
#include <vector>
#include <string>
#include <iostream>
#include <io.h>
#include <direct.h>
using namespace std;
void GetMask(const char* src, int N); //得到图片src的N×N处理后的mask
void SplitSrc(const string& s, vector<string>& v, const string& c); //s为绝对路径,v为拆分后依次放置所有文件夹和文件名的向量,c为拆分依据
void GetPatch(const char* src, int imgWidth, int imgHeight, int x, int y, int N, vector<string>& v); //从src图片x,y处开始切割得到N×N大小的patch
源文件:
main.cpp
#include"GetMask.h"
int main() {
const char* src = "H:\\52001488-3\\qiepian\\52001488-3.tif"; //切片路径
if<