[vtkusers] converting from vtk to itk and back again

Mark Roden mmroden at gmail.com
Tue Sep 20 00:59:30 EDT 2011


Hi all,

I've looked on google and the various newsgroups and have not found
the answer to what I thought was a pretty straightforward question.

I have a vtkImageData object that has been obtained... somehow.  I do
not have the pipeline at this point, just the output.  The user could
have done any number of things to have arrived at this point, and so I
store a vtkImageData that has been DeepCopied off of the end of
whatever pipeline was employed.

I then want to save that image in the Analyze format.  A coworker made
his own reader/writer using the information that Mayo put out, but the
resulting files don't appear to be oriented correctly.  I want to
verify if his version is producing a bug, and I wanted to use the itk
version of the writing plugin to do the work.

Here's what I have:

#ifdef ITK_IMG_WRITER
#include "vtkImageExport.h"
#include <itkVTKImageImport.h>
#include "itkImageFileWriter.h"
#include "itkAnalyzeImageIO.h"
#include "itkImage.h"
#include "itkOrientedImage.h"
#endif


void saveAnalyzeImage(vtkImageData * const image, const QString &file) {

#ifdef ITK_IMG_WRITER
    //this code is done using ITK, and so requires that ITK be included
    typedef itk::OrientedImage< signed short, 3 > CTImage;
    typedef itk::ImageFileWriter< CTImage> CTWriter;
    typedef itk::VTKImageImport<CTImage> VTKImageImportType;

    CTWriter::Pointer theWriter = CTWriter::New();

    theWriter->SetFileName(file.toStdString().c_str());

    vtkImageExport* theExporter = vtkImageExport::New();
    theExporter->SetInput(image);

    VTKImageImportType::Pointer vtkImageToImageFilter =
VTKImageImportType::New();
    theExporter->SetOutput(vtkImageToImageFilter);
    vtkImageToImageFilter->Update();

    theWriter->SetInput(vtkImageToImageFilter->GetOutput());

    itk::AnalyzeImageIO::Pointer analyzeIO = itk::AnalyzeImageIO::New();
    theWriter->SetImageIO(analyzeIO);

    try {
        theWriter->Update();
    } catch(itk::ExceptionObject ex){
        return; //do something else here for error handling
    }
    theWriter->Delete();
    vtkImageToImageFilter->Delete();
    analyzeIO->Delete();
    return;

#else
    //do the other code instead
#endif
}

This code is not compiling, because
theExporter->SetOutput(vtkImageToImageFilter) doesn't work.  I've also
tried just using the VTKImageImportType object directly, but that does
not have a SetInput method.  As could be guessed from the code, I also
tried vtkImageToImageFilter, but that appears to be a deprecated
leftover from 2004 or so.  Unless there's some include file I'm not
finding?

All I really want is for vtk to read and write analyze files.  Barring
that, I want to be able to seamlessly convert from a vtkImageData
object to an itk::Image object so I can use itk's analyze export and
importing functions.  How can I fix that code to make it work?

Thanks,
Mark



More information about the vtkusers mailing list