How to remove small objects from 3D image?

1.4k Views Asked by At

3D image Do you see that?There are some small objects spread below the brain. and I want to remove them to get a whole clean brain.

A 3D image can be expressed as a 3D array in Numpy.

Below is an approach to remove small objects in 2D image.

from skimage import morphology
img_size = img.shape[0] * img.shape[1]
new_img = morphology.remove_small_objects(img, img_size*0.1)
1

There are 1 best solutions below

0
On

Here is my solution:


from skimage import morphology

    def remove_small_objects(img):
        binary = copy.copy(img)
        binary[binary>0] = 1
        labels = morphology.label(binary)
        labels_num = [len(labels[labels==each]) for each in np.unique(labels)]
        rank = np.argsort(np.argsort(labels_num))
        index = list(rank).index(len(rank)-2)
        new_img = copy.copy(img)
        new_img[labels!=index] = 0
        return new_img