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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With