3

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)
Weiziyoung
  • 191
  • 1
  • 2
  • 12
  • `remove_small_objects` doesn't work for 3D images? Maybe you need to find a better toolbox. [Try DIPlib!](https://github.com/DIPlib/diplib) In DIPlib almost all the functions are defined for 3D images (and any other number of dimensions). You'd call [`dip.AreaClosing`](https://diplib.github.io/diplib-docs/group__morphology.html#gac2dd0bf265933b107e4c443a7d19310d) to remove small objects. – Cris Luengo Jul 08 '19 at 15:36
  • 1
    Yes, `remove_small_objects` works perfectly fine with a 3D image. Sorry Cris! =P – Juan Jul 09 '19 at 13:13
  • Also, try `array.size`. ;) – Juan Jul 09 '19 at 13:13
  • nice question (and answer) +1. unfortunately I'm not super experienced with `skimage` but I feel morphology filters should help, if `skimage` supports it, erosion should clear smaller areas – George Profenza Jul 18 '19 at 15:36
  • @GeorgeProfenza I don't think so. Because you don't know how large are the blobs.And erosion operation cannot tackle it. – Weiziyoung Jul 20 '19 at 12:45
  • @Weiziyoung Very good point, I stand corrected – George Profenza Jul 20 '19 at 13:20

1 Answers1

2

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
Weiziyoung
  • 191
  • 1
  • 2
  • 12