[Insight-users] Vtk->Itk connection..

Luis Ibanez luis.ibanez at kitware.com
Mon, 19 Jan 2004 10:56:05 -0500


This is a multi-part message in MIME format.
--------------090303030905000901090406
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit


Hi Yasser,

Please find attached the simplest example
on ITK segmentation + VTK visualization.

It does:

itkReader-->itkSmoothing-->itkSegmentation->

-->ITK2VTK-->vtkContour-->vtkPolyDataMapper


You will need to copy the files

   - itkImageToVTKImageFilter.h
   - itkImageToVTKImageFilter.txx

from

  InsightApplications/Auxiliary/vtk

into the directory where you put the
attached files

    SegmenterViewer2.cxx
    CMakeLists.txt


Regards,


   Luis


---------------------
yasser salman wrote:

> hi all..,
> i'm already use VTK for visualization and wanna to use
> itk for segmenation   can any body write me  a simple
> code for 
> getting the images from vtk to itk and do any simple
> segmentation process and then visualize it again ..,
> it's not the first time here
> but i read some examples (like skully doo) but i can't
> understand it well., all i need a very simple example
> to do:
> Visualize ->Segment(any simple method)->Visualize
> agian.. any help or suggestion will be appreciated..
> thanx 
> yasser.. 
> 


--------------090303030905000901090406
Content-Type: text/plain;
 name="SegmenterViewer2.cxx"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="SegmenterViewer2.cxx"


#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"
#include "itkImage.h"

#include "itkCastImageFilter.h"
#include "itkConfidenceConnectedImageFilter.h"
#include "itkCurvatureFlowImageFilter.h"

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkContourFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"

int main( int argc, char ** argv )
{

  if( argc < 6 )
    {
    std::cerr << "Usage: viewer1 imagefilename  xseed yseed zseed neighborhoodsize" << std::endl;
    return -1;
    }

  typedef signed short    InputPixelType;
  typedef float           InternalPixelType;
  typedef unsigned char   SegmentedPixelType;

  typedef itk::Image< InputPixelType, 3 >          InputImageType;
  typedef itk::Image< InternalPixelType, 3 >       InternalImageType;
  typedef itk::Image< SegmentedPixelType, 3 >      SegmentedImageType;

  typedef itk::ImageFileReader< InputImageType >   ReaderType;

  typedef   itk::CastImageFilter< 
                 InputImageType, 
                 InternalImageType >     CastImageFilterType;

  typedef   itk::CurvatureFlowImageFilter< 
                 InternalImageType, 
                 InternalImageType >     CurvatureFlowImageFilterType;

  typedef   itk::ConfidenceConnectedImageFilter< 
                         InternalImageType, 
                         SegmentedImageType >     ConfidenceConnectedImageFilterType;

  typedef itk::ImageToVTKImageFilter< SegmentedImageType >  ConnectorFilterType;


  //----------------------------------
  //           ITK Pipeline
  //----------------------------------

  ReaderType::Pointer  reader = ReaderType::New();

  CastImageFilterType::Pointer cast = CastImageFilterType::New();

  CurvatureFlowImageFilterType::Pointer smoothing = CurvatureFlowImageFilterType::New();

  ConfidenceConnectedImageFilterType::Pointer confidence = ConfidenceConnectedImageFilterType::New();

  ConnectorFilterType::Pointer connector = ConnectorFilterType::New();

  reader->SetFileName( argv[1] );

  cast->SetInput(       reader->GetOutput() );
  smoothing->SetInput(  cast->GetOutput() );
  confidence->SetInput( smoothing->GetOutput() );

  smoothing->SetTimeStep( 0.125 );
  smoothing->SetNumberOfIterations( 2 );
    
  confidence->SetMultiplier( 5.0 );
  confidence->SetNumberOfIterations( 2 );
  confidence->SetReplaceValue( 250 );
  
  typedef ConfidenceConnectedImageFilterType::IndexType IndexType;
  IndexType seed;
  seed[0] = atoi( argv[2] );
  seed[1] = atoi( argv[3] );
  seed[2] = atoi( argv[4] );

  std::cout << "Using seed = " << seed << std::endl;
  confidence->SetSeed( seed );

  confidence->SetInitialNeighborhoodRadius( atoi( argv[5] );

  connector->SetInput( confidence->GetOutput() );
  connector->GetImporter()->SetDataScalarTypeToUnsignedChar();

  //----------------------------------
  //           VTK Pipeline
  //----------------------------------

  vtkRenderer               * renderer               = vtkRenderer::New();
  vtkRenderWindow           * renderWindow           = vtkRenderWindow::New();
  vtkRenderWindowInteractor * renderWindowInteractor = vtkRenderWindowInteractor::New();

  renderWindow->AddRenderer( renderer );
  renderWindow->SetInteractor( renderWindowInteractor );

  vtkContourFilter * contour = vtkContourFilter::New();
  
  contour->SetInput( connector->GetOutput() );
  contour->SetValue( 0, 127.0 );

  vtkPolyDataMapper * polyDataMapper = vtkPolyDataMapper::New();

  polyDataMapper->SetInput( contour->GetOutput() );
  polyDataMapper->SetScalarRange( 0.0, 250.0 );

  vtkActor * actor = vtkActor::New();
  actor->SetMapper( polyDataMapper );
  actor->GetProperty()->SetColor( 0.8, 0.8, 1.0 );
  
  renderer->AddActor( actor );
  renderer->SetBackground( 0.1, 0.2, 0.4 );

  renderWindow->Render();
  renderWindowInteractor->Start();
  
  return 0;

}




--------------090303030905000901090406
Content-Type: text/plain;
 name="CMakeLists.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="CMakeLists.txt"


PROJECT( SegmenterViewer2 )

INCLUDE (${CMAKE_ROOT}/Modules/FindITK.cmake)
IF (USE_ITK_FILE)
  INCLUDE (${USE_ITK_FILE})
ENDIF (USE_ITK_FILE)

INCLUDE (${CMAKE_ROOT}/Modules/FindVTK.cmake)
IF (USE_VTK_FILE)
  INCLUDE (${USE_VTK_FILE})
ENDIF (USE_VTK_FILE)


ADD_EXECUTABLE( SegmenterViewer2 SegmenterViewer2.cxx )

TARGET_LINK_LIBRARIES( SegmenterViewer2
    ITKIO ITKCommon ITKBasicFilters ITKNumerics VXLNumerics
    vtkCommon vtkGraphics vtkRendering vtkImaging vtkIO vtkHybrid )


--------------090303030905000901090406--