yolo训练策略--使用 Python 和 OpenCV 进行图像亮度增强与批量文件复制之(图像增强是按梯度变化优化)

本文主要是介绍yolo训练策略--使用 Python 和 OpenCV 进行图像亮度增强与批量文件复制之(图像增强是按梯度变化优化),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

接上个博客:

https://blog.csdn.net/weixin_43269994/article/details/141753412

优化如下函数:

def augment_and_copy_files(base_folder, image_filename, num_augmentations=2, vgain_range=(1, 1.5), process_labels=True, process_annotations=True):base_filename, image_ext = os.path.splitext(image_filename)# 构建原始文件路径file_paths = {"images": os.path.join(base_folder, "images", image_filename),}if process_annotations:file_paths["annotations"] = os.path.join(base_folder, "annotations", f"{base_filename}.xml")if process_labels:file_paths["labels"] = os.path.join(base_folder, "labels", f"{base_filename}.txt")# 创建输出文件夹output_folders = create_output_folders(base_folder)# 复制原始图像copy_file(file_paths["images"], output_folders["images"], "", preserve_ext=True)if process_annotations:copy_file(file_paths["annotations"], output_folders["annotations"], "", preserve_ext=True)if process_labels:copy_file(file_paths["labels"], output_folders["labels"], "", preserve_ext=True)# 生成按梯度变化的增益值vgain_start, vgain_end = vgain_rangevgain_step = (vgain_end - vgain_start) / num_augmentationsfor i in range(1, num_augmentations + 1):vgain = vgain_start + i * vgain_stepbrightened_img = adjust_brightness(cv2.imread(file_paths["images"]), vgain)filename_suffix = f"_enhanced_{i}"output_image_path = copy_file(file_paths["images"], output_folders["images"], filename_suffix, preserve_ext=True)cv2.imwrite(output_image_path, brightened_img)print(f"Saved: {output_image_path}")if process_annotations:copy_file(file_paths["annotations"], output_folders["annotations"], filename_suffix, preserve_ext=True)print(f"Copied annotations: {output_image_path}")if process_labels:copy_file(file_paths["labels"], output_folders["labels"], filename_suffix, preserve_ext=True)print(f"Copied labels: {output_image_path}")print(f"All unique images and their annotations for {image_filename} have been enhanced and saved!")

这个函数 augment_and_copy_files 的目的是处理和增强图像,并将处理后的图像及其相关的注释和标签文件复制到指定的输出文件夹中。具体来说,它对图像进行亮度调整,并生成多个增强版本,同时可选择处理和复制对应的注释和标签文件。以下是详细解释:

  • base_folder: 原始数据的基路径。它包含了 images、annotations 和 labels 文件夹。
  • image_filename: 要处理的图像文件名。
  • num_augmentations: 生成的增强图像数量。
  • vgain_range: 亮度增益的范围,包含两个值,起始增益和结束增益。
  • process_labels: 布尔值,指示是否处理标签文件。
  • process_annotations: 布尔值,指示是否处理注释文件。

总体代码:

