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