Switch to unified view

a b/examples/create meshes/Create Muscle Mesh Jan.28.2022.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 4,
6
   "id": "f11c51b1",
7
   "metadata": {},
8
   "outputs": [],
9
   "source": [
10
    "import pymskt as mskt\n",
11
    "import SimpleITK as sitk\n",
12
    "import numpy as np\n",
13
    "import os\n",
14
    "\n",
15
    "from pymskt.mesh import Mesh"
16
   ]
17
  },
18
  {
19
   "cell_type": "code",
20
   "execution_count": 10,
21
   "id": "20bbd34d",
22
   "metadata": {},
23
   "outputs": [],
24
   "source": [
25
    "location_seg = '../../data/muscles/segmentation.nii.gz'\n",
26
    "location_save = os.path.expanduser('~/Downloads')"
27
   ]
28
  },
29
  {
30
   "cell_type": "code",
31
   "execution_count": 5,
32
   "id": "c43ac4d4",
33
   "metadata": {},
34
   "outputs": [
35
    {
36
     "name": "stderr",
37
     "output_type": "stream",
38
     "text": [
39
      "Use of `point_arrays` is deprecated. Use `point_data` instead.\n",
40
      "Use of `cell_arrays` is deprecated. Use `cell_data` instead.\n"
41
     ]
42
    }
43
   ],
44
   "source": [
45
    "# Loading the image in first so we can see what the labels are. \n",
46
    "# This can be skipped and a path provided directly if we know\n",
47
    "# the labels of interest. \n",
48
    "seg_image = sitk.ReadImage(location_seg)\n",
49
    "array = sitk.GetArrayFromImage(seg_image)\n",
50
    "seg_labels = np.unique(array)\n",
51
    "\n",
52
    "mesh1 = Mesh(\n",
53
    "    seg_image=seg_image,\n",
54
    "    label_idx=seg_labels[1]\n",
55
    "    \n",
56
    ")\n",
57
    "\n",
58
    "# Below is the command to create the mesh\n",
59
    "# all of the defaults inputs parameters are being listed \n",
60
    "# so that the available options are shown. \n",
61
    "mesh1.create_mesh(\n",
62
    "    smooth_image=True,            # I suggest leaving this on to help create smooth surfaces.   \n",
63
    "    smooth_image_var=0.3125/2,    # this is the variance of a gaussian filter - can probably be bigger for muscles.\n",
64
    "    marching_cubes_threshold=0.5, # this probably shouldnt be changed. \n",
65
    "    label_idx=None,               # Can specify this here instead of above if you want. \n",
66
    "    min_n_pixels=None,            # if there is a minimum number of pixels you want for it to create a mesh. \n",
67
    ")\n",
68
    "\n",
69
    "mesh1.resample_surface(clusters=10000) # Resample surface to be a specified # of vertices (w/ in a small margin of error)\n",
70
    "\n"
71
   ]
72
  },
73
  {
74
   "cell_type": "markdown",
75
   "id": "a4c3a458",
76
   "metadata": {},
77
   "source": [
78
    "## Save the mesh"
79
   ]
80
  },
81
  {
82
   "cell_type": "code",
83
   "execution_count": 11,
84
   "id": "7bd7c341",
85
   "metadata": {},
86
   "outputs": [],
87
   "source": [
88
    "mesh1.save_mesh(os.path.join(location_save, 'mesh1.vtk')) # Save .vtk version of the mesh\n",
89
    "mesh1.save_mesh(os.path.join(location_save, 'mesh1.stl')) # Save .stl version of the mesh. "
90
   ]
91
  },
92
  {
93
   "cell_type": "markdown",
94
   "id": "394b11b0",
95
   "metadata": {},
96
   "source": [
97
    "# If you want to iterate over all of the labels and save meshes: "
98
   ]
99
  },
100
  {
101
   "cell_type": "markdown",
102
   "id": "c4c39deb",
103
   "metadata": {},
104
   "source": [
105
    "### First create a dictionary to store all of the meshes"
106
   ]
107
  },
108
  {
109
   "cell_type": "code",
110
   "execution_count": 7,
111
   "id": "4368b846",
112
   "metadata": {},
113
   "outputs": [],
114
   "source": [
115
    "dict_meshes = {}\n",
116
    "for label_idx in seg_labels[1:]:\n",
117
    "    dict_meshes[label_idx] = mskt.mesh.Mesh(\n",
118
    "        seg_image=seg_image,\n",
119
    "        label_idx=label_idx\n",
120
    "\n",
121
    "    )\n",
122
    "    \n",
123
    "    dict_meshes[label_idx].create_mesh(smooth_image_var=3.0) # I ADDED MORE SMOOTHING FOR THESE\n",
124
    "    dict_meshes[label_idx].resample_surface(clusters=10000)"
125
   ]
126
  },
127
  {
128
   "cell_type": "markdown",
129
   "id": "1cc29d8d",
130
   "metadata": {},
131
   "source": [
132
    "### Save each mesh"
133
   ]
134
  },
