[Insight-users] Re: VTK ITK tcl/python connection

Charl P. Botha c . p . botha at ewi . tudelft . nl
28 Jul 2003 12:10:09 +0200


--=-6p7bFtenSftQ42Xd38pl
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2003-07-23 at 17:58, Bill Hoffman wrote:
> I would like to introduce you to Charl Botha.  He has
> already implemented a python itk/vtk integration.
> He is looking into a more general solution based on the
> same information I sent you.   You may want to try

Okay, it's all done.  Bill, I have built and tested it on Windows VC++
6.0 and on g++ 2.95.4 on Debian Linux and then went and committed it.  I
had to make more changes to CableSwig.cxx to make the CableSwig and
NoCable wrappings compatible: see CableSwig::TemplateName().  These
changes have more advantages: it means in future we will be able to add
similar pure Swig interface files to Insight and have the generated
wrappings interact with and operate on CableSwig wrapped objects.

I have attached a Python sample illustrating a pipeline that loads data
in using VTK, processes it with ITK and then writes it away using VTK
again.  Where in the Insight tree could I add this?

I had to add some code to the top-level CMakeLists.txt to add the
ITK_CSWIG_CONNECTVTKITK option (if CSWIG wrapping is active) and to mark
it as advanced.  In addition, if this option is ON, the CMakeLists in
Wrapping/CSwig has to make use of FindVTK.cmake as well.

Thanks,
Charl

-- 
charl p. botha http://cpbotha . net/ http://visualisation . tudelft . nl/

--=-6p7bFtenSftQ42Xd38pl
Content-Disposition: attachment; filename=cannyEdgeDetectionImageFilter-ConnectVTKITK.py
Content-Type: text/x-python; name=cannyEdgeDetectionImageFilter-ConnectVTKITK.py; charset=us-ascii
Content-Transfer-Encoding: 7bit

# This file demonstrates how to connect VTK and ITK pipelines together
# in scripted languages with the new ConnectVTKITK wrapping functionality.
# Data is loaded in with VTK, processed with ITK and written back to disc
# with VTK. 
#
# Note that this will only work if your ITK was compiled with
# ITK_CSWIG_CONNECTVTKITK set to ON
#
# -- Charl P. Botha <cpbotha AT ieee.org>

import os
import InsightToolkit as itk
import vtk

reader = vtk.vtkPNGReader()
ITK_TOP = "/home/cpbotha/build/Insight"
reader.SetFileName(os.path.join(ITK_TOP, "Testing/Data/Input/cthead1.png"))

# it has to be a single component, itk::VTKImageImport doesn't support more
lum = vtk.vtkImageLuminance()
lum.SetInput(reader.GetOutput())

# let's cast the output to float
imageCast = vtk.vtkImageCast()
imageCast.SetOutputScalarTypeToFloat()
imageCast.SetInput(lum.GetOutput())

# the end-point of this VTK pipeline segment is a vtkImageExport
vtkExporter = vtk.vtkImageExport()
vtkExporter.SetInput(imageCast.GetOutput())

# it connects to the itk::VTKImageImport at the beginning of
# the subsequent ITK pipeline; two-dimensional float type
itkImporter = itk.itkVTKImageImportF2_New()

# call the magic function that connects the two
itk.ConnectVTKToITKF2(vtkExporter, itkImporter.GetPointer())

canny  = itk.itkCannyEdgeDetectionImageFilterF2F2_New()
rescaler = itk.itkRescaleIntensityImageFilterF2US2_New()
canny.SetInput(itkImporter.GetOutput())
rescaler.SetInput(canny.GetOutput())
rescaler.SetOutputMinimum(0)
rescaler.SetOutputMaximum(65535)

# this is to show off the new PyCommand functionality. :)
def progressEvent():
    print canny.GetProgress()
    
pc = itk.itkPyCommand_New()
pc.SetCommandCallable(progressEvent)
canny.AddObserver(itk.itkProgressEvent(), pc.GetPointer())
# end of show-off

# this will form the end-point of the ITK pipeline segment
itkExporter = itk.itkVTKImageExportUS2_New()
itkExporter.SetInput(rescaler.GetOutput())

# the vtkImageImport will bring our data back into VTK-land
vtkImporter = vtk.vtkImageImport()
# do the magic connection call
itk.ConnectITKUS2ToVTK(itkExporter.GetPointer(), vtkImporter)

# finally write the image to disk using VTK
writer = vtk.vtkPNGWriter()
writer.SetFileName('maatjie.png')
writer.SetInput(vtkImporter.GetOutput())

# before we call Write() on the writer, it is prudent to give
# our ITK pipeline an Update() call... this is not necessary
# for normal error-less operation, but ensures that exceptions
# thrown by ITK get through to us in the case of an error;
# This is because the VTK wrapping system does not support
# C++ exceptions.
rescaler.Update()

# write the file to disk...
writer.Write()

--=-6p7bFtenSftQ42Xd38pl--