Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove small objects from 3D image?

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)
like image 438
Weiziyoung Avatar asked Dec 14 '25 14:12

Weiziyoung


1 Answers

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
like image 170
Weiziyoung Avatar answered Dec 19 '25 02:12

Weiziyoung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!