Download this file

19 lines (15 with data), 868 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def largestCC(mask):
"""Finds the largest connected component.
Takes a binary numpy array.
Returns the mask with only the largest component
"""
import numpy as np
from skimage.measure import label
# Creates a labelmap of the mask, where connected components have the same label
labels = label(mask)
# labels[np.nonzero(labels)] is a list of all nonzero values in the labelmap. np.argmax(np.bincount()) returns the most common number in that list. largest is set to a mask showing where the labelmap is equal to that value.
largest = labels == np.argmax(np.bincount(labels[np.nonzero(labels)]))
if np.count_nonzero(largest) == 0:
# Throw an exception if the output mask is empty. This could happen if the input mask is also empty
raise Exception("Largest connected component not found")
return largest