ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Core/Mesh/ExtractIsoSurface/Code.py
1 #!/usr/bin/env python
2 
3 # ==========================================================================
4 #
5 # Copyright Insight Software Consortium
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License")
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # https://www.apache.org/licenses/LICENSE-2.0.txt
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # ==========================================================================*/
20 
21 import itk
22 import numpy as np
23 import argparse
24 
25 parser = argparse.ArgumentParser(description="Extract an isosurface.")
26 parser.add_argument("input_image")
27 parser.add_argument("output_mesh")
28 parser.add_argument("lower_threshold", type=int)
29 parser.add_argument("upper_threshold", type=int)
30 args = parser.parse_args()
31 
32 input_image = itk.imread(args.input_image)
33 
34 thresholded = itk.binary_threshold_image_filter(
35  input_image,
36  lower_threshold=args.lower_threshold,
37  upper_threshold=args.upper_threshold,
38  outside_value=0,
39  inside_value=255,
40 )
41 
42 output_mesh = itk.binary_mask3_d_mesh_source(thresholded, object_value=255)
43 
44 itk.meshwrite(output_mesh, args.output_mesh)