遥感影像样本制作----边缘填充裁剪,拼接

import cv2
from patchify import patchify, unpatchify
import argparse
import os
import numpy as np


def slide_patch(opt):
    large_image = cv2.imread(opt.large_image_dir)
    padding_h = (((large_image.shape[0] - opt.patch_size[0]) // opt.step + 1) * opt.step + opt.patch_size[0]) - \
                large_image.shape[0]
    padding_w = (((large_image.shape[1] - opt.patch_size[1]) // opt.step + 1) * opt.step + opt.patch_size[1]) - \
                large_image.shape[1]
    large_image_padding = cv2.copyMakeBorder(large_image, 0, padding_h, 0, padding_w, cv2.BORDER_CONSTANT,
                                             value=(0, 0, 0))
    patches = patchify(large_image_padding, opt.patch_size, opt.step)
    index = 0

    for i in range(patches.shape[0]):
        for j in range(patches.shape[1]):
            single_patch = patches[i, j, 0, :, :, :]
            cv2.imwrite(opt.patches_dir + str(index) + '.jpg', single_patch)
            index += 1


def merge_patch(opt):
    large_image = cv2.imread(opt.large_image_dir)
    padding_h = (((large_image.shape[0] - opt.patch_size[0]) // opt.step + 1) * opt.step + opt.patch_size[0]) - \
                large_image.shape[0]
    padding_w = (((large_image.shape[1] - opt.patch_size[1]) // opt.step + 1) * opt.step + opt.patch_size[1]) - \
                large_image.shape[1]
    large_image_padding = cv2.copyMakeBorder(large_image, 0, padding_h, 0, padding_w, cv2.BORDER_CONSTANT,
                                             value=(0, 0, 0))
    patches = os.listdir(opt.patches_dir)
    patches.sort(key=lambda x: int(x[:x.find('.')]))
    predicted_patches = []
    index = 0
    for i in range((large_image_padding.shape[0] - opt.patch_size[0]) // opt.step + 1):
        for j in range((large_image_padding.shape[1] - opt.patch_size[1]) // opt.step + 1):
            predicted_patch = np.expand_dims(
                np.expand_dims(np.expand_dims(cv2.imread(opt.patches_dir + patches[index]), axis=0), axis=0), axis=0)
            predicted_patches.append(predicted_patch)
            index += 1
    predicted_patches = np.array(predicted_patches)
    predicted_patches_reshaped = np.reshape(predicted_patches, (
    (large_image_padding.shape[0] - opt.patch_size[0]) // opt.step + 1,
    (large_image_padding.shape[1] - opt.patch_size[1]) // opt.step + 1, 1, opt.patch_size[0], opt.patch_size[1],
    opt.patch_size[2]))
    reconstructed_image = unpatchify(predicted_patches_reshaped, (
    ((large_image_padding.shape[0] - opt.patch_size[0]) // opt.step) * opt.step + opt.patch_size[0],
    ((large_image_padding.shape[1] - opt.patch_size[1]) // opt.step) * opt.step + opt.patch_size[1],
    large_image_padding.shape[2]))
    reconstructed_image_clip = reconstructed_image[:large_image.shape[0], :large_image.shape[1]]
    cv2.imwrite(opt.reconstructed_image_dir, reconstructed_image_clip)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--task', type=int, default=1, help='0 or 1 mean slide or merge')
    parser.add_argument('--large_image_dir', type=str, default='I:/python/imagepadding/core.png',
                        help='large_image path')
    parser.add_argument('--patches_dir', type=str, default='I:/python/deeplabv3plus_results_bigphototrained/', help='save or load patches dir')
    parser.add_argument('--patch_size', type=int, default=(512, 512, 3), help='patch size (height, width, channel)')
    parser.add_argument('--step', type=int, default=512,
                        help='patch slide step, step=224 for 224 patch means no overlap')
    parser.add_argument('--reconstructed_image_dir', type=str, default='I:/python/pinjie_resultmerge.png',
                        help='reconstructed_image save path')
    opt = parser.parse_args()
#slide_patch(opt)
merge_patch(opt)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值