Skip to content

Commit 523327e

Browse files
committed
DOC: Python Quick Start updates for ITK 5.2
1 parent edfff2b commit 523327e

12 files changed

+65
-63
lines changed

docs/Quick_start_guide.rst

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ Here is a simple python script that reads an image, applies a median image filte
2222

2323
.. literalinclude:: code/ReadMedianWrite.py
2424

25-
There are two ways to instantiate filters with ITKPython:
25+
In `itk`, filters are optimized at compile time for each image pixel type and
26+
image dimension. There are two ways to instantiate these filters with the `itk`
27+
Python wrapping:
2628

27-
- *Implicit (recommended)*: ITK type information is automatically detected from the data. Typed filter objects and images are implicitly created.
29+
- *Implicit (recommended)*: Type information is automatically detected from the data. Typed filter objects and images are implicitly created.
2830

2931
.. literalinclude:: code/ImplicitInstantiation.py
3032
:lines: 8-
@@ -38,23 +40,23 @@ Explicit instantiation of a median image filter:
3840

3941
Explicit instantiation of cast image filter:
4042

41-
.. literalinclude:: code/CastImageFilter.py
42-
:lines: 9-23
43+
.. literalinclude:: code/Cast.py
44+
:lines: 10-18
4345

4446
ITK Python types
4547
................
4648

47-
+---------------------+--------------------+
48-
| C++ type | Python type |
49-
+=====================+====================+
50-
| float | itk.F |
51-
+---------------------+--------------------+
52-
| double | itk.D |
53-
+---------------------+--------------------+
54-
| unsigned char | itk.UC |
55-
+---------------------+--------------------+
56-
| std::complex<float> | itk.complex[itk.F] |
57-
+---------------------+--------------------+
49+
+---------------------+--------------------+--------------------+
50+
| C++ type | Python type | NumPy dtype |
51+
+=====================+====================+====================+
52+
| float | itk.F | np.float32 |
53+
+---------------------+--------------------+--------------------+
54+
| double | itk.D | np.float64 |
55+
+---------------------+--------------------+--------------------+
56+
| unsigned char | itk.UC | np.uint8 |
57+
+---------------------+--------------------+--------------------+
58+
| std::complex<float> | itk.complex[itk.F] | np.complex64 |
59+
+---------------------+--------------------+--------------------+
5860

5961
This list is not exhaustive and is only presented to illustrate the type names. The complete list of types can be found in the `ITK Software Guide <https://itk.org/ItkSoftwareGuide.pdf>`_.
6062

@@ -90,7 +92,7 @@ ITK filter parameters can be specified in the following ways:
9092
Mixing ITK and NumPy
9193
--------------------
9294

93-
A common use case for using ITK in Python is to mingle NumPy and ITK operations on raster data. ITK provides a large number of I/O image formats and several sophisticated image processing algorithms not available in any other packages. The ability to intersperse that with numpy special purpose hacking provides a great tool for rapid prototyping.
95+
A common use case for using ITK in Python is to mingle NumPy and ITK operations on raster data. ITK provides a large number of I/O image formats and several sophisticated image processing algorithms not available in any other packages. The ability to intersperse that with the SciPy ecosystem provides a great tool for rapid prototyping.
9496

9597
The following script shows how to integrate NumPy and ITK:
9698

@@ -107,4 +109,4 @@ Similar functions are available to work with `itk.Matrix`, VNL vectors and matri
107109
Examples
108110
--------
109111

110-
Examples can be found in the `ITKExamples project <https://itk.org/ITKExamples/src/index.html>`_.
112+
Examples can be found in the `ITKSphinxExamples project <https://itk.org/ITKExamples/src/index.html>`_.

docs/code/Cast.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
3+
import numpy as np
4+
import itk
5+
import sys
6+
7+
input_filename = sys.argv[1]
8+
output_filename = sys.argv[2]
9+
10+
image = itk.imread(input_filename)
11+
12+
# Cast to an unsigned char pixel type
13+
cast_image = image.astype(itk.UC)
14+
15+
# Equivalent
16+
cast_image = image.astype(np.uint8)
17+
18+
itk.imwrite(cast_image, output_filename)

docs/code/CastImageFilter.py

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

docs/code/ExplicitInstantiation.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55

66
input_filename = sys.argv[1]
77

8+
# An apriori ImageType
89
PixelType = itk.F
910
ImageType = itk.Image[PixelType,2]
11+
image = itk.imread(input_filename, PixelType)
12+
13+
# An image type dynamically determined from the type on disk
14+
image = itk.imread(input_filename)
15+
ImageType = type(image)
1016

1117
# Functional interface
12-
image = itk.imread(input_filename, PixelType)
18+
# The `ttype` keyword argument specifies the filter type.
1319
smoothed = itk.median_image_filter(image, ttype=(ImageType, ImageType))
1420

1521
# Object-oriented interface
16-
reader = itk.ImageFileReader[ImageType].New(FileName=input_filename)
22+
reader = itk.ImageFileReader[ImageType].New(file_name=input_filename)
1723
median = itk.MedianImageFilter[ImageType, ImageType].New()
1824
median.SetInput(reader.GetOutput())
1925
median.Update()

