-1

I want to remove the small connected object from my 3D volume in Python (i.e. objects having less than N adjacent pixels in 3D). I found a relevant example using OpenCV at this link, but the method does not seem to work for 3D objects (it only works on 2D images).

Is there any alternative I can use to remove objects smaller than N adjacent pixels from the input 3D binary mask?


Edit: I found a possible solution that seems to work. I leave it in the answers.

gab
  • 792
  • 1
  • 10
  • 36
  • Post an example image and explain relative to that. – fmw42 Jul 18 '22 at 16:11
  • 1
    I've tagged skimage because (1) you're gonna use numpy arrays (2) OpenCV is for 2D images, not volumes, whereas skimage has plenty of algorithms that work on N-D arrays, including "connected components labeling" – Christoph Rackwitz Jul 18 '22 at 18:41
  • Thanks for your suggestion @ChristophRackwitz . I found a possible solution using skimage. I leave it in the answers! – gab Jul 19 '22 at 08:51

1 Answers1

0

I found a possible solution using skimage morphology module:

def remove_small_blobs(binary_mask: np.ndarray, min_size: int = 0):
    """
    Removes from the input mask all the blobs having less than N adjacent pixels.
    We set the small objects to the background label 0.
    """
    if min_size > 0:
        dtype = binary_mask.dtype
        binary_mask = skimage.morphology.remove_small_objects(binary_mask.astype(bool), min_size=min_size)
        binary_mask = binary_mask.astype(dtype)
    return binary_mask
gab
  • 792
  • 1
  • 10
  • 36