目标检测数据集图片及标签同步对比度/亮度增强

前言

在深度学习领域,尤其是目标检测任务中,数据集的质量直接影响模型的性能。为了提升模型的鲁棒性和对各种场景的适应能力,数据增强技术被广泛应用于图像数据集处理。对比度和亮度增强是常见的数据增强方法,它们可以使图像在不同光照和对比度条件下表现得更加自然,帮助模型学习在多样化的视觉环境中提取有效特征。然而,仅增强图像是不够的,因为标签也需要与图像同步调整。

在本篇博客中,我们将探讨如何对目标检测数据集中的图片进行对比度和亮度的同步增强,并确保标签的准确性不受影响。这种同步增强可以提高模型在低光照、强对比度等复杂场景中的表现,有效增强检测效果和泛化能力。

实现代码

代码如下,其中alpha参数修改为对比度调整,alpha 的值为 1.0 时表示不变,大于 1.0 增强对比度,小于 1.0 减弱对比度,一般调整0.5-2.0;beta参数修改为亮度调整,beta 为 0 时表示亮度不变,正值增加亮度,负值降低亮度,一般调整负100-正100,可以根据效果自行调整数值。

import cv2
import numpy as np
import os
import xml.etree.ElementTree as ET

def getColorImg(alpha,beta,img_path,img_write_path):
    img = cv2.imread(img_path)
    colored_img = np.uint8(np.clip((alpha * img + beta), 0, 255))
    cv2.imwrite(img_write_path,colored_img)

def getColorAnno(anno_path,anno_write_path):
    tree = ET.parse(anno_path)
    tree.write(anno_write_path)

def color(alpha,beta,img_dir,anno_dir,img_write_dir,anno_write_dir):
    if not os.path.exists(img_write_dir):
        os.makedirs(img_write_dir)
    if not os.path.exists(anno_write_dir):
        os.makedirs(anno_write_dir)
    img_names=os.listdir(img_dir)
    for img_name in img_names:
        img_path=os.path.join(img_dir,img_name)
        img_write_path=os.path.join(img_write_dir,img_name[:-4]+'color'+str(int(alpha*10))+'.jpg')
        anno_path=os.path.join(anno_dir,img_name[:-4]+'.xml')
        anno_write_path = os.path.join(anno_write_dir, img_name[:-4]+ 'color'+str(int(alpha*10))+'.xml')
        getColorImg(alpha,beta,img_path,img_write_path)
        getColorAnno(anno_path,anno_write_path)
        print(img_name)

alpha=0.7  #修改对比度
beta=10 #修改亮度
img_dir=r'D:\VOC2007\JPEGImages' #原始图像路径
anno_dir=r'D:\VOC2007\Annotations' #原始标签路径
img_write_dir=r'D:\VOC2007\JPEGImages_color' #输出图像路径
anno_write_dir=r'D:\VOC2007\Annotations_color' #输出标签路径

color(alpha,beta,img_dir,anno_dir,img_write_dir,anno_write_dir)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑脸惹桃花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值