|
a |
|
b/docs/mesh/createMesh.html |
|
|
1 |
<!doctype html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" /> |
|
|
6 |
<meta name="generator" content="pdoc 0.10.0" /> |
|
|
7 |
<title>pymskt.mesh.createMesh API documentation</title> |
|
|
8 |
<meta name="description" content="" /> |
|
|
9 |
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin> |
|
|
10 |
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin> |
|
|
11 |
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin> |
|
|
12 |
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style> |
|
|
13 |
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style> |
|
|
14 |
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style> |
|
|
15 |
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script> |
|
|
16 |
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script> |
|
|
17 |
</head> |
|
|
18 |
<body> |
|
|
19 |
<main> |
|
|
20 |
<article id="content"> |
|
|
21 |
<header> |
|
|
22 |
<h1 class="title">Module <code>pymskt.mesh.createMesh</code></h1> |
|
|
23 |
</header> |
|
|
24 |
<section id="section-intro"> |
|
|
25 |
<details class="source"> |
|
|
26 |
<summary> |
|
|
27 |
<span>Expand source code</span> |
|
|
28 |
</summary> |
|
|
29 |
<pre><code class="python">import os |
|
|
30 |
import vtk |
|
|
31 |
import SimpleITK as sitk |
|
|
32 |
|
|
|
33 |
import pymskt.image as msktimage |
|
|
34 |
import pymskt.mesh.meshTransform as meshTransform |
|
|
35 |
from pymskt.utils import safely_delete_tmp_file |
|
|
36 |
|
|
|
37 |
def discrete_marching_cubes(vtk_image_reader, |
|
|
38 |
n_labels=1, |
|
|
39 |
start_label=1, |
|
|
40 |
end_label=1, |
|
|
41 |
compute_normals_on=True, |
|
|
42 |
return_polydata=True |
|
|
43 |
): |
|
|
44 |
""" |
|
|
45 |
Compute dmc on segmentation image. |
|
|
46 |
Creates a surface mesh (polydata) that closely covers binary (discrete) segmentations. |
|
|
47 |
|
|
|
48 |
Parameters |
|
|
49 |
---------- |
|
|
50 |
vtk_image_reader : vtk.Filter |
|
|
51 |
VTK Filter pipeline to apply discrete marching cubes to. |
|
|
52 |
n_labels : int, optional |
|
|
53 |
Number of labes to create mesh for, by default 1 |
|
|
54 |
start_label : int, optional |
|
|
55 |
Starting index of labels to mesh, by default 1 |
|
|
56 |
end_label : int, optional |
|
|
57 |
Ending index of labels to mesh, by default 1 |
|
|
58 |
compute_normals_on : bool, optional |
|
|
59 |
Calculate normals to surface, by default True |
|
|
60 |
return_polydata : bool, optional |
|
|
61 |
Whether to return a vtk.polydata or not (`vtk.Filter` pipeline instead), by default True |
|
|
62 |
|
|
|
63 |
Returns |
|
|
64 |
------- |
|
|
65 |
vtk.Filter Pipeline |
|
|
66 |
Returns a pipeline which more functions can be chained too - this improves performance. |
|
|
67 |
|
|
|
68 |
OR |
|
|
69 |
|
|
|
70 |
vtk.Polydata |
|
|
71 |
Returns a polydata (surface mesh). |
|
|
72 |
|
|
|
73 |
""" |
|
|
74 |
|
|
|
75 |
dmc = vtk.vtkDiscreteMarchingCubes() |
|
|
76 |
dmc.SetInputConnection(vtk_image_reader.GetOutputPort()) |
|
|
77 |
if compute_normals_on is True: |
|
|
78 |
dmc.ComputeNormalsOn() |
|
|
79 |
dmc.GenerateValues(n_labels, start_label, end_label) |
|
|
80 |
dmc.Update() |
|
|
81 |
|
|
|
82 |
if return_polydata is True: |
|
|
83 |
return dmc.GetOutput() |
|
|
84 |
elif return_polydata is False: |
|
|
85 |
return dmc |
|
|
86 |
|
|
|
87 |
|
|
|
88 |
def continuous_marching_cubes(vtk_image_reader, |
|
|
89 |
threshold=0.5, |
|
|
90 |
compute_normals_on=True, |
|
|
91 |
compute_gradients_on=True, |
|
|
92 |
return_polydata=True): |
|
|
93 |
""" |
|
|
94 |
- Compute a continuous marching cubes on a segmentation mask. |
|
|
95 |
- Enables defining the surface based on a contour set to a floating point cutoff. |
|
|
96 |
|
|
|
97 |
|
|
|
98 |
Parameters |
|
|
99 |
---------- |
|
|
100 |
vtk_image_reader : vtk.Filter |
|
|
101 |
This is the output of a `vtk.Filter` from a previous step. E.g., output of pymskt.image.read_nrrd(). |
|
|
102 |
|
|
|
103 |
threshold : float, optional |
|
|
104 |
Floating point value to create surface mesh, by default 0.5 |
|
|
105 |
compute_normals_on : bool, optional |
|
|
106 |
Whether or not to compute surface normals for mesh, by default True |
|
|
107 |
compute_gradients_on : bool, optional |
|
|
108 |
Whether or not to compute gradients over mesh surface, by default True |
|
|
109 |
return_polydata : bool, optional |
|
|
110 |
Whether to return a vtk.polydata or not (VTK filter pipeline instead e.g., `mc`), by default True |
|
|
111 |
|
|
|
112 |
Returns |
|
|
113 |
------- |
|
|
114 |
vtk.Filter Pipeline |
|
|
115 |
Returns a pipeline which more functions can be chained too - this improves performance. |
|
|
116 |
|
|
|
117 |
OR |
|
|
118 |
|
|
|
119 |
vtk.Polydata |
|
|
120 |
Returns a polydata (surface mesh). |
|
|
121 |
""" |
|
|
122 |
mc = vtk.vtkMarchingContourFilter() |
|
|
123 |
mc.SetInputConnection(vtk_image_reader.GetOutputPort()) |
|
|
124 |
if compute_normals_on is True: |
|
|
125 |
mc.ComputeNormalsOn() |
|
|
126 |
elif compute_normals_on is False: |
|
|
127 |
mc.ComputeNormalsOff() |
|
|
128 |
|
|
|
129 |
if compute_gradients_on is True: |
|
|
130 |
mc.ComputeGradientsOn() |
|
|
131 |
elif compute_gradients_on is False: |
|
|
132 |
mc.ComputeGradientsOff() |
|
|
133 |
mc.SetValue(0, threshold) |
|
|
134 |
mc.Update() |
|
|
135 |
|
|
|
136 |
if return_polydata is True: |
|
|
137 |
mesh = mc.GetOutput() |
|
|
138 |
return mesh |
|
|
139 |
elif return_polydata is False: |
|
|
140 |
return mc |
|
|
141 |
|
|
|
142 |
def create_surface_mesh(seg_image, |
|
|
143 |
label_idx, |
|
|
144 |
image_smooth_var, |
|
|
145 |
loc_tmp_save='/tmp', |
|
|
146 |
tmp_filename='temp_smoothed_bone.nrrd', |
|
|
147 |
copy_image_transform=True, |
|
|
148 |
mc_threshold=0.5, |
|
|
149 |
filter_binary_image=True): |
|
|
150 |
""" |
|
|
151 |
Create surface mesh. |
|
|
152 |
Option to filter binary image to get smoother surface representation. |
|
|
153 |
|
|
|
154 |
Parameters |
|
|
155 |
---------- |
|
|
156 |
seg_image : SimpleITK.Image |
|
|
157 |
Segmentation image to be filtered and meshed with marching cubes. |
|
|
158 |
label_idx : int |
|
|
159 |
What anatomical label to be meshed. |
|
|
160 |
image_smooth_var : float |
|
|
161 |
Variance to apply a gaussian smoothing function to. |
|
|
162 |
loc_tmp_save : str, optional |
|
|
163 |
Location to save temporary files for passing SimpleITK.Image to vtk functions, by default '/tmp' |
|
|
164 |
tmp_filename : str, optional |
|
|
165 |
Filename of saved temporary file, by default 'temp_smoothed_bone.nrrd' |
|
|
166 |
copy_image_transform : bool, optional |
|
|
167 |
Whether or not to apply image transform to final mesh or to leave it at origin, by default True |
|
|
168 |
mc_threshold : float, optional |
|
|
169 |
What floating point value to create surface mesh at, by default 0.5 |
|
|
170 |
filter_binary_image : bool, optional |
|
|
171 |
Should the binary image be filtered (smoothed) or not. |
|
|
172 |
|
|
|
173 |
Returns |
|
|
174 |
------- |
|
|
175 |
vtk.Polydata |
|
|
176 |
Surface mesh created using a continuous cutoff `mc_threshold` after applying |
|
|
177 |
gaussian smoothing with variance = `image_smooth_var`. |
|
|
178 |
""" |
|
|
179 |
|
|
|
180 |
# Set border of segmentation to 0 so that segs are all closed. |
|
|
181 |
seg_image = msktimage.set_seg_border_to_zeros(seg_image, border_size=1) |
|
|
182 |
|
|
|
183 |
if filter_binary_image is True: |
|
|
184 |
# smooth/filter the image to get a better surface. |
|
|
185 |
seg_image = msktimage.smooth_image(seg_image, label_idx, image_smooth_var) |
|
|
186 |
else: |
|
|
187 |
seg_image = msktimage.binarize_segmentation_image(seg_image, label_idx) |
|
|
188 |
# save filtered image to disk so can read it in using vtk nrrd reader |
|
|
189 |
sitk.WriteImage(seg_image, os.path.join(loc_tmp_save, tmp_filename)) |
|
|
190 |
smoothed_nrrd_reader = msktimage.read_nrrd(os.path.join(loc_tmp_save, tmp_filename), |
|
|
191 |
set_origin_zero=True) |
|
|
192 |
# create the mesh using continuous marching cubes applied to the smoothed binary image. |
|
|
193 |
smooth_mesh = continuous_marching_cubes(smoothed_nrrd_reader, threshold=mc_threshold) |
|
|
194 |
|
|
|
195 |
if copy_image_transform is True: |
|
|
196 |
# copy image transofrm to the image to the mesh so that when viewed (e.g. in 3D Slicer) it is aligned with image |
|
|
197 |
smooth_mesh = meshTransform.copy_image_transform_to_mesh(smooth_mesh, seg_image) |
|
|
198 |
|
|
|
199 |
# Delete tmp files |
|
|
200 |
safely_delete_tmp_file(loc_tmp_save, |
|
|
201 |
tmp_filename) |
|
|
202 |
return smooth_mesh</code></pre> |
|
|
203 |
</details> |
|
|
204 |
</section> |
|
|
205 |
<section> |
|
|
206 |
</section> |
|
|
207 |
<section> |
|
|
208 |
</section> |
|
|
209 |
<section> |
|
|
210 |
<h2 class="section-title" id="header-functions">Functions</h2> |
|
|
211 |
<dl> |
|
|
212 |
<dt id="pymskt.mesh.createMesh.continuous_marching_cubes"><code class="name flex"> |
|
|
213 |
<span>def <span class="ident">continuous_marching_cubes</span></span>(<span>vtk_image_reader, threshold=0.5, compute_normals_on=True, compute_gradients_on=True, return_polydata=True)</span> |
|
|
214 |
</code></dt> |
|
|
215 |
<dd> |
|
|
216 |
<div class="desc"><ul> |
|
|
217 |
<li>Compute a continuous marching cubes on a segmentation mask. </li> |
|
|
218 |
<li>Enables defining the surface based on a contour set to a floating point cutoff. </li> |
|
|
219 |
</ul> |
|
|
220 |
<h2 id="parameters">Parameters</h2> |
|
|
221 |
<dl> |
|
|
222 |
<dt><strong><code>vtk_image_reader</code></strong> : <code>vtk.Filter</code></dt> |
|
|
223 |
<dd>This is the output of a <code>vtk.Filter</code> from a previous step. E.g., output of pymskt.image.read_nrrd().</dd> |
|
|
224 |
<dt><strong><code>threshold</code></strong> : <code>float</code>, optional</dt> |
|
|
225 |
<dd>Floating point value to create surface mesh, by default 0.5</dd> |
|
|
226 |
<dt><strong><code>compute_normals_on</code></strong> : <code>bool</code>, optional</dt> |
|
|
227 |
<dd>Whether or not to compute surface normals for mesh, by default True</dd> |
|
|
228 |
<dt><strong><code>compute_gradients_on</code></strong> : <code>bool</code>, optional</dt> |
|
|
229 |
<dd>Whether or not to compute gradients over mesh surface, by default True</dd> |
|
|
230 |
<dt><strong><code>return_polydata</code></strong> : <code>bool</code>, optional</dt> |
|
|
231 |
<dd>Whether to return a vtk.polydata or not (VTK filter pipeline instead e.g., <code>mc</code>), by default True</dd> |
|
|
232 |
</dl> |
|
|
233 |
<h2 id="returns">Returns</h2> |
|
|
234 |
<dl> |
|
|
235 |
<dt><code>vtk.Filter Pipeline</code></dt> |
|
|
236 |
<dd>Returns a pipeline which more functions can be chained too - this improves performance.</dd> |
|
|
237 |
<dt><code>OR</code></dt> |
|
|
238 |
<dd> </dd> |
|
|
239 |
<dt><code>vtk.Polydata</code></dt> |
|
|
240 |
<dd>Returns a polydata (surface mesh).</dd> |
|
|
241 |
</dl></div> |
|
|
242 |
<details class="source"> |
|
|
243 |
<summary> |
|
|
244 |
<span>Expand source code</span> |
|
|
245 |
</summary> |
|
|
246 |
<pre><code class="python">def continuous_marching_cubes(vtk_image_reader, |
|
|
247 |
threshold=0.5, |
|
|
248 |
compute_normals_on=True, |
|
|
249 |
compute_gradients_on=True, |
|
|
250 |
return_polydata=True): |
|
|
251 |
""" |
|
|
252 |
- Compute a continuous marching cubes on a segmentation mask. |
|
|
253 |
- Enables defining the surface based on a contour set to a floating point cutoff. |
|
|
254 |
|
|
|
255 |
|
|
|
256 |
Parameters |
|
|
257 |
---------- |
|
|
258 |
vtk_image_reader : vtk.Filter |
|
|
259 |
This is the output of a `vtk.Filter` from a previous step. E.g., output of pymskt.image.read_nrrd(). |
|
|
260 |
|
|
|
261 |
threshold : float, optional |
|
|
262 |
Floating point value to create surface mesh, by default 0.5 |
|
|
263 |
compute_normals_on : bool, optional |
|
|
264 |
Whether or not to compute surface normals for mesh, by default True |
|
|
265 |
compute_gradients_on : bool, optional |
|
|
266 |
Whether or not to compute gradients over mesh surface, by default True |
|
|
267 |
return_polydata : bool, optional |
|
|
268 |
Whether to return a vtk.polydata or not (VTK filter pipeline instead e.g., `mc`), by default True |
|
|
269 |
|
|
|
270 |
Returns |
|
|
271 |
------- |
|
|
272 |
vtk.Filter Pipeline |
|
|
273 |
Returns a pipeline which more functions can be chained too - this improves performance. |
|
|
274 |
|
|
|
275 |
OR |
|
|
276 |
|
|
|
277 |
vtk.Polydata |
|
|
278 |
Returns a polydata (surface mesh). |
|
|
279 |
""" |
|
|
280 |
mc = vtk.vtkMarchingContourFilter() |
|
|
281 |
mc.SetInputConnection(vtk_image_reader.GetOutputPort()) |
|
|
282 |
if compute_normals_on is True: |
|
|
283 |
mc.ComputeNormalsOn() |
|
|
284 |
elif compute_normals_on is False: |
|
|
285 |
mc.ComputeNormalsOff() |
|
|
286 |
|
|
|
287 |
if compute_gradients_on is True: |
|
|
288 |
mc.ComputeGradientsOn() |
|
|
289 |
elif compute_gradients_on is False: |
|
|
290 |
mc.ComputeGradientsOff() |
|
|
291 |
mc.SetValue(0, threshold) |
|
|
292 |
mc.Update() |
|
|
293 |
|
|
|
294 |
if return_polydata is True: |
|
|
295 |
mesh = mc.GetOutput() |
|
|
296 |
return mesh |
|
|
297 |
elif return_polydata is False: |
|
|
298 |
return mc</code></pre> |
|
|
299 |
</details> |
|
|
300 |
</dd> |
|
|
301 |
<dt id="pymskt.mesh.createMesh.create_surface_mesh"><code class="name flex"> |
|
|
302 |
<span>def <span class="ident">create_surface_mesh</span></span>(<span>seg_image, label_idx, image_smooth_var, loc_tmp_save='/tmp', tmp_filename='temp_smoothed_bone.nrrd', copy_image_transform=True, mc_threshold=0.5, filter_binary_image=True)</span> |
|
|
303 |
</code></dt> |
|
|
304 |
<dd> |
|
|
305 |
<div class="desc"><p>Create surface mesh. |
|
|
306 |
Option to filter binary image to get smoother surface representation.</p> |
|
|
307 |
<h2 id="parameters">Parameters</h2> |
|
|
308 |
<dl> |
|
|
309 |
<dt><strong><code>seg_image</code></strong> : <code>SimpleITK.Image</code></dt> |
|
|
310 |
<dd>Segmentation image to be filtered and meshed with marching cubes.</dd> |
|
|
311 |
<dt><strong><code>label_idx</code></strong> : <code>int</code></dt> |
|
|
312 |
<dd>What anatomical label to be meshed.</dd> |
|
|
313 |
<dt><strong><code>image_smooth_var</code></strong> : <code>float</code></dt> |
|
|
314 |
<dd>Variance to apply a gaussian smoothing function to.</dd> |
|
|
315 |
<dt><strong><code>loc_tmp_save</code></strong> : <code>str</code>, optional</dt> |
|
|
316 |
<dd>Location to save temporary files for passing SimpleITK.Image to vtk functions, by default '/tmp'</dd> |
|
|
317 |
<dt><strong><code>tmp_filename</code></strong> : <code>str</code>, optional</dt> |
|
|
318 |
<dd>Filename of saved temporary file, by default 'temp_smoothed_bone.nrrd'</dd> |
|
|
319 |
<dt><strong><code>copy_image_transform</code></strong> : <code>bool</code>, optional</dt> |
|
|
320 |
<dd>Whether or not to apply image transform to final mesh or to leave it at origin, by default True</dd> |
|
|
321 |
<dt><strong><code>mc_threshold</code></strong> : <code>float</code>, optional</dt> |
|
|
322 |
<dd>What floating point value to create surface mesh at, by default 0.5</dd> |
|
|
323 |
<dt><strong><code>filter_binary_image</code></strong> : <code>bool</code>, optional</dt> |
|
|
324 |
<dd>Should the binary image be filtered (smoothed) or not.</dd> |
|
|
325 |
</dl> |
|
|
326 |
<h2 id="returns">Returns</h2> |
|
|
327 |
<dl> |
|
|
328 |
<dt><code>vtk.Polydata</code></dt> |
|
|
329 |
<dd>Surface mesh created using a continuous cutoff <code>mc_threshold</code> after applying |
|
|
330 |
gaussian smoothing with variance = <code>image_smooth_var</code>.</dd> |
|
|
331 |
</dl></div> |
|
|
332 |
<details class="source"> |
|
|
333 |
<summary> |
|
|
334 |
<span>Expand source code</span> |
|
|
335 |
</summary> |
|
|
336 |
<pre><code class="python">def create_surface_mesh(seg_image, |
|
|
337 |
label_idx, |
|
|
338 |
image_smooth_var, |
|
|
339 |
loc_tmp_save='/tmp', |
|
|
340 |
tmp_filename='temp_smoothed_bone.nrrd', |
|
|
341 |
copy_image_transform=True, |
|
|
342 |
mc_threshold=0.5, |
|
|
343 |
filter_binary_image=True): |
|
|
344 |
""" |
|
|
345 |
Create surface mesh. |
|
|
346 |
Option to filter binary image to get smoother surface representation. |
|
|
347 |
|
|
|
348 |
Parameters |
|
|
349 |
---------- |
|
|
350 |
seg_image : SimpleITK.Image |
|
|
351 |
Segmentation image to be filtered and meshed with marching cubes. |
|
|
352 |
label_idx : int |
|
|
353 |
What anatomical label to be meshed. |
|
|
354 |
image_smooth_var : float |
|
|
355 |
Variance to apply a gaussian smoothing function to. |
|
|
356 |
loc_tmp_save : str, optional |
|
|
357 |
Location to save temporary files for passing SimpleITK.Image to vtk functions, by default '/tmp' |
|
|
358 |
tmp_filename : str, optional |
|
|
359 |
Filename of saved temporary file, by default 'temp_smoothed_bone.nrrd' |
|
|
360 |
copy_image_transform : bool, optional |
|
|
361 |
Whether or not to apply image transform to final mesh or to leave it at origin, by default True |
|
|
362 |
mc_threshold : float, optional |
|
|
363 |
What floating point value to create surface mesh at, by default 0.5 |
|
|
364 |
filter_binary_image : bool, optional |
|
|
365 |
Should the binary image be filtered (smoothed) or not. |
|
|
366 |
|
|
|
367 |
Returns |
|
|
368 |
------- |
|
|
369 |
vtk.Polydata |
|
|
370 |
Surface mesh created using a continuous cutoff `mc_threshold` after applying |
|
|
371 |
gaussian smoothing with variance = `image_smooth_var`. |
|
|
372 |
""" |
|
|
373 |
|
|
|
374 |
# Set border of segmentation to 0 so that segs are all closed. |
|
|
375 |
seg_image = msktimage.set_seg_border_to_zeros(seg_image, border_size=1) |
|
|
376 |
|
|
|
377 |
if filter_binary_image is True: |
|
|
378 |
# smooth/filter the image to get a better surface. |
|
|
379 |
seg_image = msktimage.smooth_image(seg_image, label_idx, image_smooth_var) |
|
|
380 |
else: |
|
|
381 |
seg_image = msktimage.binarize_segmentation_image(seg_image, label_idx) |
|
|
382 |
# save filtered image to disk so can read it in using vtk nrrd reader |
|
|
383 |
sitk.WriteImage(seg_image, os.path.join(loc_tmp_save, tmp_filename)) |
|
|
384 |
smoothed_nrrd_reader = msktimage.read_nrrd(os.path.join(loc_tmp_save, tmp_filename), |
|
|
385 |
set_origin_zero=True) |
|
|
386 |
# create the mesh using continuous marching cubes applied to the smoothed binary image. |
|
|
387 |
smooth_mesh = continuous_marching_cubes(smoothed_nrrd_reader, threshold=mc_threshold) |
|
|
388 |
|
|
|
389 |
if copy_image_transform is True: |
|
|
390 |
# copy image transofrm to the image to the mesh so that when viewed (e.g. in 3D Slicer) it is aligned with image |
|
|
391 |
smooth_mesh = meshTransform.copy_image_transform_to_mesh(smooth_mesh, seg_image) |
|
|
392 |
|
|
|
393 |
# Delete tmp files |
|
|
394 |
safely_delete_tmp_file(loc_tmp_save, |
|
|
395 |
tmp_filename) |
|
|
396 |
return smooth_mesh</code></pre> |
|
|
397 |
</details> |
|
|
398 |
</dd> |
|
|
399 |
<dt id="pymskt.mesh.createMesh.discrete_marching_cubes"><code class="name flex"> |
|
|
400 |
<span>def <span class="ident">discrete_marching_cubes</span></span>(<span>vtk_image_reader, n_labels=1, start_label=1, end_label=1, compute_normals_on=True, return_polydata=True)</span> |
|
|
401 |
</code></dt> |
|
|
402 |
<dd> |
|
|
403 |
<div class="desc"><p>Compute dmc on segmentation image. |
|
|
404 |
Creates a surface mesh (polydata) that closely covers binary (discrete) segmentations.</p> |
|
|
405 |
<h2 id="parameters">Parameters</h2> |
|
|
406 |
<dl> |
|
|
407 |
<dt><strong><code>vtk_image_reader</code></strong> : <code>vtk.Filter</code></dt> |
|
|
408 |
<dd>VTK Filter pipeline to apply discrete marching cubes to.</dd> |
|
|
409 |
<dt><strong><code>n_labels</code></strong> : <code>int</code>, optional</dt> |
|
|
410 |
<dd>Number of labes to create mesh for, by default 1</dd> |
|
|
411 |
<dt><strong><code>start_label</code></strong> : <code>int</code>, optional</dt> |
|
|
412 |
<dd>Starting index of labels to mesh, by default 1</dd> |
|
|
413 |
<dt><strong><code>end_label</code></strong> : <code>int</code>, optional</dt> |
|
|
414 |
<dd>Ending index of labels to mesh, by default 1</dd> |
|
|
415 |
<dt><strong><code>compute_normals_on</code></strong> : <code>bool</code>, optional</dt> |
|
|
416 |
<dd>Calculate normals to surface, by default True</dd> |
|
|
417 |
<dt><strong><code>return_polydata</code></strong> : <code>bool</code>, optional</dt> |
|
|
418 |
<dd>Whether to return a vtk.polydata or not (<code>vtk.Filter</code> pipeline instead), by default True</dd> |
|
|
419 |
</dl> |
|
|
420 |
<h2 id="returns">Returns</h2> |
|
|
421 |
<dl> |
|
|
422 |
<dt><code>vtk.Filter Pipeline</code></dt> |
|
|
423 |
<dd>Returns a pipeline which more functions can be chained too - this improves performance.</dd> |
|
|
424 |
<dt><code>OR</code></dt> |
|
|
425 |
<dd> </dd> |
|
|
426 |
<dt><code>vtk.Polydata</code></dt> |
|
|
427 |
<dd>Returns a polydata (surface mesh).</dd> |
|
|
428 |
</dl></div> |
|
|
429 |
<details class="source"> |
|
|
430 |
<summary> |
|
|
431 |
<span>Expand source code</span> |
|
|
432 |
</summary> |
|
|
433 |
<pre><code class="python">def discrete_marching_cubes(vtk_image_reader, |
|
|
434 |
n_labels=1, |
|
|
435 |
start_label=1, |
|
|
436 |
end_label=1, |
|
|
437 |
compute_normals_on=True, |
|
|
438 |
return_polydata=True |
|
|
439 |
): |
|
|
440 |
""" |
|
|
441 |
Compute dmc on segmentation image. |
|
|
442 |
Creates a surface mesh (polydata) that closely covers binary (discrete) segmentations. |
|
|
443 |
|
|
|
444 |
Parameters |
|
|
445 |
---------- |
|
|
446 |
vtk_image_reader : vtk.Filter |
|
|
447 |
VTK Filter pipeline to apply discrete marching cubes to. |
|
|
448 |
n_labels : int, optional |
|
|
449 |
Number of labes to create mesh for, by default 1 |
|
|
450 |
start_label : int, optional |
|
|
451 |
Starting index of labels to mesh, by default 1 |
|
|
452 |
end_label : int, optional |
|
|
453 |
Ending index of labels to mesh, by default 1 |
|
|
454 |
compute_normals_on : bool, optional |
|
|
455 |
Calculate normals to surface, by default True |
|
|
456 |
return_polydata : bool, optional |
|
|
457 |
Whether to return a vtk.polydata or not (`vtk.Filter` pipeline instead), by default True |
|
|
458 |
|
|
|
459 |
Returns |
|
|
460 |
------- |
|
|
461 |
vtk.Filter Pipeline |
|
|
462 |
Returns a pipeline which more functions can be chained too - this improves performance. |
|
|
463 |
|
|
|
464 |
OR |
|
|
465 |
|
|
|
466 |
vtk.Polydata |
|
|
467 |
Returns a polydata (surface mesh). |
|
|
468 |
|
|
|
469 |
""" |
|
|
470 |
|
|
|
471 |
dmc = vtk.vtkDiscreteMarchingCubes() |
|
|
472 |
dmc.SetInputConnection(vtk_image_reader.GetOutputPort()) |
|
|
473 |
if compute_normals_on is True: |
|
|
474 |
dmc.ComputeNormalsOn() |
|
|
475 |
dmc.GenerateValues(n_labels, start_label, end_label) |
|
|
476 |
dmc.Update() |
|
|
477 |
|
|
|
478 |
if return_polydata is True: |
|
|
479 |
return dmc.GetOutput() |
|
|
480 |
elif return_polydata is False: |
|
|
481 |
return dmc</code></pre> |
|
|
482 |
</details> |
|
|
483 |
</dd> |
|
|
484 |
</dl> |
|
|
485 |
</section> |
|
|
486 |
<section> |
|
|
487 |
</section> |
|
|
488 |
</article> |
|
|
489 |
<nav id="sidebar"> |
|
|
490 |
<h1>Index</h1> |
|
|
491 |
<div class="toc"> |
|
|
492 |
<ul></ul> |
|
|
493 |
</div> |
|
|
494 |
<ul id="index"> |
|
|
495 |
<li><h3>Super-module</h3> |
|
|
496 |
<ul> |
|
|
497 |
<li><code><a title="pymskt.mesh" href="index.html">pymskt.mesh</a></code></li> |
|
|
498 |
</ul> |
|
|
499 |
</li> |
|
|
500 |
<li><h3><a href="#header-functions">Functions</a></h3> |
|
|
501 |
<ul class=""> |
|
|
502 |
<li><code><a title="pymskt.mesh.createMesh.continuous_marching_cubes" href="#pymskt.mesh.createMesh.continuous_marching_cubes">continuous_marching_cubes</a></code></li> |
|
|
503 |
<li><code><a title="pymskt.mesh.createMesh.create_surface_mesh" href="#pymskt.mesh.createMesh.create_surface_mesh">create_surface_mesh</a></code></li> |
|
|
504 |
<li><code><a title="pymskt.mesh.createMesh.discrete_marching_cubes" href="#pymskt.mesh.createMesh.discrete_marching_cubes">discrete_marching_cubes</a></code></li> |
|
|
505 |
</ul> |
|
|
506 |
</li> |
|
|
507 |
</ul> |
|
|
508 |
</nav> |
|
|
509 |
</main> |
|
|
510 |
<footer id="footer"> |
|
|
511 |
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p> |
|
|
512 |
</footer> |
|
|
513 |
</body> |
|
|
514 |
</html> |