cv 2 tif图片切割 .py

import cv2 as cv
#import matplotlib.pyplot as plt
import os


def crop_image(image_dir, output_path, size,train_tile_padding,useoverlap=True,r=0.5):   # image_dir 批量处理图像文件夹 size 裁剪后的尺寸
    # 获取图片路径列表
    file_path_list = []
    for filename in os.listdir(image_dir):
        file_path = os.path.join(image_dir, filename)
        file_path_list.append(file_path)
    num = 0
    # 逐张读取图片剪裁
    for counter, image_path in enumerate(file_path_list):
        image = cv.imread(image_path)
        # image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
        #plt.imshow(image)
        image = cv.copyMakeBorder(image, train_tile_padding, train_tile_padding, train_tile_padding,
                                   train_tile_padding, cv.BORDER_REFLECT_101)

        h, w = image.shape[0:2]

        if  useoverlap:
            h_no = int((h // size)*2-1)
            w_no =  int((h // size)*2-1)
            size1 = size//2
        else:
            h_no = h // size
            w_no = w // size

        for row in range(0, h_no):
            for col in range(0, w_no):
                if useoverlap:
                    cropped_img = image[size1 * row: size1 * row + size1*2, size1 * col: size1 * col + size1*2, : ]
                else:
                    cropped_img = image[size*row : size*(row+1), size*col : size*(col+1), : ]
                cropped_img = cv.cvtColor(cropped_img, cv.COLOR_BGR2RGB)
                cv.imwrite(output_path + str(num) + ".tif",
                           cropped_img)
                num +=1
    print('原图剪裁:%d张'%num)

def gt(gt_dir, gt_path, size ,train_tile_padding,useoverlap=True,r=0.5):   # gt 批量处理图像文件夹 size 裁剪后的尺寸
    # 获取图片路径列表
    file_path_list = []
    for filename in os.listdir(gt_dir):
        file_path = os.path.join(gt_dir, filename)
        file_path_list.append(file_path)
    num = 0
    # 逐张读取图片剪裁
    for counter, image_path in enumerate(file_path_list):
        image = cv.imread(image_path,0)
        #image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
        #image = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
        r,image = cv.threshold(image, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)

        image = cv.copyMakeBorder(image, train_tile_padding, train_tile_padding, train_tile_padding,train_tile_padding, cv.BORDER_REFLECT_101)
        h, w = image.shape[0:2]
        if  useoverlap:
            h_no = int((h // size)*2-1)
            w_no = int((h // size)*2-1)
            size1 = size // 2
        else:
            h_no = h // size
            w_no = w // size

        for row in range(0, h_no):
            for col in range(0, w_no):
                if useoverlap:
                    cropped_img = image[size1 * row: size1 * row + size1*2, size1 * col: size1 * col + size1*2]
                else:
                    cropped_img = image[size*row : size*(row+1), size*col : size*(col+1)]
                cv.imwrite(gt_path + str(num) + ".tif",cropped_img)
                num +=1
    print('label剪裁共:%d 张' %num)
if __name__ == "__main__":

    image_dir = "D:/1学习资料/1研一/论文自写/dusamp/pic/roof2/结果图/unet/pred_he"
    output_path = "D:/1学习资料/1研一/论文自写/dusamp/pic/roof2/结果图/unet/pred/"
    gt_dir = "./Training Set/Target maps/"
    gt_path = "./data/train_384/label/"

    #image_dir_val = "./Validation Set/Input images/"
    #output_path_val = "./data/train/val/"
    #gt_dir_val = "./Validation Set/Target maps/"
    #gt_path_val = "./data/train/vallabel/"

    image_dir_val = "./Test Set/Input images/"
    output_path_val = "./data/train_384/val/"
    gt_dir_val = "./Test Set/Target maps/"
    gt_path_val = "./data/train_384/vallabel/"

    #image_dir_test = "./Test Set/Input images/"
    #output_path_test = "./test/val/"
    #gt_dir_test = "./Test Set/Target maps/"
    #gt_path_test = "./test/label/"

    size = 384
    train_tile_padding = 18 #1500 是18

    # crop_image(image_dir, output_path, size ,train_tile_padding,useoverlap=False)
    gt(image_dir, output_path, size, train_tile_padding,useoverlap=False)

    #crop_image(image_dir_val, output_path_val, size, train_tile_padding,useoverlap=False)
    #gt(gt_dir_val, gt_path_val, size, train_tile_padding,useoverlap=False)

    #crop_image(image_dir_test, output_path_test, size, train_tile_padding)
    #gt(gt_dir_test, gt_path_test, size, train_tile_padding)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值