gdal ライブラリを使用して tif イメージを読み取り、エッジを塗りつぶし、ウィンドウ サイズに応じてスライドしてトリミングします (gdal から PIL への変換を含む)

関連記事

PILとOPENCVの変換関係_pil cvtcolor(image)_Tomatoはスクランブルエッグについてのブログ - CSDNブログ

Python GDAL および PIL イメージ変換_gdal.readasarray および pil_llc フットプリント ブログ - CSDN ブログ

1.オリジナルデータ

2. データを個別に読み取る

1. gdal で読み取られる配列 

2. pil で読み取られる配列

 

3. gdalをpilに変換します

image= np.rollaxis(image , 0, 3)

 変換結果

合計コード 

gdal ライブラリを使用して tif 画像を読み込んでエッジを塗りつぶし、ウィンドウ サイズに応じてスライドしてトリミングします。


def clip_picture(file_path,a):
    slide_window = 1024  # 大的滑动窗口
    step_length = 1024
    sat_list = os.listdir(file_path) 

    for file in sat_list:
        Image_Path = os.path.join(file_path,file)
        image=gdal.Open(Image_Path)
        width = image.RasterXSize
        height = image.RasterYSize

        # image = Image.open(Image_Path)

        # width = image.size[0]  # 获取图像的宽
        # height = image.size[1]  # 获取图像的高

        right_fill = step_length - (width % step_length)
        bottom_fill = step_length - (height % step_length)

        width_path_number = int((width + right_fill) / step_length)  # 横向切成的小图的数量
        height_path_number = int((height + bottom_fill) / step_length)  # 纵向切成的小图的数量

        #print(width_path_number, height_path_number)
        # image = np.array(image)
        image=image.ReadAsArray()
        
        if a=='tif':
            image= np.rollaxis(image , 0, 3)
        


        image = cv2.copyMakeBorder(image, top=0, bottom=bottom_fill, left=0, right=right_fill,
                                borderType=cv2.BORDER_CONSTANT, value=0)

        image = cv2.copyMakeBorder(image, top=step_length // 2, bottom=step_length // 2, left=step_length // 2,
                                right=step_length // 2,
                                borderType=cv2.BORDER_CONSTANT, value=0)  # 填充1/2步长的外边框
    

        # 2.将膨胀后的大图按照滑窗裁剪
        tar = './dataset/train/'
       
        target=tar

        image_crop_addr = target  # 图像裁剪后存储的文件夹
        # image = Image.fromarray(image)  # 将图片格式从numpy转回PIL
        image=Image.fromarray(np.uint8(image))
        l = 0
        if a=='tif':
            for j in range(height_path_number):
                for i in range(width_path_number):
                    box = (i * step_length, j * step_length, i * step_length + slide_window, j * step_length + slide_window)
                    small_image = image.crop(box)
                    small_image.save(
                        image_crop_addr + file[:-4] + '({},{})@{:04d}_sat.tif'.format(j, i, l), quality=95)
                    l = l + 1
        
        if a=='png':
            for j in range(height_path_number):
                for i in range(width_path_number):
                    box = (i * step_length, j * step_length, i * step_length + slide_window, j * step_length + slide_window)
                    small_image = image.crop(box)
                    small_image.save(
                        image_crop_addr + file[:-4] + '({},{})@{:04d}_mask.png'.format(j, i, l), quality=95)
                    l = l + 1

おすすめ

転載: blog.csdn.net/weixin_61235989/article/details/132354827