-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape_filter.py
453 lines (314 loc) · 14.5 KB
/
shape_filter.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from cell_count import CellData
import os,json
import numpy
from skimage.morphology import watershed, disk, square, remove_small_objects,opening
from scipy.ndimage.filters import gaussian_laplace
from skimage.filters import gaussian,laplace, threshold_otsu
from skimage.measure import regionprops,label
import scipy.ndimage as ndi
from collections import OrderedDict
from dicttoxml import dicttoxml
import xmltodict
from skimage.feature import peak_local_max
import trackpy
def threshold_runnable(image,threshold = threshold_otsu):
thresh = threshold(image)
return thresh
class shapeFilter(object):
def __init__(self, directory,cellData = None):
"""
directory should contain background subtracted MIPs from ch01 and ch02
"""
self.directory = directory
if cellData is None:
self.cellData = CellData(self.directory,setupPool = False)
self.cellData.loadImages()
else:
self.cellData = cellData
def initialShapeFilter(self):
# openRedMIP = self.openImage_runnable(self.cellData.stack_channel_images[self.cellData.channels[1]][0])
#runs an opening to amplify separation between cells
gaussianredmip = trackpy.bandpass(self.cellData.stack_channel_images[self.cellData.channels[1]][0],1,27)
gaussianredmip = gaussian(self.cellData.stack_channel_images[self.cellData.channels[1]][0],sigma = 7)
gaussianredmip = laplace(gaussianredmip)
#gaussian smoothing for binary mask
binary_gaussian_red = shapeFilter.getBinary_runnable(gaussianredmip,use_percentile=True,percentile = 0.5)
self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),self.cellData.basedir,
'Labeled_Binary_Red','binary_mip')
#creates binary mask using otsu
binary_gaussian_red = shapeFilter.labelBinaryImage_runnable(binary_gaussian_red)
self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),self.cellData.basedir,
'Labeled_Binary_Red','initial_labeling')
binary_gaussian_red = shapeFilter.areaFilter_runnable(binary_gaussian_red)
self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),self.cellData.basedir,
'Labeled_Binary_Red','binary_opened_mip')
#removes small objects from binary mask
Image_properties, binary_gaussian_red = shapeFilter.getImageCoordinates_runnable(binary_gaussian_red)
# gets properties of labeled binary objects
for i in range(len(Image_properties)):
props = Image_properties[i]
if int(props.equivalent_diameter) > 40:
pass
else:
self.cellData.labeled_properties[i] = {
'bbox': list(props.bbox),
'area': int(props.filled_area),
'y' : int(props.centroid[1]),
'x' : int(props.centroid[0]),
'diameter': int(props.equivalent_diameter),
'label' : int(props.label)}
self.cellData.processed_stack_images[self.cellData.channels[1]]['Labeled Binary Red'] = binary_gaussian_red
self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),self.cellData.basedir,
'Labeled_Binary_Red','binary_mip_labeled')
self.saveMetaData(foldername='Labeled_Binary_Red')
def countCells(self,stackCellData):
image_properties = self.cellData.labeled_properties
binary_gaussian_red = self.cellData.processed_stack_images[self.cellData.channels[1]]['Labeled Binary Red']
cell_count_list = []
for key in sorted(image_properties.keys()):
item = image_properties[key]
print(key,item['label'])
x,y = item['x'],item['y']
item['z'] = 1
item['type'] = 1
x1,x2 = item['bbox'][0],item['bbox'][2]
y1,y2 = item['bbox'][1],item['bbox'][3]
cutoutsize = int(4*item['diameter'])
objectarea = int(item['area'])
binary_cutout_image = self.cutImageByBoundary(binary_gaussian_red,xstart=x1,xstop=x2,ystart=y1,ystop=y2)
binredcut = shapeFilter.getImageCutouts_runnable(binary_cutout_image,x,y,cutout=cutoutsize)
binredcut = self.removeObjectsByLabel_runnable(binredcut,item)
print(binredcut.shape)
fieldStacks = self.getCutoutFieldStacks_runnable(stackCellData,item)
smallredstack = fieldStacks['red_stack']
smallgreenstack = fieldStacks['green_stack']
redhist = fieldStacks['sigmas']
greenbin = fieldStacks['green_bin']
redbin = fieldStacks['red_bin']
print(greenbin.shape,redbin.shape)
fieldStacks = None
finalfield = []
for i in range(1,len(smallredstack)):
if redhist[i] > numpy.percentile(redhist,20):
smallbingreen = greenbin[i]
smallbinred = redbin[i]
labeledGreen1 = shapeFilter.labelBinaryImage_runnable(smallbingreen)
labeledGreen1 = remove_small_objects(labeledGreen1,5)
themax = numpy.amax(labeledGreen1)
# if themax == 1:
# pass
# else:
labeledRed = shapeFilter.labelBinaryImage_runnable(smallbinred)
fieldsize = labeledGreen1.shape[0]*labeledGreen1.shape[1]
strucsize = int(0.25*fieldsize)
trial = ndi.distance_transform_edt(smallbingreen)
local_max = peak_local_max(trial,indices = False,labels = labeledGreen1,min_distance=3)
markers = ndi.label(local_max)[0]
watershedGreen = watershed(-trial,markers,mask = labeledGreen1, watershed_line=True)
labeledGreen = label(watershedGreen,8)
greenfieldProps,labeledGreen = shapeFilter.getImageCoordinates_runnable(labeledGreen)
gareas = []
subimg = numpy.zeros(labeledGreen.shape)
for gprop in greenfieldProps:
props = {}
props['area'] = gprop.filled_area
props['label'] = gprop.label
gareas.append(props)
for area in gareas:
if area['area'] < strucsize:
subimg = numpy.zeros(labeledGreen.shape)
pass
else:
subimg = area['label']*(labeledGreen == area['label'])
labeledGreen = labeledGreen - subimg
# print('max green',numpy.amax(labeledGreen))
redfieldProps = shapeFilter.getImageCoordinates_runnable(labeledRed)
subimg = numpy.zeros(labeledRed.shape)
rareas = []
for rprop in redfieldProps[0]:
props = {}
props['area'] = rprop.filled_area
props['label'] = rprop.label
rareas.append(props)
for area in rareas:
if area['area'] < strucsize:
subimg = numpy.zeros(labeledGreen.shape)
pass
else:
subimg = area['label']*(labeledRed == area['label'])
labeledRed = labeledRed - subimg
labeledRed = label(remove_small_objects(labeledRed,5),4)
# print('max labeled red',numpy.amax(labeledRed))
# print('binred shape', binredcut.shape,'\n','label green shape',labeledGreen.shape)
product = label(labeledRed*labeledGreen,neighbors = 8,connectivity = 2)
# print('max of product',numpy.amax(product))
try:
final_product = binredcut*product
final_product = remove_small_objects(label(final_product),5)
except ValueError:
final_product = remove_small_objects(product,5)
labeledproductprops,labeled_final_product = shapeFilter.getImageCoordinates_runnable(final_product)
# print("Detection Number:",len(labeledproductprops))
if len(labeledproductprops) != 0:
# print('appending')
finalfield.append(final_product)
# print(len(finalfield),key)
if len(finalfield) != 0:
print('cell found at:',key,item['label'])
cellDict = OrderedDict()
cellDict['MarkerX'] = item['y']
cellDict['MarkerY'] = item['x']
cellDict['MarkerZ'] = item['z']
cell_count_list.append(cellDict)
print("Count is: \t",len(cell_count_list))
return cell_count_list
def cutImageByBoundary(self,img,xstart,xstop,ystart,ystop):
newImage = numpy.zeros(img.shape)
newImage[xstart:xstop,ystart:ystop] = img[xstart:xstop,ystart:ystop]
newImage >= 0
newImage = newImage.astype(int)
return newImage
def getMaxPro_runnable(self,imgStack):
imgStack = numpy.asarray(imgStack)
return numpy.max(imgStack,axis=0)
@classmethod
def areaFilter_runnable(cls,image,objectFilter = remove_small_objects,default_size = 20):
return objectFilter(image,default_size)
@classmethod
def getBinary_runnable(cls,image,threshold = threshold_runnable,use_percentile = True,
percentile = 0.7,np=numpy):
img_threshold = threshold_runnable(image)
if use_percentile:
return numpy.asarray((image > percentile * img_threshold),dtype = numpy.int)
else:
return numpy.asarray((image > img_threshold),dtype=numpy.int)
@classmethod
def gausLap_runnable(cls,image,sigma = 3,gaussianLap = gaussian_laplace):
return gaussianLap(image,sigma)
@classmethod
def labelBinaryImage_runnable(cls,image,label = label,neighbors = 8):
return label(image,neighbors = neighbors)
@classmethod
def getImageCoordinates_runnable(cls,image,intensity_image = None,regionprops=regionprops):
labeledimg = shapeFilter.labelBinaryImage_runnable(image)
if intensity_image is not None:
props = regionprops(labeledimg,intensity_image=intensity_image)
else:
props = regionprops(labeledimg)
return props, labeledimg
@classmethod
def getImageCutouts_runnable(cls,img,x,y,cutout=50):
if x < cutout:
xstart = 0
xstop = cutout
# print('xstart',xstart,'xstop',xstop)
else:
xstart,xstop = x - int(cutout/2), x + int(cutout/2)
if y < cutout:
ystart = 0
ystop = cutout
# print('ystart',ystart,'ystop',ystop)
else:
ystart,ystop = y - int(cutout/2), y + int(cutout/2)
newimg = img[xstart:xstop,ystart:ystop]
# print('cutoutshape',newimg.shape)
return newimg
def getCutoutFieldStacks_runnable(self,cellDataObject,image_properties):
assert(type(cellDataObject == CellData))
x,y = image_properties['x'],image_properties['y']
redstack = cellDataObject.stack_channel_images['ch02']
greenstack = cellDataObject.stack_channel_images['ch01']
assert(len(redstack)==len(greenstack))
smallredstack = []
smallgreenstack = []
redhist = []
greenbin = []
redbin = []
cutoutsize = int(4*image_properties['diameter'])
# print('Field cutoutsize',cutoutsize)
for r,g in zip(redstack,greenstack):
redcut = shapeFilter.getImageCutouts_runnable(r,x,y,cutout=cutoutsize)
redhist.append(numpy.std(redcut))
redcut = opening(redcut,selem = square(1))
try:
smallbinred = shapeFilter.getBinary_runnable(redcut,use_percentile = True,percentile = .7)
except ValueError:
smallbinred = numpy.zeros(redcut.shape)
greencut = shapeFilter.getImageCutouts_runnable(g,x,y,cutout=cutoutsize)
greencut = opening(greencut,selem = square(1))
try:
smallbingreen = shapeFilter.getBinary_runnable(greencut,use_percentile=True,percentile = .7)
except ValueError:
smallbingreen = numpy.zeros(greencut.shape)
smallredstack.append(redcut)
redbin.append(smallbinred)
smallgreenstack.append(greencut)
greenbin.append(smallbingreen)
smallredstack = numpy.asarray(smallredstack)
smallgreenstack = numpy.asarray(smallgreenstack)
redhist = numpy.asarray(redhist)
greenbin = numpy.asarray(greenbin)
redbin = numpy.asarray(redbin)
dataDictionary = {'red_stack' : smallredstack,
'sigmas' : redhist,
'green_stack' : smallgreenstack,
'green_bin' : greenbin,
'red_bin' : redbin}
return dataDictionary
def openImage_runnable(self,image,opening = opening, selem = square(1)):
return opening(image,selem)
def unloadImages(self):
if self.cellData is not None:
self.cellData.unloadImages()
def removeObjectsByLabel_runnable(self,labeled_image_cutout,image_properties):
objectlabel = image_properties['label']
clean_image = labeled_image_cutout*(labeled_image_cutout == objectlabel).astype(int)
return clean_image
def saveMetaData(self,foldername):
if len(self.cellData.labeled_properties) != 0:
filepath = os.path.join(self.cellData.basedir,foldername,'measured_properties.json')
if not os.path.exists(os.path.join(self.cellData.basedir,foldername)):
os.mkdir(os.path.join(self.cellData.basedir,foldername))
with open(filepath,'w') as f:
json.dump(self.cellData.labeled_properties,f)
# for item in os.listdir(os.getcwd()):
# if 'data' in item.lower():
# practice_data_folder = os.path.join(os.getcwd(),item,'06_R','BGsubMIPs')
# print(practice_data_folder)
# atest = ShapeFilter(practice_data_folder)
# atest = None
# datalabel = '06'
# print('starting processing at ',datetime.datetime.now(pytz.timezone('US/Pacific')).isoformat())
# if __name__ == '__main__':
# for item in os.listdir(practice_data_folder):
# if datalabel in item:
# if os.path.isdir(os.path.join(practice_data_folder,item)):
# Folder = os.path.join(practice_data_folder,item,'BGsubMIPs')
# testDataMIPs = CellData(directory = Folder)
# # print(Folder)
# testDataMIPs.loadImages()
# newredMIP = testDataMIPs.stack_channel_images['ch02'][0]
# openRedMIP = openImage_runnable(newredMIP)
# gaussianredmip = gausLap_runnable(openRedMIP)
# binary_gaussian_red = getBinary_runnable(gaussianredmip,use_percentile=True,percentile = 0.5)
# binary_gaussian_red = labelBinaryImage_runnable(binary_gaussian_red)
# testDataMIPs.processed_stack_images['ch02'] = {}
# testDataMIPs.processed_stack_images['ch02']['Binarized Labeled'] = binary_gaussian_red
# area_filtered_binary = areaFilter_runnable(binary_gaussian_red).astype(numpy.uint8)
# labeled_area_filtered = labelBinaryImage_runnable(area_filtered_binary)
# mipProps = getImageCoordinates_runnable(labeled_area_filtered)
# for i in range(len(mipProps)):
# props = mipProps[i]
# properties = {'bbox' : props.bbox,
# 'area' : int(props.filled_area),
# 'y' : int(props.centroid[1]),
# 'x' : int(props.centroid[0]),
# 'diameter' : int(props.equivalent_diameter),
# 'label' : props.label}
# testDataMIPs.labeled_properties[i] = properties
# jsonfilename = os.path.join(practice_data_folder,item,'Labeled_binary_red','measured_properties.json')
# saveImages(area_filtered_binary,
# os.path.join(practice_data_folder,item),'Labeled_binary_red','red_binary_mip')
# with open(jsonfilename,'w') as f:
# json.dump(testDataMIPs.labeled_properties,f)
# testDataMIPs = None