matlab 裁剪tif图

时间:2024-03-19 20:34:42

对图像(tif格式)进行裁剪函数:
function newpic = croptif(path,lon1,lat1,lon2,lat2)
% path为图片绝对路径,lon1,lat1为裁剪图片左上角经纬度,lon2,lat1为新图右下角经纬度
[A,R] = geotiffread(path);
a = (lon1-R.LongitudeLimits(1,1))./R.CellExtentInLongitude+1;
b = (R.LatitudeLimits(1,2)-lat1)./R.CellExtentInLatitude+1;
c = (lon2-lon1)./R.CellExtentInLongitude+1;
d = (lat1-lat2)./R.CellExtentInLatitude+1;
newpic = imcrop(A,[a b c d]);
end
函数使用举例:
从图中截取规定经纬度范围内的图像,并显示出来,经纬度范围如下:
左下角经纬度坐标(单位:度):112.928123474, 28.156242371
左上角经纬度坐标(单位:度):112.928123474, 28.157272339
右上角经纬度坐标(单位:度):112.929153442, 28.157272339
右下角经纬度坐标(单位:度):112.929153442, 28.156242371

调用函数:
path = ‘D:\study\data\CSUpark.tif’;
newpic1 = croptif(path,112.928123474,28.157272339,112.929153442,28.156242371);
imshow(newpic1);
函数实现:
matlab 裁剪tif图
裁剪得到:
matlab 裁剪tif图