-
Notifications
You must be signed in to change notification settings - Fork 0
Combine_Segmentations_And_Filter_Overlaps.ipynb
This is a notebook which follows the Trackmate_Cellpose_GUI.py
script and shows how you can use the Python API for your own custom workflows of combining segmentation masks and removing any overlapping objects. This document will breakdown the notebook.
Due to the nature of
Trackmate_Cellpose_GUI.py
output folder structure,
These are paths and filename pattern matching which can be changed.
-
root_dir_path
is the directory containing all individual image folders with respective Trackmate .xml files and resultant label .tif files -
image_dir_pattern
find all folders containing labels with this pattern name -
label_img_pattern
find all label images in above folder with this pattern name -
output_folder
path to folder you want combined labels to end up, default is same folder containing labels Whendefault
is set toTrue
the notebook assumes that it is saved in the same root directory as specified fromTrackmate_Cellpose_GUI.py
and the combined labels will be saved in sub-directories for each image.
If you don't want to use the default variables then set default
to False
and change the variables here:
# specify own pattern matching rules
if default is False:
root_dir_path = Path('/nemo/stp/lm/working/shared/vd_tf/Training_Data/Images')
image_dir_pattern = 'C3*'
label_img_pattern = 'LblImg_*_pixels.tif'
output_dir_path = Path('/nemo/stp/lm/working/shared/vd_tf/Training_Data/Images/results')
'*'
in strings is a wild character, meaning it can represent any combination or length of characters for pattern matching. You could, therefore, look for only files which start with the same prefix, suffix or extension, for example.
To ensure that the notebook is able to find all your sub-directories containing labels and the labels themselves, we have a look.
Once again, this notebook follows from Trackmate_Cellpose_GUI.py
which creates sub-directories for each image and expects respective labels of different sizes in the same folder. image_dirs
is a list of these sub-directories which match image_dir_pattern
i.e. each image.
labels
is a list of all the labels in the first folder in image_dirs
which match label_img_pattern
to make sure it detects only label images you expect.
This should be changed and is a list of all the sizes of labels you wish to combine. For this to work, these numbers should be in the filename of your labels e.g LblImg_Image_Name_60_pixels.tif
. This is useful for when you want to do multiple combinations of labels per image.
# Provide pixel sizes you want to combine, should be in filename of label images i.e. LblImg_metadata_15_pixels.tif
all_pixel_sizes = [[15, 60],
[15, 75],
[60, 75],
]
It doesn't matter which order these numbers are in.
Within the list of labels, the code will look for those with the specified sizes in all_pixel_sizes
in the filename. If the code cannot find label images of one or both sizes to be combined, it will move onto the next combination/sub-directory.
For this workflow, we want to get rid of small overlapping objects if they overlap with the larger object which is why we sort
the pixel sizes every time.
create_overlap_df
creates a dataframe of all overlapping objects as well as their ID (label) in each labelled image, size, and percent overlap. This dataframe can be printed.
pct_overlap_filter
will filter out overlapping objects based on a threshold for percent overlap. At 0
, any overlapping pixels count as overlapping and the small object is removed from the final image. At 1
, all overlapping objects are kept in the final label image.
You will probably get a warning that the saved combined labels are low contrast images, that's normal.