-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData_processing_aug.py
75 lines (59 loc) · 2.9 KB
/
Data_processing_aug.py
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
import os
import base64
import PIL
from PIL import Image
from glob import glob
import pandas as pd
import numpy as np
start_index = 0 #TODO: change accordingly
crop_length = 128 #default crop length
image_dim = 1024
counter = 1
info_file = pd.read_csv(r"./data/Info.txt", sep=' ')
end_index = info_file.shape[0] #TODO: change accordingly
prepro = list()
prepro.append('Number_Case')
for i in range(start_index, end_index):
if(info_file.iloc[i, 2] != 'NORM' and not(np.isnan(info_file.iloc[i, 4]))):
image_name = './Data_original/' + info_file.iloc[i,0] + '.pgm'
image = Image.open(image_name)
x = info_file.iloc[i,4]
y = image_dim - info_file.iloc[i,5]
crop_length = info_file.iloc[i,6] + 50 #crop length according to the radius of tumor
area = (x - crop_length, y - crop_length, x + crop_length, y + crop_length)
image.crop(area).save('./data/img'+'_' + str(counter) +'_' +info_file.iloc[i,3] + '.jpg',"JPEG")
prepro.append('img' + '_' + str(counter) + '_' +info_file.iloc[i,3])
counter = counter + 1
with open('./data/Pre_processed_data.txt', 'w') as f:
for item in prepro:
f.write("%s\n" % item)
info_file_pro = pd.read_csv(r"./data/Pre_processed_data.txt",sep = '_')
end_index = info_file_pro.shape[0]
start_index = 0
saving_path = './data'
aug = list()
aug.append('Number_Case')
for i in range(start_index, end_index):
image_name = './data/img' + '_' + str(info_file_pro.iloc[i,0]) + '_' + info_file_pro.iloc[i,1]
image = Image.open(image_name + '.jpg')
#X for 90 degrees rotation
image.rotate(90).save('./data/img' + '_' + str(info_file_pro.iloc[i,0]) + 'X' + '_'
+ info_file_pro.iloc[i,1]+'.jpg',"JPEG")
aug.append('img' + '_' + str(info_file_pro.iloc[i,0]) + 'X_' + info_file_pro.iloc[i,1])
prepro.append('img' + '_' + str(info_file_pro.iloc[i,0]) + 'X_' + info_file_pro.iloc[i,1])
#Y for 180 degrees rotation
image.rotate(180).save('./data/img' + '_' + str(info_file_pro.iloc[i,0]) + 'Y' + '_'
+ info_file_pro.iloc[i,1]+'.jpg',"JPEG")
aug.append('img' + '_' + str(info_file_pro.iloc[i,0]) + 'Y_' + info_file_pro.iloc[i,1])
prepro.append('img' + '_' + str(info_file_pro.iloc[i,0]) + 'Y_' + info_file_pro.iloc[i,1])
#Z for 270 degrees rotation
image.rotate(270).save('./data/img' + '_' + str(info_file_pro.iloc[i,0]) + 'Z' + '_'
+ info_file_pro.iloc[i,1]+'.jpg',"JPEG")
aug.append('img' + '_' + str(info_file_pro.iloc[i,0]) + 'Z_' + info_file_pro.iloc[i,1])
prepro.append('img' + '_' + str(info_file_pro.iloc[i,0]) + 'Z_' + info_file_pro.iloc[i,1])
with open('./data/Augmentation.txt', 'w') as f:
for item in aug:
f.write("%s\n" % item)
with open('./data/Data_processing_augmentation.txt', 'w') as f:
for item in prepro:
f.write("%s\n" % item)