-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_mask.py
144 lines (112 loc) · 5.82 KB
/
gen_mask.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import xml.dom.minidom
import numpy as np
import os
from PIL import Image, ImageDraw
from shutil import copy2
def checkXML(filelist):
status = None
for f in filelist:
if f.split('.')[-1] == 'xml':
status = f
return status
def GenMask_inclusion(src_folder,xmlfile,save_folder):
# categorys = {'Crystal':0, 'Cloud':1, 'Pinpoint':2, 'Feather':3, 'Twinning_wisp':4, 'Needle':5}
# corresponding color for types of inclusions
colors = {'Pinpoint':(255,0,0), 'Crystal':(0,255,0), 'Needle':(0,0,255),
'Cloud':(255,0,0), 'Twinning_wisp':(0,255,0), 'Feather':(0,0,255),
'Internal_graining':(255,0,0)}
mask1 = ['Pinpoint', 'Crystal', 'Needle']
mask2 = ['Cloud', 'Twinning_wisp', 'Feather']
mask3 = ['Internal_graining']
dom = xml.dom.minidom.parse(os.path.join(src_folder,xmlfile))
xml_root = dom.documentElement
images = xml_root.getElementsByTagName('image')
for ii in range(len(images)):
t = images[ii]
t_width = int(t.getAttribute("width"))
t_height = int(t.getAttribute("height"))
t_name = t.getAttribute("name").split('/')[-1]
img_mask1 = Image.new('RGB', (int(t_width), int(t_height)))
draw1 = ImageDraw.Draw(img_mask1)
img_mask2 = Image.new('RGB', (int(t_width), int(t_height)))
draw2 = ImageDraw.Draw(img_mask2)
img_mask3 = Image.new('RGB', (int(t_width), int(t_height)))
draw3 = ImageDraw.Draw(img_mask3)
t_poly = t.getElementsByTagName("polygon") + t.getElementsByTagName("polyline")
for t_ply in t_poly:
t_ply_points = t_ply.getAttribute("points")
t_ply_points = t_ply_points.split(";")
t_ply_points = np.asarray([[int(float(b)) for b in a.split(",")] for a in t_ply_points])
t_x = t_ply_points[:,0]
t_y = t_ply_points[:,1]
t_xy = [(x,y) for x,y in zip(t_x,t_y)]
t_ply_label = t_ply.getAttribute("label").split('_')[0]
if t_ply_label in mask1:
draw1.polygon(t_xy, fill=colors[t_ply_label])
elif t_ply_label in mask2:
draw2.polygon(t_xy, fill=colors[t_ply_label])
elif t_ply_label in mask3:
draw3.polygon(t_xy, fill=colors[t_ply_label])
img_mask1.save(os.path.join(save_folder, t_name[:11]+'_mask1.png'))
img_mask2.save(os.path.join(save_folder, t_name[:11]+'_mask2.png'))
img_mask3.save(os.path.join(save_folder, t_name[:11]+'_mask3.png'))
# save original image
try:
img_name = [n for n in os.listdir(src_folder) if t_name[:11] in n and 'png' in n][0]
copy2(os.path.join(src_folder,img_name),os.path.join(save_folder,t_name[:11]+'.png'))
except:
img_name = [n for n in os.listdir(os.path.join(src_folder,'Twinning wisp')) if t_name[:11] in n and 'png' in n][0]
copy2(os.path.join(src_folder,img_name),os.path.join(save_folder,t_name[:11]+'.png'))
print(t_name[:11] + ' saved ...')
def GenMask_inclusion_reflection(src_folder,xmlfile,mask_save_folder, img_save_folder, is_index=False,fontsize=5):
# corresponding color for inclusion and reflection
# inclusion = (255,0,0), reflection = (0,255,0)
dom = xml.dom.minidom.parse(xmlfile)
xml_root = dom.documentElement
images = xml_root.getElementsByTagName('image')
for ii in range(len(images)):
t = images[ii]
t_width = int(t.getAttribute("width"))
t_height = int(t.getAttribute("height"))
t_name = t.getAttribute("name").split('/')[-1]
img_mask = Image.new('RGB', (int(t_width), int(t_height)))
draw = ImageDraw.Draw(img_mask)
t_poly = t.getElementsByTagName("polygon") + t.getElementsByTagName("polyline")
for t_ply in t_poly:
t_ply_points = t_ply.getAttribute("points")
t_ply_points = t_ply_points.split(";")
t_ply_points = np.asarray([[int(float(b)) for b in a.split(",")] for a in t_ply_points])
t_x = t_ply_points[:,0]
t_y = t_ply_points[:,1]
t_xy = [(x,y) for x,y in zip(t_x,t_y)]
t_ply_label = t_ply.getAttribute("label")
if 'Internal_graining' not in t_ply_label:
if not 'Reflection' in t_ply_label:
draw.polygon(t_xy, fill=(0,255,0))
else:
draw.polygon(t_xy, fill=(255,0,0))
img_mask.save(os.path.join(mask_save_folder, t_name[:11]+'_mask.png'))
# save original image
try:
img_name = [n for n in os.listdir(src_folder) if t_name[:11] in n and 'png' in n][0]
copy2(os.path.join(src_folder,img_name),os.path.join(img_save_folder,t_name[:11]+'.png'))
except:
img_name = [n for n in os.listdir(os.path.join(src_folder,'Twinning wisp')) if t_name[:11] in n and 'png' in n][0]
copy2(os.path.join(src_folder,'Twinning wisp',img_name),os.path.join(img_save_folder,t_name[:11]+'.png'))
print(t_name[:11] + ' saved ...')
def main():
src_folder = r'../data_input'
save_folder = r'../data_output/1_Masks'
img_save_folder = r'../data_output/1_Images'
os.makedirs(save_folder, exist_ok=True)
os.makedirs(img_save_folder, exist_ok=True)
for root,dirs,files in os.walk(src_folder):
status = checkXML(files)
if status is not None:
print(root)
xmlfile = os.path.join(root,status)
print(xmlfile)
# R=reflection, G=inclusion
GenMask_inclusion_reflection(root,xmlfile,save_folder,img_save_folder,is_index=False,fontsize=5)
if __name__ == "__main__":
main()