-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessing.py
More file actions
77 lines (62 loc) · 1.93 KB
/
Preprocessing.py
File metadata and controls
77 lines (62 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import cv2
import os, os.path
import random
import math
import numpy as np
import numpy as py
def augment_brightness_camera_images(image):
image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)
random_bright = 1+np.random.uniform()
#print(random_bright)
image1[:,:,2] = image1[:,:,2]*random_bright
image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB)
return image1
def randMute(img, t, fpath):
image = cv2.imread(img)
ang_rot = np.random.uniform(40)-40/2
rows,cols,ch = image.shape
Rot_M = cv2.getRotationMatrix2D((cols/2,rows/2),ang_rot,1)
image = cv2.warpAffine(image,Rot_M,(cols,rows))
tr_x = 10*np.random.uniform()-10/2
tr_y = 10*np.random.uniform()-10/2
Trans_M = np.float32([[1,0,tr_x],[0,1,tr_y]])
image = cv2.warpAffine(image, Trans_M, (cols, rows))
pts1 = np.float32([[5,5],[20,5],[5,20]])
pt1 = 5+5*np.random.uniform()-5/2
pt2 = 20+5*np.random.uniform()-5/2
pts2 = np.float32([[pt1,5],[pt2,pt1],[5,pt2]])
shear_M = cv2.getAffineTransform(pts1,pts2)
image = cv2.warpAffine(image,shear_M,(cols,rows))
image = augment_brightness_camera_images(image)
fpath+="/mut"
fpath+=str(t)
fpath+=".jpg"
#print(fpath)
cv2.imwrite(fpath, image)
return image
random.seed(a=None, version=2)
saving_dir= 'runes/saving/'
save = []
mut = []
images = []
j, k = 0,0
types = ('*.bmp', '*.BMP', '*.tiff', '*.TIFF', '*.tif', '*.TIF', '*.jpg', '*.JPG', '*.JPEG', '*.jpeg') # all should work but only .jpg was tested
for t in os.listdir(saving_dir):
print(os.listdir(saving_dir))
sav_dir = 'runes/saving/'
sav_dir+=t
mut_dir = 'runes/mutated/'
mut_dir+=t
save.append(sav_dir)
mut.append(mut_dir)
print(save)
for i in range (0,len(mut)):
for t in os.listdir(save[i]):
j += 1
saving_dir = save[i]
saving_dir+='/'
saving_dir+=t
images.append(saving_dir)
for m in range(0,14):
k += 1
randMute(images[j-1], k, mut[i])