import os
import numpy as np
import cv2
from PIL import Image
import random
import multiprocessing
import time
import threading
from threading import Thread
from time import ctime
from time import sleep
#####################################数据增强操作应用程序##################################
# class MyThread(Thread):
# # func:该参数是多线程执行的函数
# # args:func函数的参数列表,多个参数的列表
# # name:名称
# def __init__(self, func, args, name):
# # MyThread子类的构造函数必须先调用其基类的构造函数
# Thread.__init__(self)
# self.func = func
# self.args = args
# self.name = name
#
# # 必须在子类中定义run()方法
# def run(self):
# self.func(*self.args) #将参数传入到主程序中
mLock = threading.Lock() #创建锁
im_path = '/mnt/renpeng/textImage'
im_path3 = '/mnt/renpeng/textLabelH'
with open(os.path.join(im_path3, 'textLabel.txt'), 'r') as f:
file_names = f.readlines() #读取所有的图片和标签信息(这是读入的全局变量)
def myTest():
#使用多线程对list对象进行操作
#start = time.time()
global file_names
newStr_list = []
while True:
#for im in file_names:
if len(file_names) is 0:
break
mLock.acquire() # 加锁
im = file_names[0] # 每次取第一张图
file_names.remove(im)
mLock.release() # 释放锁
img_str = im.split(' ')[0]
img_strH = img_str[0:-4]+'H'+'.jpg'
str2 = img_strH + ' ' + im.split(' ')[1]
newStr_list.append(str2)
# print(img_str)
# print(img_strH)
img = cv2.imread(os.path.join(im_path, img_str))
h, w, c = img.shape
#### 1 随机旋转图像
degree = random.uniform(-1, 1)
degree = round(degree, 1)
#print(degree)
M = cv2.getRotationMatrix2D((w / 2, h / 2), degree, 1)
resImg = cv2.warpAffine(img, M, (w, h))
#### 2 随机平移图像(for循环转矩阵运算)
# offsetH = int(rand(0, int(h*0.1)))
# offsetW = int(rand(0, int(w*0.02)))
# dst = np.zeros((h,w,c), np.uint8)
# for i in range(h - offsetH):
# for j in range(w - offsetW):
# if random.random() > 0.5:
# dst[i + offsetH, j + offsetW] = resImg[i, j]
# else:
# dst[i, j] = resImg[i, j]
offsetHM = int(h * 0.1)
offsetWM = int(w * 0.02)
offsetH = random.randint(-offsetHM, offsetHM)
offsetW = random.randint(-offsetWM, offsetWM)
M2 = np.float32([[1, 0, offsetW], [0, 1, offsetH]])
resImg2 = cv2.warpAffine(resImg, M2, (w, h))
#### 4 随机添加类似的椒盐噪声
numNoise = random.randint(0,30) #随机添加0-50个噪声
for i in range(numNoise):
h0 = random.randint(0,h-1) #闭区间原则
w0 = random.randint(0,w-1)
rngNoise = random.randint(-5,5)
resImg2[h0, w0, 0] = 223 + rngNoise #(223 + rngNoise, 151, 133)
resImg2[h0, w0, 1] = 151
resImg2[h0, w0, 2] = 133
#img2 = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
cv2.imwrite(os.path.join(im_path, img_strH), resImg2)
#start = time.time()
with open(os.path.join(im_path3, 'textLabel.txt'), 'a') as o:
#start = time.time()
o.writelines(newStr_list)
print('耗时:', str(time.time() - start)) #四个线程就有四个输出
def main():
t1 = threading.Thread(target=myTest)
t2 = threading.Thread(target=myTest)
t3 = threading.Thread(target=myTest)
t4 = threading.Thread(target=myTest)
t5 = threading.Thread(target=myTest)
t6 = threading.Thread(target=myTest)
t7 = threading.Thread(target=myTest)
t8 = threading.Thread(target=myTest)
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
t6.start()
t7.start()
t8.start()
if __name__ == '__main__':
start = time.time()
main()
# print('耗时:', str(time.time() - start))
#encoding='utf-8'
import os
import numpy as np
import cv2
from PIL import Image
import random
import multiprocessing
import time
import threading
from threading import Thread
import math
import shutil
mLock = threading.Lock() #创建锁
im_path = './images/val'
img_names = os.listdir(im_path)
im_path2 = './images/val_aug'
#im_path3 = '/mnt/renpeng/textLabelH'
#with open(os.path.join(im_path2, 'splitLabelTruth.txt'), 'r') as f:
# file_names = f.readlines() #读取所有的图片和标签信息(这是读入的全局变量)
file_path = './labels/val'
# file_names = os.listdir(file_path)
file_path2 = './labels/val_aug'
def dumpNoiseImage(img_str, img, noise_ratio = 0.00001, img_strH_ = None):
img_Noise = img.copy()
src_txt = img_str[0:-4] + '.txt' #获取原始文件路径位置
if img_strH_ == None:
dst_txt = img_str[0:-4] +'_JY.txt'
img_strH = img_str[0:-4]+'_JY.jpg'
else:
dst_txt = img_strH_[0:-4] +'_JY.txt'
img_strH = img_strH_[0:-4]+'_JY.jpg'
H, W, C = img.shape
for i in range(int(noise_ratio*H*W)):
x = np.random.randint(H)
y = np.random.randint(W)
img_Noise[x, y, :] = 255
cv2.imwrite(os.path.join(im_path2,img_strH), img_Noise)
shutil.copy(os.path.join(file_path, src_txt), os.path.join(file_path2, dst_txt))
return img_Noise, img_strH
def dumpGsBlurImage(img_str, img, img_strH_ = None):
src_txt = img_str[0:-4] + '.txt' #获取原始文件路径位置
if img_strH_ == None:
dst_txt = img_str[0:-4] +'_GB.txt'
img_strH = img_str[0:-4]+'_GB.jpg'
else:
dst_txt = img_strH_[0:-4] +'_GB.txt'
img_strH = img_strH_[0:-4]+'_GB.jpg'
ksize = random.choice((3,5,7))
img_GaussianBlur = cv2.GaussianBlur(img, (ksize, ksize), 0)
cv2.imwrite(os.path.join(im_path2,img_strH), img_GaussianBlur)
shutil.copy(os.path.join(file_path, src_txt), os.path.join(file_path2, dst_txt))
return img_GaussianBlur, img_strH
def dumpBlurImage(img_str, img, img_strH_=None):
src_txt = img_str[0:-4] + '.txt' #获取原始文件路径位置
if img_strH_ == None:
dst_txt = img_str[0:-4] +'_B.txt'
img_strH = img_str[0:-4]+'_B.jpg'
else:
dst_txt = img_strH_[0:-4] +'_B.txt'
img_strH = img_strH_[0:-4]+'_B.jpg'
ksize = random.choice((3,5,7))
img_Mean = cv2.blur(img, (ksize, ksize))
cv2.imwrite(os.path.join(im_path2,img_strH), img_Mean)
shutil.copy(os.path.join(file_path, src_txt), os.path.join(file_path2, dst_txt))
return img_Mean, img_strH
def dumpGammaAdjust(img_str, img, img_strH_=None):
src_txt = img_str[0:-4] + '.txt'
if img_strH_ == None:
# print('*' * 10)
dst_txt = img_str[0:-4] +'_GM.txt'
img_strH = img_str[0:-4]+'_GM.jpg'
else:
dst_txt = img_strH_[0:-4] +'_GM.txt'
img_strH = img_strH_[0:-4]+'_GM.jpg'
#img_strH = os.path.join(im_path2, img_str[0:-4]+'_GM.jpg')
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
channels = cv2.split(ycrcb)
mean = np.mean(channels[0])
gamma_val = math.log10(0.5)/math.log10(mean/255) # 公式计算gamma
# channels[0] = gamma_trans(channels[0], gamma_val) # gamma变换
gamma_table = [np.power(x / 255.0, gamma_val) * 255.0 for x in range(256)] # 建立映射表
gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
channels[0] = cv2.LUT(channels[0], gamma_table)
#cv2.equalizeHist(channels[0], channels[0])
cv2.merge(channels, ycrcb)
img_Gamma = cv2.cvtColor(ycrcb, cv2.COLOR_YCR_CB2BGR)
cv2.imwrite(os.path.join(im_path2,img_strH), img_Gamma)
shutil.copy(os.path.join(file_path, src_txt), os.path.join(file_path2, dst_txt))
return img_Gamma, img_strH
def myTest():
#使用多线程对list对象进行操作
#start = time.time()
global img_names #
# newStr_list = []
while True:
#for im in file_names:
if len(img_names) == 0:
break
mLock.acquire() # 加锁
img_str = img_names[-1] # 每次取第一张图,图片名
img_names.pop()
mLock.release() # 释放锁
img = cv2.imread(os.path.join(im_path, img_str)) #os.path.join(im_path, img_str)
#print('*'*5 , img.shape)
#############每次增强之后进行一次图片保存操作!#########
#### 1 随机旋转图像(按组合往下进行)
#### 1 gamma增强
imgGama, img_strH = dumpGammaAdjust(img_str, img)
dumpBlurImage(img_str, imgGama, img_strH)
dumpGsBlurImage(img_str, imgGama, img_strH)
dumpNoiseImage(img_str, imgGama, 0.0001, img_strH)
# #dumpDoubleImage(img_strH3, newStr_list, imgGama, im)
#### 2 均值滤波
img_Mean, img_strH2 = dumpBlurImage(img_str, img)
dumpGsBlurImage(img_str, img_Mean, img_strH2)
dumpNoiseImage(img_str, img_Mean, 0.0001, img_strH2)
# dumpDoubleImage(img_strH4, newStr_list, img_Mean, im)
#### 3 高斯滤波
img_GaussianBlur, img_strH3 = dumpGsBlurImage(img_str, img)
dumpNoiseImage(img_str, img_GaussianBlur, 0.0001, img_strH3)
#dumpDoubleImage(img_strH5, newStr_list, img_GaussianBlur, im)
#### 4 椒盐噪声
dumpNoiseImage(img_str, img, noise_ratio = 0.0001)
#dumpDoubleImage(img_strH6, newStr_list, img_Double, im)
def main():
# for _ in range(1, 5):
# t = threading.Thread(target=myTest)
# t.start()
t1 = threading.Thread(target=myTest)
t2 = threading.Thread(target=myTest)
t3 = threading.Thread(target=myTest)
t4 = threading.Thread(target=myTest)
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
if __name__ == '__main__':
start = time.time()
main()
print('耗时:', str(time.time() - start))