python:图片数据集批量处理 格式、大小、亮度

格式:

1.在放着统一为一种格式图片的文件夹里新建一个记事本文件,任意取名。
2.若将jpg格式批量转化为png格式,在刚才新建的记事本文档里输入

ren *.jpg *.png

3.不要忽略空格,然后将后缀名修改为“bat”。
4.双击“bat”后缀文件,等几秒后所有的图片格式自动转换为png格式。
5.反之格式转换为ren *.png *.jpg

大小:

from PIL import Image
import os.path
import glob
def convertimg(imgfile,outdir,width=320,height=240):#需要的大小
  img=Image.open(imgfile)
  new_img=img.resize((width,height),Image.BILINEAR)
  new_img.save(os.path.join(outdir,os.path.basename(imgfile)))
for imgfile in glob.glob("./0/*.png"):#待转换图片
  convertimg(imgfile,"./00/")#保存文件夹

亮度、饱和度:

import numpy as np
import cv2
import os
MAX_VALUE = 100# 调整最大值
def update(input_img_path, output_img_path, lightness, saturation):
  """
  用于修改图片的亮度和饱和度
  :param input_img_path: 图片路径
  :param output_img_path: 输出图片路径
  :param lightness: 亮度
  :param saturation: 饱和度
  """

  # 加载图片 读取彩色图像归一化且转换为浮点型
  image = cv2.imread(input_img_path, cv2.IMREAD_COLOR).astype(np.float32) / 255.0

  # 颜色空间转换 BGR转为HLS
  hlsImg = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)

  # 1.调整亮度(线性变换)
  hlsImg[:, :, 1] = (1.0 + lightness / float(MAX_VALUE)) * hlsImg[:, :, 1]
  hlsImg[:, :, 1][hlsImg[:, :, 1] > 1] = 1
  # 饱和度
  hlsImg[:, :, 2] = (1.0 + saturation / float(MAX_VALUE)) * hlsImg[:, :, 2]
  hlsImg[:, :, 2][hlsImg[:, :, 2] > 1] = 1
  # HLS2BGR
  lsImg = cv2.cvtColor(hlsImg, cv2.COLOR_HLS2BGR) * 255
  lsImg = lsImg.astype(np.uint8)
  cv2.imwrite(output_img_path, lsImg)

dataset_dir = './0'
output_dir = './00'

lightness = int(input("lightness(亮度-100~+100):"))  # 变换亮度
saturation = int(input("saturation(饱和度-100~+100):"))  # 变换饱和度

# 获得需要转化的图片路径并生成目标路径
image_filenames = [(os.path.join(dataset_dir, x), os.path.join(output_dir, x))
                   for x in os.listdir(dataset_dir)]
# 转化所有图片
for path in image_filenames:
  update(path[0], path[1], lightness, saturation)
  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Albumentations 库对 YOLOX 数据集进行批量增强的代码示例: ```python import os import cv2 import albumentations as A # 定义增强函数 def get_aug(): return A.Compose([ A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(p=0.2), A.RandomGamma(p=0.2), A.Blur(p=0.2), A.ChannelShuffle(p=0.2), A.RGBShift(p=0.2), A.RandomFog(p=0.2), A.Rotate(limit=10, border_mode=cv2.BORDER_CONSTANT, value=0, p=0.5), A.RandomCrop(height=512, width=512, p=0.5), A.Resize(height=416, width=416, p=1), ], bbox_params=A.BboxParams(format='yolo', min_visibility=0.4, label_fields=['category_id'])) # 定义数据集路径和输出路径 data_dir = '/path/to/yolox_dataset' output_dir = '/path/to/output_directory' # 遍历数据集文件夹,对每张图片进行增强并保存到输出路径中 for img_name in os.listdir(data_dir): img_path = os.path.join(data_dir, img_name) img = cv2.imread(img_path) annotations_path = os.path.join(data_dir, img_name.replace('.jpg', '.txt')) with open(annotations_path, 'r') as f: annotations = f.readlines() bboxes = [] categories = [] for annotation in annotations: annotation = annotation.strip().split() category_id = int(annotation[0]) x_center = float(annotation[1]) y_center = float(annotation[2]) width = float(annotation[3]) height = float(annotation[4]) x_min = int((x_center - width / 2) * img.shape[1]) y_min = int((y_center - height / 2) * img.shape[0]) x_max = int((x_center + width / 2) * img.shape[1]) y_max = int((y_center + height / 2) * img.shape[0]) bboxes.append([x_min, y_min, x_max, y_max]) categories.append(category_id) augmented = get_aug()(image=img, bboxes=bboxes, category_id=categories) img_aug = augmented['image'] bboxes_aug = augmented['bboxes'] categories_aug = augmented['category_id'] annotations_aug = [] for i in range(len(bboxes_aug)): x_min = bboxes_aug[i][0] / img_aug.shape[1] y_min = bboxes_aug[i][1] / img_aug.shape[0] x_max = bboxes_aug[i][2] / img_aug.shape[1] y_max = bboxes_aug[i][3] / img_aug.shape[0] width = x_max - x_min height = y_max - y_min category_id = categories_aug[i] annotations_aug.append(f"{category_id} {x_min} {y_min} {width} {height}") img_aug_path = os.path.join(output_dir, img_name) cv2.imwrite(img_aug_path, img_aug) annotations_aug_path = os.path.join(output_dir, img_name.replace('.jpg', '.txt')) with open(annotations_aug_path, 'w') as f: f.write('\n'.join(annotations_aug)) ``` 在上述代码中,我们定义了一个名为 `get_aug()` 的函数,用于返回 Albumentations 库中一系列增强操作的组合。这个函数实现了水平翻转、亮度和对比度调整、Gamma 调整、模糊、通道随机交换、RGB 位移、雾化、旋转、随机裁剪和缩放等操作。 接下来,我们遍历数据集文件夹中的所有图片,并对每张图片进行增强。对于每张图片,我们首先读取其对应的标注文件,然后使用 `get_aug()` 函数对图片和标注进行增强。最后,我们将增强后的图片和标注保存到输出路径中。注意,在保存标注时,我们需要将标注的格式转换为 YOLO 格式。 需要注意的是,以上代码只是一个示例,具体的增强操作和参数需要根据实际情况进行调整

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值