forked from medgift/PROCESS_L1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
old.integral.py
254 lines (206 loc) · 9.92 KB
/
old.integral.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import cv2
import numpy as np
from PIL import Image
from skimage.transform.integral import integral_image, integrate
from random import randint
def is_white_patch(cur_patch,white_percentage):
''' Basic is_white check: checks if the extracted patch is white
and returns True if so
input:
cur_patch, patch to check
white_percentage, white portion threshold
output:
True if percentage of white> white portion threshold
False otherwise
'''
#good buttt slowww
# half black and half white patches are still kept. not a good thing.
#print 'into is_white'
is_white = True
total_white = float(cur_patch.shape[0] *cur_patch.shape[1] * cur_patch.shape[2] * 255)
if (cur_patch.sum()/total_white)>white_percentage:
return is_white
else:
return not is_white
def patch_sampling_using_integral(slide,slide_level,mask,patch_size,patch_num):
"""
patch sampling on whole slide image
input:
slide = OpenSlide Object
slide_level = level of mask
mask = mask image ( 0-1 int type nd-array)
patch_size = size of patch scala integer n
patch_num = the number of output patches
output:
list of patches(RGB Image), list of patch point (starting from left top)
"""
patch_list = [] # patches
patch_point = [] # patch locations
# taking the nonzero points in the mask
x_l,y_l = mask.nonzero()
#slide_level=7
if len(x_l) > patch_size*2:
level_patch_size = int(patch_size/slide.level_downsamples[slide_level])
print 'DEBUGGG: ', slide_level
# computing the actual level of resolution
# applying the nonzero mask as a dot product
x_ws = (np.round(x_l*slide.level_downsamples[slide_level])).astype(int)
y_ws = (np.round(y_l*slide.level_downsamples[slide_level])).astype(int)
cnt = 0 # patch counter
nt_cnt = 1 # not taken counter
white_threshold = .3
#white_threshold = 1.0
while(cnt < patch_num) :
# sampling from random distribution
p_idx = randint(0,len(x_l)-1)
# picking the random point in the mask
level_point_x,level_point_y = x_l[p_idx], y_l[p_idx]
# check the boundary to make patch
check_bound = np.resize(np.array([level_point_x+level_patch_size,level_point_y+level_patch_size]),(2,))
if check_bound[0] > mask.shape[0] or check_bound[1] > mask.shape[1]:
continue
# make patch from mask image
level_patch_mask = mask[int(level_point_x):int(level_point_x+level_patch_size),int(level_point_y):int(level_point_y+level_patch_size)]
# apply integral
ii_map = integral_image(level_patch_mask)
ii_sum = integrate(ii_map,(0,0),(level_patch_size-1,level_patch_size-1))
area_percent = float(ii_sum)/(level_patch_size**2)
# checking if the total area of the patch covers at least 80% of
# the annotation region
if area_percent<0.6:
continue
if cnt > patch_num*10+1000:
print "There is no more patches to extract in this slide"
print "mask region is too small"
print "final number of patches : ",len(patch_list)
break
patch=slide.read_region((y_ws[p_idx],x_ws[p_idx]),0,(patch_size,patch_size))
patch = np.array(patch)
#print '[integral] np.sum(patch): ', np.sum(patch)
if np.sum(patch)==0:
print('[integral] AaAaAH its zeroo!!')
continue
white_mask = patch[:,:,0:3] > 200
if float(np.sum(white_mask))/(patch_size**2*3) <= white_threshold :
#if True:
if np.sum(patch)>0:
# adding the patch to the patches list
patch_list.append(cv2.cvtColor(patch,cv2.COLOR_RGBA2BGR))
# adding the patch location to the list
patch_point.append((x_l[p_idx],y_l[p_idx]))
cnt += 1 # increasing patch counter
else:
print 'This is a black patch!'
else:
nt_cnt += 1
#print 'white_mask sum: ', np.sum(white_mask)
#print 'white ratio: ', float(np.sum(white_mask))/(patch_size**2*3)
#print 'Rejected location: {0},{1}'.format(x_l[p_idx],y_l[p_idx])
if nt_cnt %1000 == 0:
if white_threshold < .7:
white_threshold += .05
nt_cnt = 1
print 'Increasing white_threshold of 0.05: ', white_threshold
else:
print 'No more patches to extract that have more than 30 percent of not white content'
break
#exit(0)
#hist, _ = np.histogram(patch, 256, [0, 256])
#if float(np.sum(hist[200:]))/np.sum(hist) < 0.5 :
# # adding the patch to the patches list
# patch_list.append(cv2.cvtColor(patch,cv2.COLOR_RGBA2BGR))
# # adding the patch location to the list
# patch_point.append((x_l[p_idx],y_l[p_idx]))
# cnt += 1 # increasing patch counter
'''TO Forget : Need to add data-driven tuning of threshold
ii_map = integral_image(cv2.cvtColor(patch,cv2.COLOR_RGB2GRAY)>210)
ii_sum = integrate(ii_map,(0,0),(level_patch_size-1,level_patch_size-1))
area_percent = float(ii_sum)/(level_patch_size**2)
#histogram = np.histogram(patch,256,[0,256])[0]
if area_percent < 0.2:
# adding the patch to the patches list
patch_list.append(cv2.cvtColor(patch,cv2.COLOR_RGBA2BGR))
# adding the patch location to the list
patch_point.append((x_l[p_idx],y_l[p_idx]))
cnt += 1 # increasing patch counter
### checking if white patch (seba version)
if not is_white_patch(patch, 0.8):
# adding the patch to the patches list
patch_list.append(cv2.cvtColor(patch,cv2.COLOR_RGBA2BGR))
# adding the patch location to the list
patch_point.append((x_l[p_idx],y_l[p_idx]))
cnt += 1 # increasing patch counter
'''
''' To Forget
if True:
patch_list.append(cv2.cvtColor(patch,cv2.COLOR_RGBA2BGR))
cnt += 1
#print 'increasing counter ',cnt
'''
def_pl=[]
def_pp=[]
for i in range(len(patch_list)):
if (np.sum(patch_list[i])>0) and (np.mean(patch_list[i])>90):
def_pl.append(patch_list[i])
def_pp.append(patch_point[i])
return def_pl, def_pp
def tumor_patch_sampling_using_centerwin(slide,slide_level,mask,patch_size,patch_num):
"""
tumor patch sampling using center window
plz input the only tumor mask
it will malfunctioned if you input normal mask or tissue mask
input parameters are same as patch_sampling_using_integral
"""
patch_list = []
patch_point = []
window_size = int(32/ slide.level_downsamples[slide_level])
x_l,y_l = mask.nonzero()
if len(x_l) > patch_size*2:
level_patch_size = int(patch_size/slide.level_downsamples[slide_level])
x_ws = (np.round(x_l*slide.level_downsamples[slide_level])).astype(int)
y_ws = (np.round(y_l*slide.level_downsamples[slide_level])).astype(int)
cnt=0
while(len(patch_list) < patch_num) :
# loop cnt
cnt+=1
#random Pick point in mask
p_idx = randint(0,len(x_l)-1)
#Get the point in mask
level_point_x,level_point_y = x_l[p_idx], y_l[p_idx]
#Check boundary to make patch
check_bound = np.resize(np.array([level_point_x+level_patch_size,level_point_y+level_patch_size]),(2,))
if check_bound[0] > mask.shape[0] or check_bound[1] > mask.shape[1]:
continue
#make patch from mask image
level_patch_mask = mask[int(level_point_x):int(level_point_x+level_patch_size),int(level_point_y):int(level_point_y+level_patch_size)]
'''Biggest difference is here'''
#apply center window (32x32)
cntr_x= (level_patch_size/2)-1
cntr_y= (level_patch_size/2)-1
win_x = cntr_x-window_size/2
win_y = cntr_y-window_size/2
t_window = level_patch_mask[win_x:(win_x+window_size),win_y:(win_y+window_size)]
#print level_patch_mask.shape
#print win_x
#print win_y
#apply integral to window
ii_map = integral_image(t_window)
#print t_window.shape
ii_sum = integrate(ii_map,(0,0),(window_size-1,window_size-1))
area_percent = float(ii_sum)/(window_size**2)
# print "integral_area: ",area_percent
# print "loop count: ",cnt
if area_percent <1.0:
continue
if cnt > patch_num*10+1000:
print "There is no moare patches to extract in this slide"
print "mask region is too small"
print "final number of patches : ",len(patch_list)
break
#patch,point is appended the list
#print "region percent: ",area_percent
patch_point.append((x_l[p_idx],y_l[p_idx]))
patch=slide.read_region((y_ws[p_idx],x_ws[p_idx]),0,(patch_size,patch_size))
patch =np.array(patch)
patch_list.append(cv2.cvtColor(patch,cv2.COLOR_RGBA2BGR))
return patch_list, patch_point