135
  {
136
   "cell_type": "code",
137
   "execution_count": 12,
138
   "id": "f5d6738e",
139
   "metadata": {},
140
   "outputs": [],
141
   "source": [
142
    "for key, mesh in dict_meshes.items():\n",
143
    "    mesh.save_mesh(os.path.join(location_save, f'muscle_mesh{key}.vtk')) # Save vtk version\n",
144
    "    mesh.save_mesh(os.path.join(location_save, f'muscle_mesh{key}.stl')) # Save stl version"
145
   ]
146
  },
147
  {
148
   "cell_type": "markdown",
149
   "id": "b0a66862",
150
   "metadata": {},
151
   "source": [
152
    "# Below is the viewer that should work to see these in jupyter notebooks\n",
153
    "- This wasnt working on my computer for some reason. "
154
   ]
155
  },
156
  {
157
   "cell_type": "code",
158
   "execution_count": 13,
159
   "id": "b9f350ba",
160
   "metadata": {},
161
   "outputs": [],
162
   "source": [
163
    "from itkwidgets import view"
164
   ]
165
  },
166
  {
167
   "cell_type": "code",
168
   "execution_count": 14,
169
   "id": "505700e8",
170
   "metadata": {},
171
   "outputs": [
172
    {
173
     "data": {
174
      "text/plain": [
175
       "array([ 0, 14, 16, 18, 20, 22, 24, 26], dtype=uint16)"
176
      ]
177
     },
178
     "execution_count": 14,
179
     "metadata": {},
180
     "output_type": "execute_result"
181
    }
182
   ],
183
   "source": [
184
    "seg_labels"
185
   ]
186
  },
187
  {
188
   "cell_type": "code",
189
   "execution_count": 15,
190
   "id": "52169ea2",
191
   "metadata": {},
192
   "outputs": [
193
    {
194
     "data": {
195
      "application/vnd.jupyter.widget-view+json": {
196
       "model_id": "5f7605d7f4274c78bbcbcb884feb90f8",
197
       "version_major": 2,
198
       "version_minor": 0
199
      },
200
      "text/plain": [
201
       "Viewer(geometries=[{'vtkClass': 'vtkPolyData', 'points': {'vtkClass': 'vtkPoints', 'name': '_points', 'numberO…"
202
      ]
203
     },
204
     "metadata": {},
205
     "output_type": "display_data"
206
    }
207
   ],
208
   "source": [
209
    "# view expects geometries to be in a list: \n",
210
    "geometries = [\n",
211
    "    dict_meshes[14].mesh  # the Mesh object has the actual mesh inside of it at Mesh.mesh\n",
212
    "]\n",
213
    "view(geometries=geometries)\n"
214
   ]
215
  },
216
  {
217
   "cell_type": "markdown",
218
   "id": "9c6b9c5a",
219
   "metadata": {},
220
   "source": [
221
    "### Show multiple meshes at once: "
222
   ]
223
  },
224
  {
225
   "cell_type": "code",
226
   "execution_count": 16,
227
   "id": "ddf2bed6",
228
   "metadata": {},
229
   "outputs": [
230
    {
231
     "data": {
232
      "application/vnd.jupyter.widget-view+json": {
233
       "model_id": "b9275aa069ce432b9ab363874895c7d4",
234
       "version_major": 2,
235
       "version_minor": 0
236
      },
237
      "text/plain": [
238
       "Viewer(geometries=[{'vtkClass': 'vtkPolyData', 'points': {'vtkClass': 'vtkPoints', 'name': '_points', 'numberO…"
239
      ]
240
     },
241
     "metadata": {},
242
     "output_type": "display_data"
243
    }
244
   ],
245
   "source": [
246
    "# view expects geometries to be in a list: \n",
247
    "geometries = [\n",
248
    "    dict_meshes[14].mesh,  # the Mesh object has the actual mesh inside of it at Mesh.mesh\n",
249
    "    dict_meshes[16].mesh,\n",
250
    "    dict_meshes[18].mesh,\n",
251
    "    dict_meshes[20].mesh,\n",
252
    "    dict_meshes[22].mesh,\n",
253
    "    dict_meshes[24].mesh,\n",
254
    "    dict_meshes[26].mesh\n",
255
    "]\n",
256
    "view(geometries=geometries)"
257
   ]
258
  },
259
  {
260
   "cell_type": "code",
261
   "execution_count": null,
262
   "id": "397e00d4",
263
   "metadata": {},
264
   "outputs": [],
265
   "source": []
266
  }
267
 ],
268
 "metadata": {
269
  "kernelspec": {
270
   "display_name": "Python 3 (ipykernel)",
271
   "language": "python",
272
   "name": "python3"
273
  },
274
  "language_info": {
275
   "codemirror_mode": {
276
    "name": "ipython",
277
    "version": 3
278
   },
279
   "file_extension": ".py",
280
   "mimetype": "text/x-python",
281
   "name": "python",
282
   "nbconvert_exporter": "python",
283
   "pygments_lexer": "ipython3",
284
   "version": "3.8.12"
285
  }
286
 },
287
 "nbformat": 4,
288
 "nbformat_minor": 5
289
}