import cv2
import numpy as np
import os
import shutildef adjust_brightness(im, vgain):hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)hue, sat, val = cv2.split(hsv)val = np.clip(val * vgain, 0, 255).astype(np.uint8)enhanced_hsv = cv2.merge((hue, sat, val))brightened_img = cv2.cvtColor(enhanced_hsv, cv2.COLOR_HSV2BGR)return brightened_imgdef create_output_folders(base_folder):new_base_folder = os.path.join(os.path.dirname(base_folder), "augmented_data")output_folders = {"images": os.path.join(new_base_folder, "images"),"annotations": os.path.join(new_base_folder, "annotations"),"labels": os.path.join(new_base_folder, "labels")}for folder in output_folders.values():os.makedirs(folder, exist_ok=True)return output_foldersdef copy_file(src_path, dst_folder, filename_suffix, preserve_ext=True):base_filename, ext = os.path.splitext(os.path.basename(src_path))if preserve_ext:new_filename = f"{base_filename}{filename_suffix}{ext}"else:new_filename = f"{base_filename}{filename_suffix}"dst_path = os.path.join(dst_folder, new_filename)shutil.copy(src_path, dst_path)return dst_pathdef augment_and_copy_files(base_folder, image_filename, num_augmentations=2, vgain_range=(1, 1.5), process_labels=True, process_annotations=True):base_filename, image_ext = os.path.splitext(image_filename)# 构建原始文件路径file_paths = {"images": os.path.join(base_folder, "images", image_filename),}if process_annotations:file_paths["annotations"] = os.path.join(base_folder, "annotations", f"{base_filename}.xml")if process_labels:file_paths["labels"] = os.path.join(base_folder, "labels", f"{base_filename}.txt")# 创建输出文件夹output_folders = create_output_folders(base_folder)# 复制原始图像copy_file(file_paths["images"], output_folders["images"], "", preserve_ext=True)if process_annotations:copy_file(file_paths["annotations"], output_folders["annotations"], "", preserve_ext=True)if process_labels:copy_file(file_paths["labels"], output_folders["labels"], "", preserve_ext=True)# 生成按梯度变化的增益值vgain_start, vgain_end = vgain_rangevgain_step = (vgain_end - vgain_start) / num_augmentationsfor i in range(1, num_augmentations + 1):vgain = vgain_start + i * vgain_stepbrightened_img = adjust_brightness(cv2.imread(file_paths["images"]), vgain)filename_suffix = f"_enhanced_{i}"output_image_path = copy_file(file_paths["images"], output_folders["images"], filename_suffix, preserve_ext=True)cv2.imwrite(output_image_path, brightened_img)print(f"Saved: {output_image_path}")if process_annotations:copy_file(file_paths["annotations"], output_folders["annotations"], filename_suffix, preserve_ext=True)print(f"Copied annotations: {output_image_path}")if process_labels:copy_file(file_paths["labels"], output_folders["labels"], filename_suffix, preserve_ext=True)print(f"Copied labels: {output_image_path}")print(f"All unique images and their annotations for {image_filename} have been enhanced and saved!")def process_all_images_in_folder(base_folder, num_augmentations=2, vgain_range=(1, 1.5), process_labels=True, process_annotations=True):images_folder = os.path.join(base_folder, "images")for image_filename in os.listdir(images_folder):if image_filename.lower().endswith(('.bmp', '.jpg', '.jpeg', '.png')):augment_and_copy_files(base_folder, image_filename, num_augmentations, vgain_range, process_labels, process_annotations)# 使用示例
base_folder = r"C:\Users\linds\Desktop\fsdownload\upgrade_algo_so\data_res_2024_08_31_16_38\train"
process_all_images_in_folder(base_folder, num_augmentations=10, vgain_range=(1, 3), process_labels=True, process_annotations=False)

这篇关于yolo训练策略--使用 Python 和 OpenCV 进行图像亮度增强与批量文件复制之(图像增强是按梯度变化优化)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1124793

相关文章

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

浅析python如何去掉字符串中最后一个字符

《浅析python如何去掉字符串中最后一个字符》在Python中,字符串是不可变对象,因此无法直接修改原字符串,但可以通过生成新字符串的方式去掉最后一个字符,本文整理了三种高效方法,希望对大家有所帮助... 目录方法1:切片操作(最推荐)方法2:长度计算索引方法3:拼接剩余字符(不推荐,仅作演示)关键注意事

python版本切换工具pyenv的安装及用法

《python版本切换工具pyenv的安装及用法》Pyenv是管理Python版本的最佳工具之一,特别适合开发者和需要切换多个Python版本的用户,:本文主要介绍python版本切换工具pyen... 目录Pyenv 是什么?安装 Pyenv(MACOS)使用 Homebrew:配置 shell(zsh

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Python自动化提取多个Word文档的文本

《Python自动化提取多个Word文档的文本》在日常工作和学习中,我们经常需要处理大量的Word文档,本文将深入探讨如何利用Python批量提取Word文档中的文本内容,帮助你解放生产力,感兴趣的小... 目录为什么需要批量提取Word文档文本批量提取Word文本的核心技术与工具安装 Spire.Doc

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.