ITK  5.2.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 # 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 # ==========================================================================*/
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 # this is to show off the new PyCommand functionality. :)
71 def progressEvent():
72  print(f"{canny.GetProgress() * 100.0:.0f}{'%'} done...")
73 
74 
75 pc = itk.itkPyCommand_New()
76 pc.SetCommandCallable(progressEvent)
77 canny.AddObserver(itk.itkProgressEvent(), pc.GetPointer())
78 # end of show-off
79 
80 # this will form the end-point of the ITK pipeline segment
81 itkExporter = itk.itkVTKImageExportUS2_New()
82 itkExporter.SetInput(rescaler.GetOutput())
83 
84 # the vtkImageImport will bring our data back into VTK-land
85 vtkImporter = vtk.vtkImageImport()
86 # do the magic connection call (once again: only available if you built
87 # ITK with ITK_CSWIG_CONNECTVTKITK set to ON)
88 CVIPy.ConnectITKUS2ToVTK(itkExporter, vtkImporter)
89 
90 # finally write the image to disk using VTK
91 writer = vtk.vtkPNGWriter()
92 writer.SetFileName("./testout.png")
93 writer.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.
101 rescaler.Update()
102 
103 # write the file to disk...
104 writer.Write()
105 
106 print("\n\nWrote testout.png to current directory.")