def create_roi_masks(centers_of_mass, putative_nuclei_image, putative_somata_image=None, radius=3):
"""
create roi masks for the outer segment of the cell (i.e. soma)
:param radius: limits the size of the mask
:return roi_mask_list: a list of pixels for each cell, for further analysis
"""
roi_mask_list = []
if putative_somata_image is None:
putative_somata_image = np.zeros_like(putative_nuclei_image)
putative_nuclei_image = remove_small_blobs(centers_of_mass, putative_nuclei_image)[0]
watershed_image = np.logical_or(putative_nuclei_image, putative_somata_image)
labelled_watershed = calculate_distance(centers_of_mass, watershed_image)
labelled_putative_somata = putative_somata_image*labelled_watershed
labelled_putative_nuclei = calculate_distance(centers_of_mass, putative_nuclei_image)*putative_nuclei_image # nuclei need their own watershed
for i in range(np.max(labelled_putative_somata)): # for each nucleus
# calculate the distance away from the nucleus boundary
distance_from_blob_centre = ndimage.distance_transform_edt(labelled_putative_nuclei != i+1)
bool_mask = np.ones_like(labelled_putative_nuclei)
bool_mask[distance_from_blob_centre > radius] = 0
bool_mask[distance_from_blob_centre == 0] = 0
# take all indices within the radius number of pixels of the nucleus boundary
idx = np.where(labelled_putative_somata*bool_mask == i+1)
x = np.vstack((idx[0], idx[1]))
m = np.reshape(x.T, (len(idx[0]), 2))
roi_mask_list.append(m)
return roi_mask_list
评论列表
文章目录