Newer
Older
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
"cells": [
{
"cell_type": "markdown",
"id": "a17e6d9a",
"metadata": {},
"source": [
"# Erode and dilate masks\n",
"Notebook author: Thorbjørn Erik Køppen Christensen (tekc@dtu.dk)\n",
"\n",
"This notebook can be used to run erosion and dilation on binary masks.\n",
"This can be useful for removing \"noisy\" attatchments, closing holes and smoothing edges of masks.\n",
"Can run in either 2D (circle) or 3D (sphere). Note that the 3D version is somewhat more expensive in memory requirements"
]
},
{
"cell_type": "markdown",
"id": "2801d11e",
"metadata": {},
"source": [
"## Import Libraries"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "386d5c86",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import h5py\n",
"import qim3d\n",
"import scipy.ndimage"
]
},
{
"cell_type": "markdown",
"id": "7e2a79e1",
"metadata": {},
"source": [
"## Define functions"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "90f12d70",
"metadata": {},
"outputs": [],
"source": [
"def structure_sphere(r,dims=3):\n",
" #Creates a sphere of with radius r in dims dimensions\n",
" #r is in pixels\n",
" \n",
" d = 2*r-1 #The \"-1\" is to combat a small offeset from indexing starting at 0\n",
" #Define the center for dims dimensions\n",
" if r % 2 ==0:\n",
" center = (np.s_[r-1:r],)*dims \n",
" else:\n",
" center = np.s_[(r-1,)*dims]\n",
" \n",
" #turn everything within r indices of center into 1\n",
" vol = np.zeros((d,)*dims)\n",
" vol[center] = 1\n",
" distance = scipy.ndimage.distance_transform_edt(1-vol)\n",
" vol[distance<=r-1] = 1\n",
" vol[distance>1] = 0\n",
" return vol"
]
},
{
"cell_type": "markdown",
"id": "babfd650",
"metadata": {},
"source": [
"## Load data"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "8a8cec6f",
"metadata": {},
"outputs": [],
"source": [
"# Load data\n",
"filename = \"../resources/img3d/shell_225x128x128_bin_mask.h5\"\n",
"vol = qim3d.io.load(filename) "
]
},
{
"cell_type": "markdown",
"id": "3afb4c6e",
"metadata": {},
"source": [
"## Erode and dilate"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "e30210bd",
"metadata": {},
"outputs": [],
"source": [
"# Erode and dilate\n",
"\n",
"vol = structure_sphere(100)\n",
"sphere = structure_sphere(5)\n",
"vol = scipy.ndimage.binary_erosion(vol, structure=sphere.astype(vol.dtype))\n",
"vol = scipy.ndimage.binary_dilation(vol, structure=sphere.astype(vol.dtype))"
]
},
{
"cell_type": "markdown",
"id": "357a1f48",
"metadata": {},
"source": [
"## Save eroded/dilated volume"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "e3243a93",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(slice(1, 2, None), slice(1, 2, None), slice(1, 2, None))"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Save binary_segmentation\n",
"save_folder = '../resources/img3d/'\n",
"save_file = filename.split('/')[-1].split('.')[0] + '_eroded_dilated.h5'\n",
"with h5py.file(os.path.join(save_folder,save_file),'w') as sfile:\n",
" sfile.create_dataset('mask/eroded',data=vol)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcfadd98",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}