Keep the biggest cluster/object inside an array

1.1k Views Asked by At

I've created a mask to extract only the red portion of an image. The result looks like this: Mask I want to only keep the biggest cluster of white (and remove every other smaller cluster). I looked into morphology.remove_small_objects to remove the other cultures, but sometimes these second clusters get almost the size of the biggest one. So I need another way to get rid of them.

1

There are 1 best solutions below

1
Paddy Harrison On

morphology.remove_small_objects will certainly work, you just need to calculate the size accurately. You can do this with measure.label and measure.regionprops:

from skimage import measure, morphology

# assuming mask is a binary image
# label and calculate parameters for every cluster in mask
labelled = measure.label(mask)
rp = measure.regionprops(labelled)

# get size of largest cluster
size = max([i.area for i in rp])

# remove everything smaller than largest
out = morphology.remove_small_objects(mask, min_size=size-1)