当前位置:网站首页>深度学习训练样本扩增同时修改标签名称
深度学习训练样本扩增同时修改标签名称
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")边栏推荐
猜你喜欢

【入门】取近似值
![[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B](/img/48/e98d01830867baa742574e1b6e1096.jpg)
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B

Adding color blocks to Seaborn clustermap matrix

CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試

Gdip - hatchbrush pattern table

Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation

网关gateway-88

0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions

Soft keyboard height error
![[getting started] input n integers and output the smallest K of them](/img/b8/20852484f10bc968d529e9c1ff5480.png)
[getting started] input n integers and output the smallest K of them
随机推荐
Differential: definition of total differential, partial derivative, gradient
uni 热更新
程序员养生宝典
Utiliser Beef pour détourner le navigateur utilisateur
Leetcode t34: find the first and last positions of elements in a sorted array
golang中的正则表达式使用注意事项与技巧
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
Adding color blocks to Seaborn clustermap matrix
【入门】输入n个整数,输出其中最小的k个
Gdip - hatchbrush pattern table
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
How to prevent the other party from saying that he has no money after winning the lawsuit?
CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試
empirical study and case study
shardingSphere
【入门】截取字符串
[force deduction 10 days SQL introduction] Day10 control flow
軟鍵盤高度報錯
Luogu p3799 demon dream stick
How can beginners correctly understand Google's official suggested architectural principles (questions?)