-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoft_skeleton.py
More file actions
51 lines (34 loc) · 1.36 KB
/
soft_skeleton.py
File metadata and controls
51 lines (34 loc) · 1.36 KB
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
41
42
43
44
45
46
47
48
49
50
51
import torch
import torch.nn.functional as F
class SoftSkeletonize(torch.nn.Module):
def __init__(self, num_iter=40):
super(SoftSkeletonize, self).__init__()
self.num_iter = num_iter
def soft_erode(self, img):
if len(img.shape)==4:
p1 = -F.max_pool2d(-img, (3,1), (1,1), (1,0))
p2 = -F.max_pool2d(-img, (1,3), (1,1), (0,1))
return torch.min(p1,p2)
elif len(img.shape)==5:
p1 = -F.max_pool3d(-img,(3,1,1),(1,1,1),(1,0,0))
p2 = -F.max_pool3d(-img,(1,3,1),(1,1,1),(0,1,0))
p3 = -F.max_pool3d(-img,(1,1,3),(1,1,1),(0,0,1))
return torch.min(torch.min(p1, p2), p3)
def soft_dilate(self, img):
if len(img.shape)==4:
return F.max_pool2d(img, (3,3), (1,1), (1,1))
elif len(img.shape)==5:
return F.max_pool3d(img,(3,3,3),(1,1,1),(1,1,1))
def soft_open(self, img):
return self.soft_dilate(self.soft_erode(img))
def soft_skel(self, img):
img1 = self.soft_open(img)
skel = F.relu(img-img1)
for j in range(self.num_iter):
img = self.soft_erode(img)
img1 = self.soft_open(img)
delta = F.relu(img-img1)
skel = skel + F.relu(delta - skel * delta)
return skel
def forward(self, img):
return self.soft_skel(img)