docs/code/FilterParameters.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
#
1212
# number_of_iterations
1313
#
14-
smoothed = itk.anti_alias_binary_image_filter(image,
15-
number_of_iterations=3)
14+
smoothed = itk.anti_alias_binary_image_filter(image, number_of_iterations=3)
1615

1716
# CamelCase keyword arguments:
1817
#
1918
# NumberOfIterations
2019
#
21-
smoother = itk.AntiAliasBinaryImageFilter.New(image,
22-
NumberOfIterations=3)
20+
smoother = itk.AntiAliasBinaryImageFilter.New(image, NumberOfIterations=3)
2321
smoother.Update()
2422
smoothed = smoother.GetOutput()
2523

docs/code/ImplicitInstantiation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
input_filename = sys.argv[1]
77

88
image = itk.imread(input_filename)
9+
910
# Use ITK's functional, Pythonic interface. The filter type is implied by the
1011
# type of the input image. The filter is eagerly executed, and the output image
1112
# is directly returned.
@@ -21,9 +22,10 @@
2122

2223
# Use itk.ImageFileReader instead of the wrapping function,
2324
# itk.imread to illustrate this example.
24-
reader = itk.ImageFileReader.New(FileName=input_filename)
25+
ImageType = itk.Image[itk.UC, 2]
26+
reader = itk.ImageFileReader[ImageType].New(FileName=input_filename)
2527
# Here we specify the filter input explicitly
26-
median = itk.MedianImageFilter.New(Input=reader.GetOutput())
28+
median = itk.MedianImageFilter.New(reader.GetOutput())
2729
# Same as above but shortened. Input does not have to be specified.
2830
median = itk.MedianImageFilter.New(reader.GetOutput())
2931
# Same as above. .GetOutput() does not have to be specified.

docs/code/InstantiateITKObjects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
median = itk.MedianImageFilter[InputType, OutputType].New()
99

1010
# Instantiate non-SmartPointer objects
11-
pixel = itk.RGBPixel[itk.D]()
11+
pixel = itk.RGBPixel[itk.UC]()

docs/code/MixingITKAndNumPy.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
# Run filters on itk.Image
1515

16-
# View only of itk.Image, data is not copied
16+
# View only of itk.Image, pixel data is not copied
1717
np_view = itk.array_view_from_image(itk_image)
1818

19-
# Copy of itk.Image, data is copied
19+
# Copy of itk.Image, pixel data is copied
2020
np_copy = itk.array_from_image(itk_image)
21+
# Equivalent
22+
np_copy = np.asarray(itk_image)
2123

2224

2325
# Do NumPy stuff...
@@ -32,22 +34,22 @@
3234
# Save result
3335
itk.imwrite(itk_np_view, output_filename)
3436

35-
# VNL matrix from array
37+
# VNL matrix from np.ndarray
3638
arr = np.zeros([3,3], np.uint8)
3739
matrix = itk.vnl_matrix_from_array(arr)
3840

3941
# Array from VNL matrix
4042
arr = itk.array_from_vnl_matrix(matrix)
4143

42-
# VNL vector from array
44+
# VNL vector from np.ndarray
4345
vec = np.zeros([3], np.uint8)
4446
vnl_vector = itk.vnl_vector_from_array(vec)
4547

4648
# Array from VNL vector
4749
vec = itk.array_from_vnl_vector(vnl_vector)
4850

49-
# itk.Matrix from array
51+
# itk.Matrix from np.ndarray
5052
mat = itk.matrix_from_array(np.eye(3))
5153

52-
# array from itk.Matrix
54+
# np.ndarray from itk.Matrix
5355
arr = itk.array_from_matrix(mat)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def cleanup(files):
3939
# Run test ExplicitInstantiation.py
4040
add_test(["ExplicitInstantiation.py", baseline_image])
4141

42-
# Run test CastImageFilter.py
42+
# Run test Cast.py
4343
output_image = os.path.join(temp_folder, "filtered_image.png")
44-
add_test(["CastImageFilter.py", baseline_image, output_image])
44+
add_test(["Cast.py", baseline_image, output_image])
4545
cleanup([output_image])
4646

4747
# Run test CompareITKTypes.py

scripts/internal/manylinux-build-wheels.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,5 @@ for PYBIN in "${PYBINARIES[@]}"; do
161161
(cd $HOME && ${PYBIN}/python -c 'from itk import ITKCommon;')
162162
(cd $HOME && ${PYBIN}/python -c 'import itk; image = itk.Image[itk.UC, 2].New()')
163163
(cd $HOME && ${PYBIN}/python -c 'import itkConfig; itkConfig.LazyLoading = False; import itk;')
164-
(cd $HOME && ${PYBIN}/python ${script_dir}/../../docs/code/testDriver.py )
164+
(cd $HOME && ${PYBIN}/python ${script_dir}/../../docs/code/test.py )
165165
done

0 commit comments

Comments
 (0)