深度学习过程中如果图像过大会占用更多内存,因此往往将图片切分成许多较小的图片放入网络学习。以下代码能同时将数据集和标签裁剪,图片名称需要和对应标签名称一致。切分效果参考下面链接 。
import cv2
import numpy
import scipy.io as scio
import math
import os
# 待裁剪的标签mask和图像img
# 待裁剪的图片 两者实际操作相同,如果只裁剪一类图像,如只有mask,则删除对应的裁剪图片的代码即可
maskFile = r'C:\Users\Aisin Gioro dong po\Desktop\train_label'
imgFile = r'C:\Users\Aisin Gioro dong po\Desktop\train'
# 新文件路径
new_file_path = r'C:\Users\Aisin Gioro dong po\Desktop\train1'
new_file_path_label = r'C:\Users\Aisin Gioro dong po\Desktop\train_label1'
# 判断是否存在目标文件夹,不存在则创建
if os.path.isdir(new_file_path):
pass
else:
os.makedirs(new_file_path)
if os.path.isdir(new_file_path_label):
pass
else:
os.makedirs(new_file_path_label)
list_mask = []
list_img = []
# 获取所有mask图像的绝对路径(包括图片名称)保存在list_mask列表
for root, _, fnames in sorted(os.walk(maskFile)): # sorted() 函数对所有可迭代的对象进行排序操作;
# os.walk()方法是用来获取指定路径下所有文件:返回三个参数的列表,
# ①root保存的就是当前遍历的文件夹的绝对路径;②dirs保存当前文件夹下的所有子文件夹的名称;③files保存当前文件夹下的所有文件的名称
for fname in fnames:
path = os.path.join(root, fname)
list_mask.append(path)
# 获取所有img图像的绝对路径(包括图片名称)保存在list_img列表
for root, _, fnames in sorted(os.walk(imgFile)):
for fname in fnames:
path = os.path.join(root, fname)
list_img.append(path)
for i in range(len(list_mask)):
p, n = os.path.split(list_mask[i]) # os.path.split() 返回文件的路径和文件名
pro, ext = os.path.splitext(n) # os.path.splitext() 将文件名和扩展名分开
print(pro) # 打印文件名
mask = cv2.imread(list_mask[i]) # cv2.imread 用来从文件中读取图像
img = cv2.imread(list_img[i])
(h, w, c) = mask.shape # shape返回图片的高,宽,通道数
h_n = math.ceil(h / 512.0) # 裁剪的高 512修改为想裁剪的大小; ceil()用于向上取整
w_n = math.ceil(w / 512.0) # 裁剪的宽 512修改为想裁剪的大小
(h, w, c) = img.shape
for i in range(h_n):
if i < h_n - 1:
for j in range(w_n):
if j < w_n - 1:
mask_patch = mask[i * 512:(i + 1) * 512, j * 512:(j + 1) * 512, :] # a:b表示从a到b
img_patch = img[i * 512:(i + 1) * 512, j * 512:(j + 1) * 512, :]
mask_pathname = os.path.join(new_file_path_label,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
img_pathname = os.path.join(new_file_path,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
# str(i) 它将参i数转换成字符串类型
# imwrite(filename, img[, params])保存图像到硬盘。参数filename表示写入硬盘文件的路径、文件名和扩展名;参数img表示待写入的源图像
cv2.imwrite(mask_pathname, mask_patch)
cv2.imwrite(img_pathname, img_patch)
# 剩余图像不够切分时,从右向左取图像
else:
mask_patch = mask[i * 512:(i + 1) * 512, (w - 512):, :]
img_patch = img[i * 512:(i + 1) * 512, (w - 512):, :]
mask_pathname = os.path.join(new_file_path_label,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
img_pathname = os.path.join(new_file_path,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
cv2.imwrite(mask_pathname, mask_patch)
cv2.imwrite(img_pathname, img_patch)
else:
for j in range(w_n):
if j < w_n - 1:
mask_patch = mask[(h - 512):, j * 512:(j + 1) * 512, :]
img_patch = img[(h - 512):, j * 512:(j + 1) * 512, :]
mask_pathname = os.path.join(new_file_path_label,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
img_pathname = os.path.join(new_file_path,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
cv2.imwrite(mask_pathname, mask_patch)
cv2.imwrite(img_pathname, img_patch)
else:
mask_patch = mask[(h - 512):, (w - 512):, :]
img_patch = img[(h - 512):, (w - 512):, :]
mask_pathname = os.path.join(new_file_path_label,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
img_pathname = os.path.join(new_file_path,
pro[:4] + '-' + str(i) + '-' + str(j) + '.png')
cv2.imwrite(mask_pathname, mask_patch)
cv2.imwrite(img_pathname, img_patch)