-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflipAll.py
233 lines (146 loc) · 7.71 KB
/
flipAll.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
import shutil
def isFlipped(path):
img = cv.imread(path, cv.IMREAD_GRAYSCALE)
fig = plt.figure()
plt.imshow(img, cmap = 'gray')
plt.title(path)
plt.draw()
plt.waitforbuttonpress(0)
plt.close(fig)
val = input("F: Flip")
print(val)
if (val == 'f'):
return True
else:
return False
def testslice(array):
plt.imshow(array[:, 100, :], cmap = 'gray')
plt.show()
def flip_ct(array):
flipped = np.rot90(array, k = 2, axes = (2, 0))
return flipped
def flip_xr(array):
flipped = np.rot90(array, k = 2)
return flipped
def move(path, outpath):
img = cv.imread(path, cv.IMREAD_GRAYSCALE)
cv.imwrite(outpath, img)
root = 'D:\Documents\GitHub\GitHub\Synthetic-X-Ray\Matlab_SXR\CXR'
def flip_all_scans(root):
flippedlist = []
normal = []
for path, subdirs, files in os.walk(root):
for name in subdirs:
print(name)
subpath = os.path.join(root ,name)
for spath, ssubdirs, sfiles in os.walk(subpath):
flipped = False
for bname in sfiles:
if (bname == '0.png'):
flipped = isFlipped(os.path.join(subpath, bname))
if (flipped):
flippedlist.append(name)
else:
normal.append(name)
print("flipped: {}".format(len(flippedlist)))
print("normal: {}".format(len(normal)))
for path, subdirs, files in os.walk(root):
for name in subdirs:
print(name)
subpath = os.path.join(root ,name)
for spath, ssubdirs, sfiles in os.walk(subpath):
for sname in sfiles:
if (sname == '0.png'):
finalpath = os.path.join('Data/In/0/', name + '.png')
if name in flippedlist:
flipped = True
else:
flipped = False
if (flipped):
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
flippedlist.append(subpath)
ct = np.load(os.path.join('Data\Out', name + '.npy'))
ct_flipped = flip_ct(ct)
np.save(os.path.join('Data\Out', name), ct_flipped)
else:
normal.append(subpath)
shutil.copy(os.path.join(subpath, sname), finalpath)
elif (sname == '1.png'):
if (flipped):
finalpath = (os.path.join('Data/In/3/', name + '.png'))
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
else:
finalpath = (os.path.join('Data/In/1/', name + '.png'))
shutil.copy(os.path.join(subpath, sname), finalpath)
elif (sname == '2.png'):
finalpath = (os.path.join('Data/In/2/', name + '.png'))
if (flipped):
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
else:
shutil.copy(os.path.join(subpath, sname), finalpath)
elif (sname == '3.png'):
if (flipped):
finalpath = (os.path.join('Data/In/1/', name + '.png'))
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
else:
finalpath = (os.path.join('Data/In/3/', name + '.png'))
shutil.copy(os.path.join(subpath, sname), finalpath)
#flip180(os.path.join(subpath, name, "placeholder"), False)
def flip_individual_scan(root, num_str):
flipped = True
for path, subdirs, files in os.walk(root):
for name in subdirs:
if (name == num_str):
subpath = os.path.join(root ,name)
for spath, ssubdirs, sfiles in os.walk(subpath):
for sname in sfiles:
if (sname == '0.png'):
finalpath = os.path.join('Data/In/0/', name + '.png')
if (flipped):
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
ct = np.load(os.path.join('Data\Out', name + '.npy'))
ct_flipped = flip_ct(ct)
np.save(os.path.join('Data\Out', name), ct_flipped)
else:
shutil.copy(os.path.join(subpath, sname), finalpath)
elif (sname == '1.png'):
if (flipped):
finalpath = (os.path.join('Data/In/3/', name + '.png'))
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
else:
finalpath = (os.path.join('Data/In/1/', name + '.png'))
shutil.copy(os.path.join(subpath, sname), finalpath)
elif (sname == '2.png'):
finalpath = (os.path.join('Data/In/2/', name + '.png'))
if (flipped):
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
else:
shutil.copy(os.path.join(subpath, sname), finalpath)
elif (sname == '3.png'):
if (flipped):
finalpath = (os.path.join('Data/In/1/', name + '.png'))
ff1 = cv.imread(os.path.join(subpath, sname), cv.IMREAD_GRAYSCALE)
ff1 = cv.rotate(ff1, cv.ROTATE_180)
cv.imwrite(finalpath, ff1)
else:
finalpath = (os.path.join('Data/In/3/', name + '.png'))
shutil.copy(os.path.join(subpath, sname), finalpath)
flip_individual_scan(r'D:\Documents\GitHub\GitHub\Synthetic-X-Ray\Matlab_SXR\CXR', '0997')