Skip to content

Instantly share code, notes, and snippets.

@kieranrcampbell
Last active February 24, 2021 15:45
Show Gist options
  • Save kieranrcampbell/f792ea97e45bc6323697b816406edef9 to your computer and use it in GitHub Desktop.
Save kieranrcampbell/f792ea97e45bc6323697b816406edef9 to your computer and use it in GitHub Desktop.
Expand a cell mask by one
def expand_mask_by_one(m):
"""Takes a cell mask np array as input
and returns with mask expanded by 1
"""
from skimage.morphology import dilation
m2 = m.copy()
unique_cell_ids = np.sort(np.unique(m.reshape(-1)))
unique_cell_ids = unique_cell_ids[unique_cell_ids>0]
for cell_id in unique_cell_ids:
is_cell_id = 1 * (m == cell_id)
empty = m == 0
D = dilation(is_cell_id) # new expanded binary bask
pixels_to_flip = D.astype('bool') & empty # flip expanded pixels that were previously empty
m2[pixels_to_flip] = cell_id
return m2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment