Python高分辨率图像的分割与合并

分割:

  # 目标分割大小
    DES_HEIGHT = 640
    DES_WIDTH = 640

    # 获取图像信息
    path_img = r'G:\A_SCI_DATASET\poppy\voc_wait_cut\img\12.jpg'

    #获取原始高分辨的图像的属性信息
    src = cv2.imread(path_img)
    height = src.shape[0]
    width = src.shape[1]

    # 把原始图像边缘填充至分割大小的整数倍
    padding_height = math.ceil(height / DES_HEIGHT) * DES_HEIGHT
    padding_width = math.ceil(width / DES_WIDTH) * DES_WIDTH

    #将padding图像与原始图像进行融合,使得原始
    padding_img = np.random.randint(0, 255, size=(padding_height, padding_width, 3)).astype(np.uint8)
    padding_img[0:height + 0, 0:width + 0] = src

    img = padding_img  ##读取彩色图像,图像的透明度(alpha通道)被忽略,默认参数;灰度图像;读取原始图像,包括alpha通道;可以用1,0,-1来表示
    sum_rows = img.shape[0]  # 高度
    sum_cols = img.shape[1]  # 宽度

    cols = DES_WIDTH
    rows = DES_HEIGHT

    save_path = "crop{0}_{1}\\".format(cols, rows)  #切割后的照片的存储路径
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    setDir(save_path)

    print("裁剪所得{0}列图片,{1}行图片.".format(int(sum_cols / cols), int(sum_rows / rows)))

    filename = os.path.split(path_img)[1]
    for i in range(int(sum_cols / cols)):
        for j in range(int(sum_rows / rows)):
            cv2.imwrite(
                save_path + os.path.splitext(filename)[0] + '_' + str(j) + '_' + str(i) + os.path.splitext(filename)[1],
                img[j * rows:(j + 1) * rows, i * cols:(i + 1) * cols, :])
    print("裁剪完成,得到{0}张图片.".format(int(sum_cols / cols) * int(sum_rows / rows)))
    print("文件保存在{0}".format(save_path))

合并:

#合并分割图像,指定行列数
def merge_picture(merge_path,num_of_cols,num_of_rows):
    filename=os.listdir(merge_path)
    full_path=os.path.join(merge_path,filename[0])
    # filename=file_name(merge_path,".tif")
    shape=cv2.imread(full_path).shape #三通道的影像需把-1改成1

    cols=shape[1]
    rows=shape[0]
    channels=shape[2]

    dst=np.zeros((rows*num_of_rows,cols*num_of_cols,channels),np.uint8)
    for i in range(len(filename)):
        full_path=os.path.join(merge_path,filename[i])
        img=cv2.imread(full_path,-1)
        cols_th=int(full_path.split("_")[-1].split('.')[0])
        rows_th=int(full_path.split("_")[-2])
        roi=img[0:rows,0:cols,:]
        dst[rows_th*rows:(rows_th+1)*rows,cols_th*cols:(cols_th+1)*cols,:]=roi
    cv2.imwrite(merge_path+"merge.jpg",dst)
  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIOT魔法师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值