基于Python的有重叠区域的批量图像分割

1. 代码部分

代码参考自网络,可实现将图像分割为800 x 800。如果需要同步分割标签内容,请移步另一篇文章:利用Python将图像与xml标签同步分割。

import cv2
import os

def tianchong_you(img):
    size = img.shape
    # 这里的大小可以自己设定,但是尽量是32的倍数
    constant = cv2.copyMakeBorder(img,0,0,0,800-size[1],cv2.BORDER_CONSTANT,value=(107,113,115))#填充值为数据集均值
    return constant

def tianchong_xia(img):
    size = img.shape
    constant = cv2.copyMakeBorder(img,0,800-size[0],0,0,cv2.BORDER_CONSTANT, value=(107, 113, 115))
    return constant

def tianchong_xy(img):
    size = img.shape
    constant = cv2.copyMakeBorder(img,0,800-size[0],0,800-size[1],cv2.BORDER_CONSTANT,value=(107,113,115))
    return constant

def caijian(path, path_out, size_w=800, size_h=800, step=700): #重叠度为100
    ims_list=os.listdir(path)
    count = 0
    for im_list in ims_list:
        number = 0
        name = im_list[:-4]  #去处“.png后缀”
        print(name)
        img = cv2.imread(path+im_list)
        size = img.shape
        if size[0]>=800 and size[1]>=800:
           count = count + 1
           for h in range(0,size[0]-1,step):
               star_h = h
               for w in range(0,size[1]-1,step):
                   star_w = w
                   end_h = star_h + size_h
                   if end_h > size[0]:
                      star_h = size[0] - size_h
                      end_h = star_h + size_h
                   end_w = star_w + size_w
                   if end_w > size[1]:
                      star_w = size[1] - size_w
                   end_w = star_w + size_w
                   cropped = img[star_h:end_h, star_w:end_w]
                   name_img = name + '_'+ str(star_h) +'_' + str(star_w)#用起始坐标来命名切割得到的图像,为的是方便后续标签数据抓取
                   cv2.imwrite('{}/{}.png'.format(path_out,name_img),cropped)
                   number = number + 1
        if size[0]>=800 and size[1]<800:
            print('图片{}需要在右面补齐'.format(name))
            count = count + 1
            img0 = tianchong_you(img)
            for h in range(0,size[0]-1,step):
               star_h = h
               star_w = 0
               end_h = star_h + size_h
               if end_h > size[0]:
                  star_h = size[0] - size_h
                  end_h = star_h + size_h
               end_w = star_w + size_w
               cropped = img0[star_h:end_h, star_w:end_w]
               name_img = name + '_'+ str(star_h) +'_' + str(star_w)
               cv2.imwrite('{}/{}.png'.format(path_out,name_img),cropped)
               number = number + 1
        if size[0]<800 and size[1]>=800:
            count = count + 1
            print('图片{}需要在下面补齐'.format(name))
            img0 = tianchong_xia(img)
            for w in range(0,size[1]-1,step):
               star_h = 0
               star_w = w
               end_w = star_w + size_w
               if end_w > size[1]:
                  star_w = size[1] - size_w
                  end_w = star_w + size_w
               end_h = star_h + size_h
               cropped = img0[star_h:end_h, star_w:end_w]
               name_img = name + '_'+ str(star_h) +'_' + str(star_w)
               cv2.imwrite('{}/{}.png'.format(path_out,name_img),cropped)
               number = number + 1
        if size[0]<800 and size[1]<800:
            count = count + 1
            print('图片{}需要在下面和右面补齐'.format(name))
            img0 = tianchong_xy(img)
            cropped = img0[0:800, 0:800]
            name_img = name + '_'+ '0' +'_' + '0'
            cv2.imwrite('{}/{}.png'.format(path_out,name_img),cropped)
            number = number + 1
        print('图片{}切割成{}张'.format(name,number))
        print('共完成{}张图片'.format(count))

if __name__ == '__main__':
    ims_path = 'E:/BaiduNetdiskDownload/out_dota/output_dota/'# 图像数据集的路径
    path = 'E:/BaiduNetdiskDownload/out_dota/images_split/' #切割得到的数据集存放路径
    caijian(ims_path, path, size_w=800, size_h=800, step=600)

2. 需修改参数部分
如果需要分割为其他大小,可修改相应参数。如修改为:分割为400 x 400像素大小,分割重叠区域设置为100,需修改以下部分:
(1)修改91,92行的图像路径与裁剪后的路径。其中ims_path改为原图像路径,path为裁剪后图像保存路径。
在这里插入图片描述

(2)代码93行参数size_w=400, size_h=400, step=300。其中step为步长。

# 原代码
caijian(ims_path, path, size_w=800, size_h=800, step=600)
# 更改裁剪尺寸后的代码
caijian(ims_path, path, size_w=400, size_h=400, step=300)

(3)将代码中所有的800改为400:按ctrl+F搜索800,点击选中全部可实现一键修改。
在这里插入图片描述

你可能感兴趣的:(深度学习,python,计算机视觉,opencv)