-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreprocessing.py
More file actions
66 lines (52 loc) · 1.91 KB
/
Copy pathpreprocessing.py
File metadata and controls
66 lines (52 loc) · 1.91 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
import cv2
import numpy as np
from skimage import transform
import math
import os
from data_augmentation import * # Import all augmentation methods
from matplotlib import pyplot as plt
folder = '../MRI_T2_COR_2019_02_20'
savefolder = 'augmented'
try:
os.mkdir(savefolder)
except:
pass
def import_file(folder):
sub_folders = sorted(os.listdir(folder)) # 1, 2, ..., 2300
for sub_folder in sub_folders:
sub_folder_path = folder + '/' + sub_folder
savefolder2 = savefolder + '/' + sub_folder
if not os.path.exists(savefolder2):
os.mkdir(savefolder2)
image_names = sorted(os.listdir(sub_folder_path))
# 'MRI_T2_COR_2019_02_20/1/1_0096.png'
try:
for image_name in image_names:
img = cv2.imread(sub_folder_path + '/' + image_name)
img = cv2.resize(img, (256, 256))
print(image_name)
# Randomly apply augmentation methods
if np.random.rand() < 0.5:
img = flip(img, 0)
if np.random.rand() < 0.5:
img = flip(img, 1)
if np.random.rand() < 0.5:
img = flip(img, 1)
if np.random.rand() < 0.5:
img = gaussian_noise(img)
if np.random.rand() < 0.5:
img = jittering(img)
if np.random.rand() < 0.5:
img = scaling(img)
if np.random.rand() < 0.5:
img = gamma_correction(img)
if np.random.rand() < 0.5:
img = gaussian_blur(img)
if np.random.rand() < 0.5:
img = rotations(img)
if np.random.rand() < 0.5:
img = shear(img)
cv2.imwrite(os.path.join(savefolder2, image_name), img)
except:
pass
print(import_file(folder))