行业知识
为YOLOv5、YOLOv8带来全新的数据增强方式-合成雾增强算法
2026-11-19 11:07:31
来源:互联网转载
简介
BestYOLO:https://github.com/WangRongsheng/BestYOLO
BestYOLO是一个以科研和竞赛为导向的最好的YOLO实践框架!
目前BestYOLO是一个完全基于YOLOv5 v7.0 进行改进的开源库,该库将始终秉持以落地应用为导向,以轻便化使用为宗旨,简化各种模块的改进。目前已经集成了基于torchvision.models 模型为Backbone的YOLOv5目标检测算法,同时也将逐渐开源更多YOLOv5应用程序。
合成雾增强算法
合成雾数据增强算法是一种基于图像处理技术的算法,用于增加由雾霾天气产生的雾气效果,从而提高图像的质量和可用性。该算法通过模拟雾气的形成原理,对图像进行处理和合成,使其看起来更加真实和自然。
合成雾数据增强算法的具体实现步骤如下:
-
提取图像的深度信息和细节信息,包括场景的几何结构、纹理和颜色等。
-
通过计算雾气的传播模型,确定雾气的密度和浓度,从而模拟出雾气效果。
-
根据模拟的雾气效果,对图像进行混合处理,包括颜色平衡、对比度调整以及明暗度等参数的调整。
-
针对特定场景的需求,可以对雾气的效果进行调整和优化,比如增强景深、调整雾气的颜色和透明度等。
通过合成雾数据增强算法,可实现对图像的自然场景雾化处理,从而提高图像的可视化效果和实用性。该算法在计算机视觉、图像处理、人工智能等领域都有着广泛的应用和研究价值。
实现效果
为YOLOv5\YOLOv8引入合成雾数据增强算法
synthetic_fog.py
"""直接运行程序可以测试合成雾气效果Produced by: zhangzhengde@sjtu.edu.cn"""import osimport mathimport cv2import timefrom pathlib import Pathimport numpy as npimport matplotlib.pyplot as pltfrom tqdm import tqdmimport shutilclass SyntheticFog(object): def __init__(self): pass def __call__(self, show=False): img_path = '../example/fog_image/raw.jpg' # img_path = '../sources/IMG_6685.JPG' assert os.path.exists(img_path), f'error: img does not exists, {img_path}' img = cv2.imread(img_path) print(img.shape) img = img/255.0 print(f'fogging...') t0 = time.time() br = 0.7 th = 0.05 fogged_img = self.fogging_img( img, brightness=br, thickness=th, high_efficiency=True) print(f'fogging time: {(time.time()-t0)*1000:.4f}ms') rf = 1 # resize factor img = cv2.resize(img, (int(img.shape[1]*rf), int(img.shape[0]*rf))) fogged_img = cv2.resize(fogged_img, ((int(fogged_img.shape[1]*rf)), (int(fogged_img.shape[0]*rf)))) fogged_img = np.array(fogged_img*255, dtype=np.uint8) if show: cv2.imshow('src', img) cv2.imshow('fogged', fogged_img) cv2.waitKey(0) cv2.imwrite(f'../example/fog_image/fogged_br{br}_th{th}.jpg', fogged_img) def fogging_dir(self, sp, tp=None, random_params=True, brightness=None, thickness=None, save_src_img=False): """ fogging images in a directory :param sp: str, source dir path :param tp: str, target dir path, tp is fogged_{sp} by default :param random_params: bool, use random brightness and fog thickness params if True :param brightness: float, 0.1 to 0.9, gray of synthetic fog, pure white fog if 1, dark fog if 0. :param thickness: float, 0.01 to 0.09, thickness of synthetic fog, the larger the value, the thicker the fog. :param save_src_img: save source image at the same time i.e. copy source imgs to tp :return: None, all fogged images will be saved to target dir path. """ tp = tp if tp is not None else f'{Path(sp).parent}/fogged_{Path(sp).name}' if os.path.exists(tp): ipt = input(f'Target dir: {tp} exists, do you want to remove it and continue. [Yes]/No: ') if ipt in ['', 'Yes', 'Y', 'yes']: shutil.rmtree(tp) else: print('do nothing') exit() os.makedirs(f'{tp}') imgs = [x for x in os.listdir(sp) if str(Path(x).suffix).lower() in ['.jpg', '.bmp']] print(f'Fogging {len(imgs)} images in dir {sp}, \nfogged images will be save to {tp}.') bar = tqdm(imgs) for i, img_name in enumerate(bar): img_path = f'{sp}/{img_name}' # stem = Path(img_path).stem # suffix = Path(img_path).suffix if save_src_img: # save source img shutil.copy(f'{sp}/{img_name}', f'{tp}/{img_name}') img = cv2.imread(img_path) h, w, c = img.shape normed_img = img.copy()/255.0 if random_params: br = np.clip(0.2 * np.random.randn() + 0.5, 0.1, 0.9) # 0.1~0.9 th = np.clip(0.01 * np.random.randn() + 0.05, 0.01, 0.09) else: br = brightness th = thickness assert br is not None assert th is not None fogged_img = self.fogging_img(normed_img, br, th, high_efficiency=True) fogged_img = np.array(fogged_img * 255, dtype=np.uint8) cv2.imwrite(f'{tp}/fogged_{img_name}', fogged_img) bar.set_description(f'Fogged image saved, fogged_{img_name}') def fogging_img(self, img, brightness=0.7, thickness=0.05, high_efficiency=True): """ fogging single image :param img: src img :param brightness: brightness :param thickness: fog thickness, without fog when 0, max 0.1, :param high_efficiency: use matrix to improve fogging speed when high_efficiency is True, else use loops low efficiency: about 4000ms, high efficiency: about 80ms, tested in (864, 1152, 3) img :return: fogged image """ assert 0
上一篇:怎么设置mail服务器?