-
Notifications
You must be signed in to change notification settings - Fork 5
/
skeletonization.py
35 lines (28 loc) · 1.11 KB
/
skeletonization.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
from typing import Tuple
import torch
import numpy as np
from skimage.morphology import skeletonize, dilation
from batchgeneratorsv2.transforms.base.basic_transform import BasicTransform
class SkeletonTransform(BasicTransform):
def __init__(self, do_tube: bool = True):
"""
Calculates the skeleton of the segmentation (plus an optional 2 px tube around it)
and adds it to the dict with the key "skel"
"""
super().__init__()
self.do_tube = do_tube
def apply(self, data_dict, **params):
seg_all = data_dict['segmentation'].numpy()
# Add tubed skeleton GT
bin_seg = (seg_all > 0)
seg_all_skel = np.zeros_like(bin_seg, dtype=np.int16)
# Skeletonize
if not np.sum(bin_seg[0]) == 0:
skel = skeletonize(bin_seg[0])
skel = (skel > 0).astype(np.int16)
if self.do_tube:
skel = dilation(dilation(skel))
skel *= seg_all[0].astype(np.int16)
seg_all_skel[0] = skel
data_dict["skel"] = torch.from_numpy(seg_all_skel)
return data_dict