-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmake_balvan_patches.py
228 lines (201 loc) · 10.3 KB
/
make_balvan_patches.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
# -*- coding: utf-8 -*-
# make Balvan data to patches
import pandas as pd
import skimage.io as skio
import skimage.transform as skt
from tqdm import tqdm
from glob import glob
import os, random, math, cv2
import numpy as np
import matplotlib.pyplot as plt
# %% Helper functions
def split_celllines(fold):
''' Split to train/test sets for a given fold number
'''
celllines = {'DU145', 'PNT1A', 'LNCaP'}
test_celllines_dict = {1:'DU145', 2:'PNT1A', 3:'LNCaP'}
test_celllines = test_celllines_dict[fold]
train_celllines = list(celllines - {test_celllines})
return train_celllines, test_celllines
def tform_centred(radian, translation, center):
# first translation, then rotation
tform1 = skt.SimilarityTransform(translation=center)
tform2 = skt.SimilarityTransform(rotation=radian)
tform3 = skt.SimilarityTransform(translation=-center)
tform4 = skt.SimilarityTransform(translation=translation)
tform = tform4 + tform3 + tform2 + tform1
return tform
def dist_coords(coords1, coords2):
''' Calculate the point-to-point distances between two coordinates, return a list.
'''
return [sum((coords1[i] - coords2[i]) ** 2) ** 0.5 for i in range(len(coords1))]
# =============================================================================
# # %% Cut 1 image to 4 patches (ONLY NEED TO RUN ONCE)
# import image_slicer
#
# def slice1to4(src_dir, tar_dir):
# ''' Slice each image in src_dir to 4 tiles and save to tar_dir
# '''
# # src_dir='./Datasets/Balvan'
# # tar_dir='./Datasets/Balvan_1to4tiles'
#
# modalities = {'GREEN':'A', 'QPI':'B'}
# for modality in os.listdir(src_dir):
# if not os.path.exists(f'{tar_dir}/{modalities[modality]}'):
# os.makedirs(f'{tar_dir}/{modalities[modality]}')
# img_paths = glob(f'{src_dir}/{modality}/*.tif')
# for img_path in tqdm(img_paths):
# tiles = image_slicer.slice(img_path, 4, save=False)
# image_slicer.save_tiles(tiles,
# directory=f'{tar_dir}/{modalities[modality]}',
# prefix=os.path.basename(img_path).split('.')[0],
# format='png')
# return
#
# #slice1to4(src_dir='./Datasets/Balvan', tar_dir='./Datasets/Balvan_1to4tiles')
# =============================================================================
# %%
def make_patches(img_root, target_root, fold=1, t_level=1,
# trans_min=0, trans_max=15, rot_min=0, rot_max=5,
mode='train', display=None):
# img_root='./Datasets/Balvan_1to4tiles'
# target_root='./Datasets/Balvan_patches'
# fold=1
# t_level=2
# trans_min=0
# trans_max=15
# rot_min=0
# rot_max=5
# mode='test'
w=300 # patch width
o=0 # upper-left corner of patch
coords_ref = np.array(([0,0], [0,w], [w,w], [w,0]))
centre_patch = np.array((w, w)) / 2. - 0.5
step_trans = 7
step_rot = 5
trans_min = step_trans * (t_level - 1)
trans_max = step_trans * t_level
rot_min = step_rot * (t_level - 1)
rot_max = step_rot * t_level
#modalities = {'GREEN':'A', 'QPI':'B'}
tardirA = f'{target_root}/fold{fold}/patch_tlevel{t_level}/A/{mode}'
tardirB = f'{target_root}/fold{fold}/patch_tlevel{t_level}/B/{mode}'
if not os.path.exists(tardirA):
os.makedirs(tardirA)
if not os.path.exists(tardirB):
os.makedirs(tardirB)
train_celllines, test_celllines = split_celllines(fold)
if mode=='train':
f_names = [os.path.basename(f_path).split('.')[0] for
cellline in train_celllines for
f_path in glob(f'{img_root}/A/{cellline}_*')]
elif mode=='test':
f_names = [os.path.basename(f_path).split('.')[0] for
f_path in glob(f'{img_root}/A/{test_celllines}_*')]
f_names = list(f_names)
f_names.sort()
# csv information
header = [
'ReferenceImage', 'Method',
'X1_Ref', 'Y1_Ref', 'X2_Ref', 'Y2_Ref', 'X3_Ref', 'Y3_Ref', 'X4_Ref', 'Y4_Ref',
'X1_Trans', 'Y1_Trans', 'X2_Trans', 'Y2_Trans', 'X3_Trans', 'Y3_Trans', 'X4_Trans', 'Y4_Trans',
'X1_Recover', 'Y1_Recover', 'X2_Recover', 'Y2_Recover', 'X3_Recover', 'Y3_Recover', 'X4_Recover', 'Y4_Recover',
'Displacement', 'RelativeDisplacement', 'Tx', 'Ty', 'AngleDegree', 'AngleRad', 'Error', 'DisplacementCategory']
df = pd.DataFrame(index=f_names, columns=header)
df.index.set_names('Filename', inplace=True)
if display is not None:
cnt_disp = 0
for f_name in tqdm(f_names):
# load original images
suffix = os.path.basename(os.listdir(f'{img_root}/A/')[0]).split('.')[-1]
imgA = skio.imread(f"{img_root}/A/{f_name}.{suffix}")
imgB = skio.imread(f"{img_root}/B/{f_name}.{suffix}")
# random transformation parameters
rot_degree = random.choice((random.uniform(-rot_max, -rot_min), random.uniform(rot_min, rot_max)))
tx = random.choice((random.uniform(-trans_max, -trans_min), random.uniform(trans_min, trans_max)))
ty = random.choice((random.uniform(-trans_max, -trans_min), random.uniform(trans_min, trans_max)))
rot_radian = np.deg2rad(rot_degree)
# transform original images
centre_img = np.array((imgA.shape[0], imgA.shape[1])) / 2. - 0.5
tform_img = tform_centred(radian=rot_radian, translation=(tx, ty), center=centre_img)
imgA_trans = np.asarray(skt.warp(imgA, tform_img, mode='reflect', preserve_range=True), dtype=np.uint8)
imgB_trans = np.asarray(skt.warp(imgB, tform_img, mode='reflect', preserve_range=True), dtype=np.uint8)
# crop patches
patchA_ref = imgA[o:o+w, o:o+w]
patchB_ref = imgB[o:o+w, o:o+w]
patchA_trans = imgA_trans[o:o+w, o:o+w]
patchB_trans = imgB_trans[o:o+w, o:o+w]
# transform patch coordinates
tform_patch = tform_centred(radian=rot_radian, translation=(tx, ty), center=centre_patch)
coords_trans = skt.matrix_transform(coords_ref, tform_patch.params)
# calculate distance
dist_array = dist_coords(coords_trans, coords_ref)
dist = np.mean(dist_array)
# write csv line
line = {
'X1_Ref': coords_ref[0][0], 'Y1_Ref': coords_ref[0][1],
'X2_Ref': coords_ref[1][0], 'Y2_Ref': coords_ref[1][1],
'X3_Ref': coords_ref[2][0], 'Y3_Ref': coords_ref[2][1],
'X4_Ref': coords_ref[3][0], 'Y4_Ref': coords_ref[3][1],
'X1_Trans': coords_trans[0][0], 'Y1_Trans': coords_trans[0][1],
'X2_Trans': coords_trans[1][0], 'Y2_Trans': coords_trans[1][1],
'X3_Trans': coords_trans[2][0], 'Y3_Trans': coords_trans[2][1],
'X4_Trans': coords_trans[3][0], 'Y4_Trans': coords_trans[3][1],
'Displacement': dist,
'RelativeDisplacement': dist/w,
'Tx': tx, 'Ty': ty,
'AngleDegree': rot_degree, 'AngleRad': rot_radian}
df.loc[f_name] = line
# save patches
skio.imsave(f'{tardirA}/{f_name}_R.{suffix}', patchA_ref)
skio.imsave(f'{tardirA}/{f_name}_T.{suffix}', patchA_trans)
skio.imsave(f'{tardirB}/{f_name}_R.{suffix}', patchB_ref)
skio.imsave(f'{tardirB}/{f_name}_T.{suffix}', patchB_trans)
# display patch outline in original image
if display is not None and cnt_disp < display:
dispdirA = f'{target_root}/fold{fold}/patch_tlevel{t_level}/display/A/{mode}'
dispdirB = f'{target_root}/fold{fold}/patch_tlevel{t_level}/display/B/{mode}'
if not os.path.exists(dispdirA):
os.makedirs(dispdirA)
if not os.path.exists(dispdirB):
os.makedirs(dispdirB)
if len(imgA.shape) == 2:
imgA_disp = np.pad(imgA, w//2, mode='reflect')
imgA_disp = np.repeat(imgA_disp.reshape(imgA_disp.shape[0], imgA_disp.shape[1], 1), 3, axis=-1)
else:
imgA_disp = np.pad(imgA, ((w//2, w//2), (w//2, w//2), (0, 0)), mode='reflect')
imgA_disp = cv2.polylines(imgA_disp, pts=[(w//2+coords_ref).reshape((-1,1,2))], isClosed=True, color=(0,255,0), thickness=2)
imgA_disp = cv2.polylines(imgA_disp, pts=[np.int32(w//2+coords_trans).reshape((-1,1,2))], isClosed=True, color=(0,0,255), thickness=2)
skio.imsave(f'{dispdirA}/{f_name}_display.{suffix}', imgA_disp)
if len(imgB.shape) == 2:
imgB_disp = np.pad(imgB, w//2, mode='reflect')
imgB_disp = np.repeat(imgB_disp.reshape(imgB_disp.shape[0], imgB_disp.shape[1], 1), 3, axis=-1)
else:
imgB_disp = np.pad(imgB, ((w//2, w//2), (w//2, w//2), (0, 0)), mode='reflect')
imgB_disp = cv2.polylines(imgB_disp, pts=[(w//2+coords_ref).reshape((-1,1,2))], isClosed=True, color=(0,255,0), thickness=2)
imgB_disp = cv2.polylines(imgB_disp, pts=[np.int32(w//2+coords_trans).reshape((-1,1,2))], isClosed=True, color=(0,0,255), thickness=2)
# imgB_disp = cv2.polylines(imgB_disp, pts=[np.int32(w//2+coords_rec).reshape((-1,1,2))], isClosed=True, color=(255,0,0), thickness=2)
skio.imsave(f'{dispdirB}/{f_name}_display.{suffix}', imgB_disp)
cnt_disp += 1
df.to_csv(f'{target_root}/fold{fold}/patch_tlevel{t_level}/info_{mode}.csv')
# %%
if __name__ == '__main__':
# trans_mins = list(range(0, 28, 7))
# trans_maxs = list(range(7, 35, 7))
# rot_mins = list(range(0, 20, 5))
# rot_maxs = list(range(5, 25, 5))
for i in range(1, 5):
make_patches(
img_root='./Datasets/Balvan_1to4tiles',
target_root='./Datasets/Balvan_patches',
fold=1,
t_level=i,
mode='test',
display=5)
# make_patches(
# img_root='./Datasets/Balvan_1to4tiles',
# target_root='./Datasets/Balvan_patches',
# fold=1,
# t_level=i,
# mode='train',
# display=5)