Skip to content

Instantly share code, notes, and snippets.

@caniko
Last active March 15, 2023 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caniko/2a16621567d451d58e8c4ca26a475fc2 to your computer and use it in GitHub Desktop.
Save caniko/2a16621567d451d58e8c4ca26a475fc2 to your computer and use it in GitHub Desktop.
Delete smaller artifacts from a single object binary image volume
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