当前位置:网站首页>深度学习训练样本扩增同时修改标签名称
深度学习训练样本扩增同时修改标签名称
2022-07-01 08:16:00 【天人合一peng】
之前的是在不同文件夹,自己手动修改
# -*- coding:utf-8 -*-
import sys
# sys.path.append('E:\\Anaconda\\libs')
import os # os:操作系统相关的信息模块
import shutil
from os.path import join
import cv2
import numpy as np
import random # 导入随机函数
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# 存放原始图片地址
data_base_dir = '../switch/images/train2017'
label_path = '../switch/labels/train2017'
file_dir = '../switch/images/train2017'
dest_dir = "./VINS"
# dest_dir1 = r"E:\VINS\train01"
# dest_dir2 = r"E:\VINS\train02"
# dest_dir3 = r"E:\VINS\train03"
# dest_dir4 = r"E:\VINS\train04"
# dest_dir5 = r"E:\VINS\train05"
# dest_dir6 = r"E:\VINS\train06"
# dest_dir7 = r"E:\VINS\train07"
# dest_dir8 = r"E:\VINS\train08"
# dest_dir9 = r"E:\VINS\train09"
# dest_dir10 = r"E:\VINS\train10"
# dest_dir11 = r"E:\VINS\train11"
# dest_dir12 = r"E:\VINS\train12"
if not os.path.exists(dest_dir): #判断所在目录下是否有该文件名的文件夹
os.mkdir(dest_dir)
file_list = [] # 建立列表,用于保存图片信息
# 读取图片文件,并将图片地址、图片名和标签写到txt文件中
def imgBrightness(img1, c, b):
rows, cols, channels = img1.shape
blank = np.zeros([rows, cols, channels], img1.dtype)
rst = cv2.addWeighted(img1, c, blank, 1 - c, b)
return rst
def noise(img, snr):
h = img.shape[0]
w = img.shape[1]
img1 = img.copy()
sp = h * w # 计算图像像素点个数
NP = int(sp * (1 - snr)) # 计算图像椒盐噪声点个数
for i in range(NP):
randx = np.random.randint(1, h - 1) # 生成一个 1 至 h-1 之间的随机整数
randy = np.random.randint(1, w - 1) # 生成一个 1 至 w-1 之间的随机整数
if np.random.random() <= 0.5: # np.random.random()生成一个 0 至 1 之间的浮点数
img1[randx, randy] = 0
else:
img1[randx, randy] = 255
return img1
for file in os.listdir(data_base_dir): # file为current_dir当前目录下图片名
if file.endswith(".jpg"): # 如果file以jpg结尾
for item in range(13):
# print(file)
imageName = "VIN" + str(item) + file[:-4] + '.jpg'
labelName = "VIN" + str(item) + file[:-4] + '.txt'
if item == 0:
# 原图不复制了
scr_dir = join(file_dir, file)
shutil.copyfile(scr_dir, os.path.join(dest_dir, imageName))
# shutil.copyfile(scr_dir, det)
# 增强图片
if item == 1:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
gaussianBlur = cv2.GaussianBlur(img, (3, 3), 3)
blur = cv2.blur(gaussianBlur, (5, 5)) # 模糊图片
# cv2.imshow("img1", blur)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, blur)
if item == 2:
img = cv2.imread(scr_dir)
boxblur = cv2.boxFilter(img, -1, (9, 9)) # 模糊图片*2
# cv2.imshow("img2", boxblur)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur)
# 加椒盐噪声小
if item == 3:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
# cv2.imshow("img3", noiseimage)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage)
# # 加椒盐噪声中
# if item == 4:
# img = cv2.imread(scr_dir)
# noiseimage4 = noise(img, 0.90) # 将信噪比设定为0.6
# # cv2.imshow("img4", noiseimage4)
# # cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage4)
#
# # 加椒盐噪声多
# if item == 5:
# img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
# noiseimage5 = noise(img, 0.86) # 将信噪比设定为0.6
# cv2.imshow("img5", noiseimage5)
# cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage5)
# 加椒盐噪声加高斯模糊
if item == 6:
img = cv2.imread(scr_dir)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
gaussianBlur6 = cv2.GaussianBlur(noiseimage, (3, 3), 3)
# cv2.imshow("img3", gaussianBlur6)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, gaussianBlur6)
# 加椒盐噪声加高斯模糊
if item == 7:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
boxblur7 = cv2.boxFilter(noiseimage, -1, (9, 9))
# cv2.imshow("img7", boxblur7)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur7)
# 高斯模糊加椒盐噪声
if item == 8:
img = cv2.imread(scr_dir)
gaussianBlur = cv2.GaussianBlur(img, (5, 5), 3)
noiseimage8 = noise(gaussianBlur, 0.98) # 将信噪比设定为0.6
# cv2.imshow("img8", noiseimage8)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage8)
# # 双边模糊加椒盐噪声
# if item == 9:
# img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
#
# bilBlur = cv2.bilateralFilter(img, 1, 25, 10)
# noiseimage9 = noise(bilBlur, 0.92) # 将信噪比设定为0.6
# cv2.imshow("img9", noiseimage9)
# cv2.waitKey()
# det = join(dest_dir, imageName)
# cv2.imwrite(det, noiseimage9)
# 调亮
if item == 10:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.6, 1)
# cv2.imshow("img10", rst)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, rst)
# 调亮并模糊
if item == 11:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.4, 4)
boxblur11 = cv2.boxFilter(rst, -1, (3, 3))
# cv2.imshow("img11", boxblur11)
# cv2.waitKey()
det = join(dest_dir, imageName)
cv2.imwrite(det, boxblur11)
# 调亮加椒盐噪声
if item == 12:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.28, 4)
noiseimage12 = noise(rst, 0.98) # 将信噪比设定为0.6
cv2.imshow("img12", noiseimage12)
cv2.waitKey(3)
det = join(dest_dir, imageName)
cv2.imwrite(det, noiseimage12)
shutil.copyfile(os.path.join(label_path, file[:-4] + '.txt'), os.path.join(dest_dir, labelName))
print("all down")
边栏推荐
- 【入门】提取不重复的整数
- [no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
- Redis publish subscription
- 01 numpy introduction
- 【力扣10天SQL入门】Day9 控制流
- [untitled]
- Keithley 2100 software 𞓜 Keithley2400 test software ns SourceMeter
- Book of quantitative trading - reading notes of the man who conquers the market
- Differential: definition of total differential, partial derivative, gradient
- [dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
猜你喜欢
基于Gazebo的无人机管道检测
seaborn clustermap矩阵添加颜色块
[getting started] input n integers and output the smallest K of them
[untitled]
Serial port oscilloscope software ns-scope
C basic knowledge review (Part 4 of 4)
Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
Connect timed out of database connection
Use threejs simple Web3D effect
[getting started] extract non repeating integers
随机推荐
Burpsuite -- brute force cracking of intruder
P4 installation bmv2 detailed tutorial
Utiliser Beef pour détourner le navigateur utilisateur
Connect timed out of database connection
OJ input and output exercise
01 numpy introduction
[getting started] extract non repeating integers
【刷题】字符统计【0】
【入门】提取不重复的整数
Instead of houses, another kind of capital in China is rising
网关gateway-88
Leetcode t31: next spread
Gateway-88
DID的使用指南,原理
golang中的正则表达式使用注意事项与技巧
XX攻击——反射型 XSS 攻击劫持用户浏览器
Hijacking a user's browser with beef
Huawei machine test questions column subscription Guide
Provincial election + noi part I dynamic planning DP
【入门】取近似值