该代码是在处理DeepGlobe时所写,可灵活应用
deepglobe 数据训练图片和标签的大小都是1024×1024,而我们要处理成256×256;便可以使用下列代码。
def clip():
imgpath = "Original training image path"
labelpath = "Original label path"
imgfile = os.listdir(imgpath)
labelfile = os.listdir(labelpath)
conut = len(imgfile)
strip = np.int32(1024/256) #256 随自己变动
for i in range(conut):
imgp = imgpath + "/" + imgfile[i]
labelp = labelpath + "/" + labelfile[i]
imgname = imgfile[i][0:-4]
labelname = labelfile[i][0:-4]
img = Image.open(imgp)
label = Image.open(labelp)
saveimg = "save image path" + imgname
savelabel = "save label path" + labelname
c = 0
for j in range(strip):
for n in range(strip):
box = (256*j, 256*n, 256*(j+1), 256*(n+1)) #裁剪关键
img2 = img.crop(box)
label2 = label.crop(box)
si = saveimg + " %d" % (c+1) + ".png"
sl = savelabel + " %d " % (c+1) + ".png"
img2.save(si)
label2.save(sl)
c = c + 1