matlab切割超大tiff

目标:切割超大不能读入内存的tiff影像

最近遇到了一个困难:在处理遥感影像的时候,遇到了超大tiff(大概一张36个G),根本无法读进内存,后来编写了matlab程序,调用了blockproc函数解决了该问题

下面是代码,分享给大家:

% Created by Weizhen Fang
% Version 1.0
% 20190518
% 这个程序是用来切割不能读入内存的超大tiff
% filepath:超大tiff路径
% OutputPath:输出路径
% outputname:输出名称
% cutblocksize:切割的每张图大小

clc;
clear;

%input
filepath='.\Band_20_Beijing_006_004_ID_0.tif';  %要读取的文档所在的路径
OutputPath ='.\out\test\';
outputname='haidian';
cutblocksize=[100 100];

info = geotiffinfo(filepath);

fun = @(block_struct)yourAlgorithm(block_struct,OutputPath,outputname,info);
blockproc(filepath,cutblocksize, fun);

function data=yourAlgorithm(blockStruct,OutputPath,outputname,info)
   %do your processing of the block here and 
   data = blockStruct.data;
   location= blockStruct.location;
   blockSize=blockStruct.blockSize;
  
   SpatialRef_new=info.SpatialRef;
    LatitudeLimits=SpatialRef_new.LatitudeLimits;
    LongitudeLimits=SpatialRef_new.LongitudeLimits;
    CellExtentInLatitude=SpatialRef_new.CellExtentInLatitude;
    CellExtentInLongitude=SpatialRef_new.CellExtentInLongitude;
   
   row=location(1,1); %行
   col=location(1,2); %列
   
   %被裁剪的局部的范围(左上,右下点坐标:行,列)。%Matlab图上x对应列,y对应行
   %转化为矩阵 a(1,1):右y / a(1,2):左y / a(1,3):左x / a(1,4):右x
   a(1,1)=row+blockSize(1,1);
   a(1,2)=row;
   a(1,3)=col+blockSize(1,2);
   a(1,4)=col;
   file_path=[OutputPath,outputname,'_',num2str(row),'_',num2str(col),  '.tif'];
   
    img_i_size=blockSize;%求图像大小
    SpatialRef_new.RasterSize=img_i_size;%以下参数为非只读参数,即可编辑的数据
    SpatialRef_new.LatitudeLimits(1)=LatitudeLimits(2)-(a(1,1)-1).*CellExtentInLatitude;
    SpatialRef_new.LatitudeLimits(2)=LatitudeLimits(2)-(a(1,2)-1).*CellExtentInLatitude;
    SpatialRef_new.LongitudeLimits(2)=LongitudeLimits(1)+(a(1,3)-1).*CellExtentInLongitude;
    SpatialRef_new.LongitudeLimits(1)=LongitudeLimits(1)+(a(1,4)-1).*CellExtentInLongitude;

    geotiffwrite(file_path,data,SpatialRef_new,...
        'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag,'TiffType','bigtiff');

end

小结

blockproc的可拓展性非常强,速度也很快,推荐使用

你可能感兴趣的:(matlab,遥感图像处理)