Python remote sensing image SAR segmentation and cropping multiple small images

Use python to crop and segment SAR remote sensing images into multiple small images, and split tif into png format. After segmentation, there is no positioning information.

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)

The effect after splitting:

Guess you like

Origin blog.csdn.net/weixin_52127098/article/details/124815706