图片数据集批量数据增强

图片数据集批量数据增强(九种图像处理方法,增加到10倍)

数据增强代码

ck+数据集直接用于表情识别数量较少,去掉contempt大概在900张左右(个人处理,不同处理方法会有数量差异),近期查阅相关博客,参照其他图像处理方法和文件批量处理代码,基于Python语言整合了一个批量处理图片的数据增强代码。图像处理方法包括:增加亮度,降低亮度,色度改变,对比对改变,锐度改变,拉伸图片(y方向),拉伸图片(x方向),左平移,右平移,处理后的图片尺寸不变,9种方法加上原图,扩容到10倍。图像处理还有别的方法,如旋转、更多方向的平移等,可根据个人需求更改。

参考

链接: https://blog.csdn.net/hfutdog/article/details/82351549.
链接: https://blog.csdn.net/weixin_41803874/article/details/81201699.
链接: https://blog.csdn.net/Jeremy_lf/article/details/103738213.

批量数据增强代码

直接上代码,有可以改进的地方欢迎留言交流。

# coding = utf-8
import cv2
from PIL import Image
from PIL import ImageEnhance
from numpy.ma import array
import numpy as np
import os
# 批量处理代码
rootdir = 'D://data//data enhancement//surprise//surprise_ini//' # 指明被遍历的文件夹

def operate(currentPath, filename, targetPath):
    # 读取图像
    image = Image.open(currentPath)
    image_cv = cv2.imread(currentPath)
    # image.show()
    # 增强亮度 bh_
    enh_bri = ImageEnhance.Brightness(image)
    brightness = 1.07
    image_brightened_h = enh_bri.enhance(brightness)
    # image_brightened_h.show()
    image_brightened_h.save(targetPath + 'bh_//images//bh_' + filename)  # 保存

    # 降低亮度 bl_
    enh_bri_low = ImageEnhance.Brightness(image)
    brightness = 0.87
    image_brightened_low = enh_bri_low.enhance(brightness)
    # image_brightened_low.show()
    image_brightened_low.save(targetPath + 'bl_//images//bl_' + filename)

    # 改变色度 co_
    enh_col = ImageEnhance.Color(image)
    color = 0.8
    image_colored = enh_col.enhance(color)
    # image_colored.show()
    image_colored.save(targetPath + 'co_//images//co_' + filename)

    # 改变对比度 cont_
    enh_con = ImageEnhance.Contrast(image)
    contrast = 0.8
    image_contrasted = enh_con.enhance(contrast)
    # image_contrasted.show()
    image_contrasted.save(targetPath + 'cont_//images//cont_' + filename)

    # 改变锐度 sha_
    enh_sha = ImageEnhance.Sharpness(image)
    sharpness = 3.0
    image_sharp = enh_sha.enhance(sharpness)
    # image_sharp.show()
    image_sharp.save(targetPath + 'sha_//images//sha_' + filename)

    # y方向上的缩放 yre_
    # image.show()
    w = image.width
    h = image.height
    print(w, h)
    out_ww = image.resize((w, h + 40))  # 拉伸成高为h的正方形
    # out_ww.show()
    out_ww_1 = np.array(out_ww)
    out_w_2 = out_ww_1[30:(h - 10), 0:w]  # 开始的纵坐标,开始的横坐标
    out_w_2 = Image.fromarray(out_w_2)
    # out_w_2.show()
    out_w_2.save(targetPath + 'yre_//images//yre_' + filename)

    # x方向上的缩放 xre_
    # image.show()
    out_hh = image.resize((w + 80, h))  # 拉伸成宽为w的正方形,width,height
    # out_hh.show()
    out_hh_1 = array(out_hh)
    out_h_2 = out_hh_1[0:h, 40:(w + 40)]
    out_h_2 = Image.fromarray(out_h_2)
    # out_h_2.show()
    out_h_2.save(targetPath + 'xre_//images//xre_' + filename)

    # x左方向的平移 xl_
    # 平移矩阵[[1,0,-10][0,1,-12]]
    # image.show()
    w = image.width
    h = image.height
    M = np.array([[1, 0, -80], [0, 1, 0]], dtype=np.float32)
    image_cv_change = cv2.warpAffine(image_cv, M, (w, h))
    image_cv_change_RGB = cv2.cvtColor(image_cv_change, cv2.COLOR_BGR2RGB)
    image_cv_change = Image.fromarray(image_cv_change_RGB)
    # image_cv_change.show()
    image_cv_change.save(targetPath + 'xl_//images//xl_' + filename)

    # x右方向的平移 xr_
    # image.show()
    w = image.width
    h = image.height
    M = np.array([[1, 0, 80], [0, 1, 0]], dtype=np.float32)
    image_cv_change = cv2.warpAffine(image_cv, M, (w, h))
    image_cv_change_RGB = cv2.cvtColor(image_cv_change, cv2.COLOR_BGR2RGB)
    image_cv_change = Image.fromarray(image_cv_change_RGB)
    # image_cv_change.show()
    image_cv_change.save(targetPath + 'xr_//images//xr_' + filename)


for parent, dirnames, filenames in os.walk(rootdir):
    for filename in filenames:
        # print('parent is:' + parent)
        print('filename is: ' + filename)
        # 把文件名添加到一起后输出
        currentPath = os.path.join(parent, filename)
        # print('the full name of file is :' + currentPath)
        # 保存处理后的图片的目标文件夹
        targetPath = 'D://data//data enhancement//surprise//'
        # 进行处理
        operate(currentPath, filename, targetPath)

代码文件说明

  • rootdir:被处理图片所在文件夹,不包括文件名。
  • targetPath:处理后图片保存路径,不包括文件名
  • image_brightened_h.save(targetPath + ‘bh_//images//bh_’ + filename) # 处理后文件夹名称+分类后的文件夹名称+文件名
  • 注意:所有文件夹必须在代码运行前建好(能力有限,还没有做传建文件夹功能),欢迎留言交流。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值