图片数据集批量数据增强

编程入门 行业动态 更新时间:2024-10-24 14:21:33

图片<a href=https://www.elefans.com/category/jswz/34/1771445.html style=数据集批量数据增强"/>

图片数据集批量数据增强

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

  • 数据增强代码
  • 参考
  • 批量数据增强代码
  • 代码文件说明

数据增强代码

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

参考

链接: .
链接: .
链接: .

批量数据增强代码

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

# 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.07image_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.87image_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.8image_colored = enh_col.enhance(color)# image_colored.show()image_colored.save(targetPath + 'co_//images//co_' + filename)# 改变对比度 cont_enh_con = ImageEnhance.Contrast(image)contrast = 0.8image_contrasted = enh_con.enhance(contrast)# image_contrasted.show()image_contrasted.save(targetPath + 'cont_//images//cont_' + filename)# 改变锐度 sha_enh_sha = ImageEnhance.Sharpness(image)sharpness = 3.0image_sharp = enh_sha.enhance(sharpness)# image_sharp.show()image_sharp.save(targetPath + 'sha_//images//sha_' + filename)# y方向上的缩放 yre_# image.show()w = image.widthh = image.heightprint(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.widthh = image.heightM = 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.widthh = image.heightM = 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) # 处理后文件夹名称+分类后的文件夹名称+文件名
  • 注意:所有文件夹必须在代码运行前建好(能力有限,还没有做传建文件夹功能),欢迎留言交流。

更多推荐

图片数据集批量数据增强

本文发布于:2024-02-13 04:40:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1690723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   批量   图片

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!