利用python,把SAR遥感影像裁剪分割多张小影像,tif分割png格式,分割后没有定位信息。
import numpy as np
from osgeo import gdal
from osgeo import gdal_array
import cv2
import os
outPath = r'D:\Study\SeaIce\sea_ice_data\Data\UNet\11_2048' # 输出文件夹
if not os.path.exists(outPath):
os.makedirs(outPath)
filename = r'D:\Study\SeaIce\sea_ice_data\Data\UNet\11.tif' # 待裁剪文件
dataset = gdal.Open(filename)
size=int(2048)
datatype = dataset.GetRasterBand(1).DataType # 文件类型
rows = dataset.RasterYSize # 行
columns = dataset.RasterXSize # 列
bands = dataset.RasterCount # 波段数
image = np.zeros((rows, columns, bands),
dtype = gdal_array.GDALTypeCodeToNumericTypeCode(datatype))
for b in range(bands):
band = dataset.GetRasterBand(b+1)
image[:, :, b] = band.ReadAsArray()
print("裁剪图片行:"+str(rows), "裁剪图片列"+ str(columns))
image_cut = image[:rows//size*size, :columns//size*size, :] # 裁剪,取256的整数倍
for i in range(rows//size):
for j in range(columns//size):
image_save = image_cut[i * size:(i + 1) * size, j * size:(j + 1) * size, :]
filename = str(i) + str(j) + '.png'
path = os.path.join(outPath, filename)
cv2.imwrite(path, image_save)
分割后效果: