【语义分割】关于在使用Augmentor遇到的问题

  1. 注意事项
    需要确保图像原图和掩膜图像的命名完全一样,例如原图命名为1.tif,那么掩膜图像的名字也应该叫1.tif
  2. 分类问题
    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)  # 将文件移动到目标文件夹中
  1. 命名问题
    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}")

  1. 参考博文
    【语义分割项目实战】基于Augmentor工具的语义分割中常见几种数据增强方式(一)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值