-
Notifications
You must be signed in to change notification settings - Fork 2
/
post_processing.py
40 lines (29 loc) · 1.01 KB
/
post_processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from scipy.ndimage import morphology
from skimage.measure import label
import numpy as np
import os
def connected_component_analysis_3d(seg):
"""Binary connected component analysis
for 3D segmentation masks
ASSUMPTION: the voxel at coordinate (0,0,0)
is assumed to be a background.
"""
CC_labels = label(seg)
# (0,0,0) belongs to background
bkg_label = CC_labels[0,0,0]
# labels of different components
comp_labels = np.unique(CC_labels)
comp_labels = list(comp_labels)
comp_labels.remove(bkg_label)
# find the component with largest volume
vols = np.zeros(len(comp_labels))
for i, lab in enumerate(comp_labels):
vols[i] = np.sum(CC_labels==lab)
largest_comp_label = comp_labels[np.argsort(-vols)[0]]
cc_seg = np.zeros(seg.shape, dtype=np.uint32)
cc_seg[CC_labels==largest_comp_label] = 1
return cc_seg
def fill_holes(seg):
"""Filling holes in a binary segmentation mask
"""
return np.uint32(morphology.binary_fill_holes(seg))