- 注意事项
需要确保图像原图和掩膜图像的命名完全一样,例如原图命名为1.tif,那么掩膜图像的名字也应该叫1.tif - 分类问题
Augmentor无法对原图和掩膜图像进行分类
以下是解决代码(根据文件名的首字母进行划分)
import os
import shutil
# 指定图像文件所在文件夹的路径
img_folder = r'D:\Desk\Test1\output'
# 创建两个文件夹用于存储不同类型的文件
folder1 = os.path.join(img_folder, 'type1')
folder2 = os.path.join(img_folder, 'type2')
# 如果文件夹不存在,则创建
if not os.path.exists(folder1):
os.mkdir(folder1)
if not os.path.exists(folder2):
os.mkdir(folder2)
# 循环遍历图像文件夹中的每个文件
for filename in os.listdir(img_folder):
# 获取文件名的首个字符
first_char = filename[0]
# 根据首个字符将文件移动到相应的文件夹中
if first_char == '_':
src_path = os.path.join(img_folder, filename) # 源文件的完整路径
dst_path = os.path.join(folder1, filename) # 目标文件的完整路径
shutil.move(src_path, dst_path) # 将文件移动到目标文件夹中
elif first_char == 'T':
src_path = os.path.join(img_folder, filename) # 源文件的完整路径
dst_path = os.path.join(folder2, filename) # 目标文件的完整路径
shutil.move(src_path, dst_path) # 将文件移动到目标文件夹中
- 命名问题
Augmentor增强后的图片的命名比较杂乱
以下是一个重命名的代码
import os
# 指定文件夹路径
image_folder = r'D:\Desk\Test1\output\type1'
label_folder = r'D:\Desk\Test1\output\type2'
# 遍历文件夹中的所有图像文件
for idx, (img_file, lbl_file) in enumerate(zip(os.listdir(image_folder), os.listdir(label_folder))):
# 检查文件扩展名,确保它是一个图像文件
if not (img_file.endswith('.tif') or img_file.endswith('.tiff') or img_file.endswith('.img')):
continue
if not (lbl_file.endswith('.tif') or lbl_file.endswith('.tiff') or lbl_file.endswith('.img')):
continue
# 构建新文件名,比如 "image_001.tif", "image_002.tif" ...
img_new_filename = f"img_{idx+1:03d}.tif"
# 构建新文件名,比如 "label_001.tif", "label_002.tif" ...
lbl_new_filename = f"lbl_{idx+1:03d}.tif"
# 构建旧文件路径和新文件路径
img_old_path = os.path.join(image_folder, img_file)
img_new_path = os.path.join(image_folder, img_new_filename)
lbl_old_path = os.path.join(label_folder, lbl_file)
lbl_new_path = os.path.join(label_folder, lbl_new_filename)
# 重命名文件
os.rename(img_old_path, img_new_path)
os.rename(lbl_old_path, lbl_new_path)
print(f"{img_file} and {lbl_file} renamed to {img_new_filename} and {lbl_new_filename}")
- 参考博文
【语义分割项目实战】基于Augmentor工具的语义分割中常见几种数据增强方式(一)