-
Notifications
You must be signed in to change notification settings - Fork 0
/
leviating_volume.py
55 lines (43 loc) · 1.47 KB
/
leviating_volume.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from scipy import ndimage
from skimage.segmentation import clear_border
def get_structure(neighbors_num=6):
"""
function for determine which voxels we consider as neighbor ones
:type neighbors_num: int, shud be 6,18 or 26
"""
if neighbors_num == 6:
structure = [
[[0, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 1, 0], [1, 1, 1], [0, 1, 0]],
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]
]
elif neighbors_num == 18:
structure = [
[[0, 1, 0], [1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 1, 1], [1, 1, 1]],
[[0, 1, 0], [1, 1, 1], [0, 1, 0]]
]
elif neighbors_num == 26:
structure = [
[[1, 1, 1], [1, 1, 1], [1, 1, 1]],
[[1, 1, 1], [1, 1, 1], [1, 1, 1]],
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
]
else:
raise ValueError('You should choose neighbors_num from [6, 18, 26]')
return structure
def get_levitating_volume(volume, neighbors_num=6):
"""
function returns a volume of levitating stones for the
chosen neighbor voxels configuration
"""
structure = get_structure(neighbors_num)
connected_components, _ = ndimage.label(volume, structure)
levitating_volume = clear_border(connected_components) > 0
return levitating_volume
def remove_levitating_stones(image):
levitating_stones = get_levitating_volume(
image, neighbors_num=6
)
image[levitating_stones] = False
return image