使用albumentations进行数据增强保存图像

import cv2
import numpy as np
import albumentations as A
from albumentations.pytorch import ToTensorV2

# 读取图片
image = cv2.imread("test_2_0000_0000.png")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# 定义数据增强的transform
transform = A.Compose([
    # A.RandomRotate90(),  # 随机旋转90度
    # A.HorizontalFlip(p=0.5),  # 水平翻转
    # A.VerticalFlip(p=0.5),  # 垂直翻转
    # A.RandomResizedCrop(256, 256, scale=(0.5, 1.0)),  # 随机缩放
    A.ColorJitter(p=0.5),  # 色彩调整
    ToTensorV2()  # 转换为PyTorch张量
])

# 对图片进行数据增强
augmented = transform(image=image)

# 获取增强后的图像
augmented_image = augmented["image"].permute(1, 2, 0).numpy()  # 转换为NumPy数组

# 将增强后的图像保存为文件
cv2.imwrite("augmented_image.jpg", cv2.cvtColor(augmented_image, cv2.COLOR_RGB2BGR))

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Albumentations 库可以很方便地进行批量图像增强保存。下面是一些示例代码: ```python import os import cv2 import albumentations as A from albumentations.augmentations.bbox_utils import BboxParams # 定义增强器 transform = A.Compose([ A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(p=0.2), A.Resize(width=512, height=512, p=1), ], bbox_params=BboxParams(format='pascal_voc', label_fields=['category_ids'])) # 定义图像和标注文件夹 img_folder = 'images/' ann_folder = 'annotations/' # 定义输出文件夹 output_folder = 'output/' if not os.path.exists(output_folder): os.makedirs(output_folder) # 循环遍历图像和标注文件夹中的所有文件 for img_file in os.listdir(img_folder): # 读取图像和标注文件 img_path = os.path.join(img_folder, img_file) ann_file = os.path.join(ann_folder, img_file[:-4] + '.xml') img = cv2.imread(img_path) # 读取标注信息 bboxes = [] categories = [] with open(ann_file, 'r') as f: lines = f.readlines() for line in lines: if '<xmin>' in line: xmin = int(line.strip().split('>')[1].split('<')[0]) elif '<ymin>' in line: ymin = int(line.strip().split('>')[1].split('<')[0]) elif '<xmax>' in line: xmax = int(line.strip().split('>')[1].split('<')[0]) elif '<ymax>' in line: ymax = int(line.strip().split('>')[1].split('<')[0]) elif '<name>' in line: category = line.strip().split('>')[1].split('<')[0] categories.append(category) bboxes.append([xmin, ymin, xmax, ymax]) # 进行图像增强和标注转换 transformed = transform(image=img, bboxes=bboxes, category_ids=categories) transformed_img = transformed['image'] transformed_bboxes = transformed['bboxes'] transformed_categories = transformed['category_ids'] # 保存增强后的图像和标注文件 output_img_file = os.path.join(output_folder, img_file[:-4] + '_aug.jpg') cv2.imwrite(output_img_file, transformed_img) output_ann_file = os.path.join(output_folder, img_file[:-4] + '_aug.xml') with open(output_ann_file, 'w') as f: f.write('<annotation>\n') f.write(' <filename>{}</filename>\n'.format(img_file[:-4] + '_aug.jpg')) f.write(' <size>\n') f.write(' <width>{}</width>\n'.format(transformed_img.shape[1])) f.write(' <height>{}</height>\n'.format(transformed_img.shape[0])) f.write(' <depth>{}</depth>\n'.format(transformed_img.shape[2])) f.write(' </size>\n') for i, bbox in enumerate(transformed_bboxes): xmin, ymin, xmax, ymax = bbox category = transformed_categories[i] f.write(' <object>\n') f.write(' <name>{}</name>\n'.format(category)) f.write(' <bndbox>\n') f.write(' <xmin>{}</xmin>\n'.format(xmin)) f.write(' <ymin>{}</ymin>\n'.format(ymin)) f.write(' <xmax>{}</xmax>\n'.format(xmax)) f.write(' <ymax>{}</ymax>\n'.format(ymax)) f.write(' </bndbox>\n') f.write(' </object>\n') f.write('</annotation>\n') ``` 上面的代码中,我们使用了 `A.Compose` 定义了增强器,然后通过循环遍历图像和标注文件夹中的所有文件,读取图像和标注信息,进行增强和标注转换,最后保存增强后的图像和标注文件到输出文件夹中。其中,我们使用了 OpenCV 库来读取和保存图像使用了标注文件的 XML 格式来保存标注信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值