目标检测:python实现多种图像数据增强的方法(光照,对比度,遮挡,模糊)

本文介绍了使用Python进行图像数据增强的各种方法,包括直方图均衡化、CLAHE自适应对比度增强、白平衡、亮度和对比度调整、高光去除、自适应亮度增强、随机遮挡、图像模糊等,以提升模型训练的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图像数据增强的内容(可根据需要自定义选择):
1.直方图均衡化
2.clahe自适应对比度直方图均衡化
3.白平衡
4.亮度增强
5.亮度,饱和度,对比度增强
6.去除图像上的高光部分
7.自适应亮度增强
8.随机遮挡
9.图像高斯模糊
10.压缩图像

# -*- coding: utf-8 -*-

"""
******不改变原始xml的一些数据增强方法 type 1-10*******
把增强后的图像和xml一起放入新的文件夹
rootpath:picture_xml原始路径
savepath:picture_xml保存路径
*******改变原始 xml的一些数据增强方法  type 11-15******
修改图片的同时修改对应的xml
file_path:传入类别的信息txt,最好和生成labelmap的顺序一致
rootpath:picture_xml原始路径
savepath:picture_xml保存路径

11:自定义裁剪,图像大小 w,h,例如 w=400,h=600
12:自定义平移,平移比例 w,h [0-1] 例如w=0.1,h=0,2
13:自定义缩放,调整图像大小 w,h,例如 w=400,h=600
14:图像翻转
15:图像任意旋转,传入旋转角度列表anglelist=[90,-90]

"""
import cv2
import random
import math
import os,shutil
import numpy as np
from PIL import Image, ImageStat
from skimage import exposure
import matplotlib.pyplot as plt
import tensorlayer as tl
from lxml.etree import Element, SubElement, tostring
import xml.etree.ElementTree as ET


def hisColor_Img(path):
    """
    对图像直方图均衡化
    :param path: 图片路径
    :return: 直方图均衡化后的图像
    """
    img = cv2.imread(path)
    ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
    channels = cv2.split(ycrcb)
    cv2.equalizeHist(channels[0], channels[0]) #equalizeHist(in,out)
    cv2.merge(channels, ycrcb)
    img_eq=cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR)
    return img_eq

def clahe_Img(path,ksize):
    """
    :param path: 图像路径
    :param ksize: 用于直方图均衡化的网格大小,默认为8
    :return: clahe之后的图像
    """
    image = cv2.imread(path, cv2.IMREAD_COLOR)
    b, g, r = cv2.split(image)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(ksize,ksize))
    b = clahe.apply(b)
    g = clahe.apply(g)
    r = clahe.apply(r)
    image = cv2.merge([b, g, r])
    return image

def whiteBalance_Img(path):
    """
    对图像白平衡处理
    """
    img = cv2.imread(path)
    b, g, r = cv2.split(img)
    Y = 0.299 * r + 0.587 * g + 0.114 * b
    Cr = 0.5 * r - 0.419 * g - 0.081 * b
    Cb = -0.169 * r - 0.331 * g + 0.5 * b
    Mr = np.mean(Cr)
    Mb = np.mean(Cb)
    Dr = np.var(Cr)
    Db = np.var(Cb)
    temp_arry = (np.abs(Cb - (Mb + Db * np.sign(Mb))) < 1.5 * Db) & (
                np.abs(Cr - (1.5 * Mr + Dr * np.sign(Mr))) < 1.5 * Dr)
    RL = Y * temp_arry
    # 选取候选白点数的最亮10%确定为最终白点,并选择其前10%中的最小亮度值
    L_list = list(np.reshape(RL, (RL.shape[0] * RL.shape[1],)).astype(np.int))
    hist_list = np.zeros(256)
    min_val = 0
    sum = 0
    for val in L_list:
        hist_list[val] += 1
    for l_val in range(255, 0, -1):
        sum += hist_list[l_val]
        if sum >= len(L_list) * 0.1:
            min_val = l_val
            break
    # 取最亮的前10%为最终的白点
    white_index = RL < min_val
    RL[white_index] = 0
    # 计算选取为白点的每个通道的增益
    b[white_index] = 0
    g[white_index] = 0
    r[white_index] = 0
    Y_max = np.max(RL)
    b_gain = Y_max / (np.sum(b) / np.sum(b > 0))
    g_gain = Y_max / (np.sum(g) / np.sum(g > 0))
    r_gain = Y_max / (np.sum(r) / np.sum(r > 0))
    b, g, r = cv2.split(img)
    b = b * b_gain
    g = g * g_gain
    r = r * r_gain
    # 溢出处理
    b[b > 255] = 255
    g[g > 255] = 255
    r[r > 255] = 255
    res_img = cv2.merge((b, g, r))
    return res_img

def bright_Img(path,ga,flag):
    """
    亮度增强 Tensorlayer
    :param ga: ga为gamma值,>1亮度变暗,<1亮度变亮
    :param flag:True: 亮度值为(1-ga,1+ga)
                False:亮度值为ga,默认为1
    :return: 亮度增强后的图像
    """
    image = tl.vis.read_image(path)
    tenl_img = tl.prepro.brightness(image, gamma=ga, is_random=flag)
    return tenl_img

def illumination_Img(path,ga,co,sa,flag):
    """
    亮度,饱和度,对比度增强 Tensorlayer
    :param ga: ga为gamma值,>1亮度变暗,<1亮度变亮
    :param co: 对比度值,1为原始值
    :param sa: 饱和度值,1为原始值
    :param flag:True: 亮度值为(1-ga,1+ga),对比度(1-co,1+co),饱和度(1-sa,1+sa)
                False:亮度值为ga,对比度co,饱和度sa
    :return:增强后的结果
    """
    image = tl.vis.read_image(path)
    tenl_img= tl.prepro.illumination(image, gamma=ga, contrast=co , saturation=sa, is_random=flag)
    return tenl_img

def create_mask(imgpath):
    image = cv2.imread(imgpath, cv2.IMREAD_GRAYSCALE)
    _, mask = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)
    return mask
def xiufu_Img(imgpath,maskpath):
    """
    去除图像上的高光部分
    """
    src_ = cv2.imread(imgpath)
    mask = cv2.imread(maskpath, cv2.IMREAD_GRAYSCALE)
    #缩放因子(fx,fy)
    res_ = cv2.resize(src_,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
    mask = cv2.resize(mask,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC)
    dst = cv2.inpaint(res_, mask, 10, cv2.INPAINT_TELEA)
    return dst

def image_brightness(rgb_image):
    '''
    检测图像亮度(基于RMS)
    '''
    stat = ImageStat.Stat(rgb_image)
    r, g, b = stat.rms
    return math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
def calc_gamma(brightness):
    return brightness/127.0
def image_gamma_transform(pil_im, gamma):
    image_arr = np.array(pil_im)
    image_arr2 = exposure.adjust_gamma(image_arr, gamma)
    if len(image_arr.shape) == 3:  # 格式为(height(rows), weight(colums), 3)
        r = Image.fromarray(np.uint8(image_arr[:, :, 0]))
        g = Image.fromarray(np.uint8(image_arr[:, :, 1]))
        b = Image.fromarray(np.uint8(image_arr[:, :, 2]))
        image = Image.merge("RGB", (r, g, b))
        return image
    elif len(image_arr.shape) == 2:  
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值