Last active
March 15, 2023 16:25
-
-
Save caniko/2a16621567d451d58e8c4ca26a475fc2 to your computer and use it in GitHub Desktop.
Delete smaller artifacts from a single object binary image volume
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterable | |
from scipy.ndimage import label, find_objects | |
def volume_from_slices(*slices: Iterable[slice]): | |
volume = 1 | |
for comp_slice in slices: | |
volume *= (comp_slice.stop - comp_slice.start) | |
return volume | |
def clean_smaller_artifacts(image_volume): | |
labeled, num_features = label(image_volume) | |
object_slice_sequence = find_objects(labeled) | |
size_to_label = { | |
volume_from_slices(*slices): label for label, slices in zip(range(1, num_features + 1), object_slice_sequence) | |
} | |
max_size = max(size_to_label) | |
max_label = size_to_label[max_size] | |
return labeled[object_slice_sequence[max_label - 1]] == max_label |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment