ITK  4.8.0
Insight Segmentation and Registration Toolkit
Examples/Visualization/CannyEdgeDetectionImageFilterConnectVTKITK.py
1 #==========================================================================
2 #
3 # Copyright Insight Software Consortium
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 "%.0f%s done..." % (canny.GetProgress() * 100.0, '%')
73 
74 pc = itk.itkPyCommand_New()
75 pc.SetCommandCallable(progressEvent)
76 canny.AddObserver(itk.itkProgressEvent(), pc.GetPointer())
77 # end of show-off
78 
79 # this will form the end-point of the ITK pipeline segment
80 itkExporter = itk.itkVTKImageExportUS2_New()
81 itkExporter.SetInput(rescaler.GetOutput())
82 
83 # the vtkImageImport will bring our data back into VTK-land
84 vtkImporter = vtk.vtkImageImport()
85 # do the magic connection call (once again: only available if you built
86 # ITK with ITK_CSWIG_CONNECTVTKITK set to ON)
87 CVIPy.ConnectITKUS2ToVTK(itkExporter.GetPointer(), vtkImporter)
88 
89 # finally write the image to disk using VTK
90 writer = vtk.vtkPNGWriter()
91 writer.SetFileName('./testout.png')
92 writer.SetInput(vtkImporter.GetOutput())
93 
94 # before we call Write() on the writer, it is prudent to give
95 # our ITK pipeline an Update() call... this is not necessary
96 # for normal error-less operation, but ensures that exceptions
97 # thrown by ITK get through to us in the case of an error;
98 # This is because the VTK wrapping system does not support
99 # C++ exceptions.
100 rescaler.Update()
101 
102 # write the file to disk...
103 writer.Write()
104 
105 print "\n\nWrote testout.png to current directory."