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