Skip to content

Commit 5fb149d

Browse files
authored
Merge pull request #416 from thewtex/binary-mask-python
ENH: Add Python version of ExtractIsoSurface
2 parents 724543b + a312fd3 commit 5fb149d

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/Core/Mesh/ExtractIsoSurface/CMakeLists.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,31 @@ install(TARGETS ${PROJECT_NAME}
1313
COMPONENT Runtime
1414
)
1515

16-
install(FILES Code.cxx CMakeLists.txt
16+
install(FILES Code.cxx CMakeLists.txt Code.py
1717
DESTINATION share/ITKSphinxExamples/Code/Core/Mesh/ExtractIsoSurface
1818
COMPONENT Code
1919
)
2020

21+
set(input_volume ${CMAKE_CURRENT_BINARY_DIR}/HeadMRVolume.mha)
22+
set(output_mesh ${CMAKE_CURRENT_BINARY_DIR}/Output.vtk)
23+
set(lower_threshold 64)
24+
set(upper_threshold 255)
25+
2126
enable_testing()
2227
add_test(NAME ExtractIsoSurfaceTest
2328
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}
24-
${CMAKE_CURRENT_BINARY_DIR}/HeadMRVolume.mha
25-
${CMAKE_CURRENT_BINARY_DIR}/Output.vtk
26-
64
27-
255
29+
${input_volume}
30+
${output_mesh}
31+
${lower_threshold}
32+
${upper_threshold}
2833
)
34+
35+
if(ITK_WRAP_PYTHON)
36+
add_test(NAME ExtractIsoSurfaceTestPython
37+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
38+
${input_volume}
39+
OutputPython.vtk
40+
${lower_threshold}
41+
${upper_threshold}
42+
)
43+
endif()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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)

0 commit comments

Comments
 (0)