ITK  5.4.0
Insight Toolkit
Examples/Visualization/CannyEdgeDetectionImageFilterConnectVTKITK.py
1# ==========================================================================
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# https://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# ==========================================================================*/
18
19# This file demonstrates how to connect VTK and ITK pipelines together
20# in scripted languages with the new ConnectVTKITK wrapping functionality.
21# Data is loaded in with VTK, processed with ITK and written back to disc
22# with VTK.
23#
24# For this to work, you have to build InsightApplications/ConnectVTKITK
25# as well.
26#
27# It also demonstrates the use of the python-specific itkPyCommand object.
28#
29# -- Charl P. Botha <cpbotha AT ieee.org>
30
31import os
32import sys
33import InsightToolkit as itk
34import ConnectVTKITKPython as CVIPy
35import vtk
36
37# VTK will read the PNG image for us
38reader = vtk.vtkPNGReader()
39reader.SetFileName("../../Testing/Data/Input/cthead1.png")
40
41# it has to be a single component, itk::VTKImageImport doesn't support more
42lum = vtk.vtkImageLuminance()
43lum.SetInput(reader.GetOutput())
44
45# let's cast the output to float
46imageCast = vtk.vtkImageCast()
47imageCast.SetOutputScalarTypeToFloat()
48imageCast.SetInput(lum.GetOutput())
49
50# the end-point of this VTK pipeline segment is a vtkImageExport
51vtkExporter = vtk.vtkImageExport()
52vtkExporter.SetInput(imageCast.GetOutput())
53
54# it connects to the itk::VTKImageImport at the beginning of
55# the subsequent ITK pipeline; two-dimensional float type
56itkImporter = itk.itkVTKImageImportF2_New()
57
58# Call the magic function that connects the two. This will only be
59# available if you built ITK with ITK_CSWIG_CONNECTVTKITK set to ON.
60CVIPy.ConnectVTKToITKF2(vtkExporter, itkImporter.GetPointer())
61
62# perform a canny edge detection and rescale the output
63canny = itk.itkCannyEdgeDetectionImageFilterF2F2_New()
64rescaler = itk.itkRescaleIntensityImageFilterF2US2_New()
65canny.SetInput(itkImporter.GetOutput())
66rescaler.SetInput(canny.GetOutput())
67rescaler.SetOutputMinimum(0)
68rescaler.SetOutputMaximum(65535)
69
70# this is to show off the new PyCommand functionality. :)
71def progressEvent():
72 print(f"{canny.GetProgress() * 100.0:.0f}{'%'} done...")
73
74
75pc = itk.itkPyCommand_New()
76pc.SetCommandCallable(progressEvent)
77canny.AddObserver(itk.itkProgressEvent(), pc.GetPointer())
78# end of show-off
79
80# this will form the end-point of the ITK pipeline segment
81itkExporter = itk.itkVTKImageExportUS2_New()
82itkExporter.SetInput(rescaler.GetOutput())
83
84# the vtkImageImport will bring our data back into VTK-land
85vtkImporter = vtk.vtkImageImport()
86# do the magic connection call (once again: only available if you built
87# ITK with ITK_CSWIG_CONNECTVTKITK set to ON)
88CVIPy.ConnectITKUS2ToVTK(itkExporter, vtkImporter)
89
90# finally write the image to disk using VTK
91writer = vtk.vtkPNGWriter()
92writer.SetFileName("./testout.png")
93writer.SetInput(vtkImporter.GetOutput())
94
95# before we call Write() on the writer, it is prudent to give
96# our ITK pipeline an Update() call... this is not necessary
97# for normal error-less operation, but ensures that exceptions
98# thrown by ITK get through to us in the case of an error;
99# This is because the VTK wrapping system does not support
100# C++ exceptions.
101rescaler.Update()
102
103# write the file to disk...
104writer.Write()
105
106print("\n\nWrote testout.png to current directory.")