Skip to content

Commit 72afc3e

Browse files
authored
Merge pull request #215 from thewtex/resample-a-scalar-image-python
2 parents eddbd28 + 81a5821 commit 72afc3e

File tree

10 files changed

+99
-35
lines changed

10 files changed

+99
-35
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ CMakeLists.txt.user*
2727
*.orig
2828

2929
*.DS_Store
30+
31+
# Jupyter notebooks
32+
.ipynb_checkpoints/

src/Filtering/ImageGrid/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ compare_to_baseline(
4444
BASELINE_PREFIX BrainProtonDensitySlice
4545
)
4646

47-
add_example( UpsampleOrDownsampleScalarImage )
47+
add_example( ResampleAScalarImage )
4848
compare_to_baseline(
49-
EXAMPLE_NAME UpsampleOrDownsampleScalarImage
49+
EXAMPLE_NAME ResampleAScalarImage
5050
BASELINE_PREFIX OutputBaseline
5151
)
5252

@@ -105,4 +105,4 @@ compare_to_baseline(EXAMPLE_NAME RunImageFilterOnRegionOfImage
105105
add_example(CropImageBySpecifyingRegion2)
106106
compare_to_baseline(EXAMPLE_NAME CropImageBySpecifyingRegion2
107107
BASELINE_PREFIX CropImageBySpecifyingRegion2
108-
)
108+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.10.2)
2+
3+
project(ResampleAScalarImage )
4+
5+
find_package(ITK REQUIRED)
6+
include(${ITK_USE_FILE})
7+
8+
add_executable(ResampleAScalarImage Code.cxx)
9+
target_link_libraries(ResampleAScalarImage ${ITK_LIBRARIES})
10+
11+
install(TARGETS ResampleAScalarImage
12+
DESTINATION bin/ITKExamples/Filtering/ImageGrid
13+
COMPONENT Runtime
14+
)
15+
16+
install(FILES Code.cxx CMakeLists.txt Code.py
17+
DESTINATION share/ITKExamples/Code/Filtering/ImageGrid/ResampleAScalarImage
18+
COMPONENT Code
19+
)
20+
21+
enable_testing()
22+
add_test(NAME ResampleAScalarImageTest
23+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ResampleAScalarImage
24+
${CMAKE_CURRENT_BINARY_DIR}/Gourds.png
25+
Output.png
26+
160
27+
120
28+
)
29+
30+
if(ITK_WRAP_PYTHON)
31+
add_test(NAME ResampleAScalarImageTestPython
32+
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py
33+
${CMAKE_CURRENT_BINARY_DIR}/Gourds.png
34+
OutputPython.png
35+
160
36+
120
37+
)
38+
endif()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright NumFOCUS
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0.txt
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import sys
18+
import itk
19+
20+
if len(sys.argv) != 5:
21+
print("Usage: " + sys.argv[0] + " <input_filename> <output_filename> <size_x> <size_y>")
22+
sys.exit(1)
23+
24+
input_filename = sys.argv[1]
25+
output_filename = sys.argv[2]
26+
output_size = [int(sys.argv[3]), int(sys.argv[4])]
27+
28+
input_image = itk.imread(input_filename)
29+
30+
input_spacing = itk.spacing(input_image)
31+
input_size = itk.size(input_image)
32+
dimension = input_image.GetImageDimension()
33+
output_spacing = [input_spacing[dim] * input_size[dim] / output_size[dim] for
34+
dim in range(dimension)]
35+
36+
transform = itk.IdentityTransform[itk.D, dimension].New()
37+
38+
output_image = itk.resample_image_filter(input_image,
39+
size=output_size,
40+
output_spacing=output_spacing,
41+
output_origin=itk.origin(input_image),
42+
transform=transform
43+
)
44+
45+
itk.imwrite(output_image, output_filename)

src/Filtering/ImageGrid/UpsampleOrDownsampleScalarImage/Documentation.rst renamed to src/Filtering/ImageGrid/ResampleAScalarImage/Documentation.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
Resample Scalar Image
2-
=====================
1+
Resample a Scalar Image
2+
=======================
33

44
.. index::
55
single: ResampleImageFilter
66

77
Synopsis
88
--------
99

10-
Resample one scalar image
10+
Resample a scalar image.
1111

1212

1313
Results
@@ -35,6 +35,12 @@ C++
3535
.. literalinclude:: Code.cxx
3636
:lines: 18-
3737

38+
Python
39+
......
40+
41+
.. literalinclude:: Code.py
42+
:language: python
43+
:lines: 1, 16-
3844

3945
Classes demonstrated
4046
--------------------

src/Filtering/ImageGrid/UpsampleOrDownsampleScalarImage/CMakeLists.txt

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Filtering/ImageGrid/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ ImageGrid
2424
Stack2DImagesInto3DImage/Documentation.rst
2525
TileImagesSideBySide/Documentation.rst
2626
UpsampleAnImage/Documentation.rst
27-
UpsampleOrDownsampleScalarImage/Documentation.rst
27+
ResampleAScalarImage/Documentation.rst
2828
WarpAnImageUsingADeformationField/Documentation.rst

0 commit comments

Comments
 (0)