[Insight-users] how can i convert from Vtk To itk...

Luis Ibanez luis.ibanez at kitware.com
Fri, 06 Feb 2004 17:56:06 -0500


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

Hi Yasser,

My mistake,

I should have been more explicit
in the previous email...

You need one filter for converting an
ITK image into a VTK one, and *another*
type of filter for converting from a
VTK image into an ITK image.


Both filters are in:

   InsightApplications/Auxiliary/vtk

The filter

   itkImageToVTKImageFilter<>

converts an ITK image into a vtkImageData.
while the filter

   itkVTKImageToImageFilter<>

converts a vtkImageData into an ITK image.


---

You were attempting to force the ImageToVTKImage
filter to do both conversions by using casts like
(itk::Image<char,2> *). Please never do that!!.
Such castings defeat all the purpose of typing
in C++. The error message from the compiler was
telling you that this was not the right filter
to use.

---

Please find attached the modified file that
is now compiling.

Note that the CurvatureFlow filter needs the pixel
type of the image to be float type. Therefore the
"InternalPixelType" was changed.

Note also that ITK has a Dicom reader, so you
don't really need to use the vtk one for reading
the slices into memory. For examples on how to
use the ITK dicom reader please look in

        Insight/Examples/IO



A missing definition was added to the file
itkVTKImageToImageFilter.h. Please update your
checkout in order to get it. This is required
for maintaining compatibility with VTK 4.2 and
VTK-CVS. You could also get the files through
CVS-Web:
http://www.itk.org/cgi-bin/cvsweb.cgi/InsightApplications/Auxiliary/vtk/?cvsroot=Insight


Please let us know if you find any problem,



    Thanks



    Luis




----------------------
yasser salman wrote:
> Note: forwarded message attached.
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Finance: Get your refund fast by filing online.
> http://taxes.yahoo.com/filing.html
> 
> 
> ------------------------------------------------------------------------
> 
> Subject:
> Re: [Insight-users] From Vtk To itk...
> From:
> yasser salman <yass71 at yahoo.com>
> Date:
> Fri, 6 Feb 2004 04:58:48 -0800 (PST)
> To:
> Luis Ibanez <luis.ibanez at kitware.com>
> 
> 
> hi Luis ..,
> About ur suggestion to use the same filter
> "Itk::ImageToVTKImageFilter" to connect vtk->Itk->Vtk
> agian.., i wrote this code at the attachment but  it
> didn't work.., 
> can u help me to show me how to connect these
> pipeLines..,
> thanx..,
> 
> --- Luis Ibanez <luis.ibanez at kitware.com> wrote:
> 
>>Yasser,
>>
>>If you need to convert a VTK image
>>into an ITK one you use the filter
>>itkVTKImageToImageFilter.
>>
>>You will find the files in the directory:
>>
>>   InsightApplications/Auxiliary/vtk
>>
>>      itkVTKImageToImageFilter.h
>>      itkVTKImageToImageFilter.txx
>>
>>
>>With this filter you can do:
>>
>>
>>VTKReader-->VTK2ITK---ITKSegmentation-->
>>
>>--->ITK2VTK--->VTKContour..... visualization
>>
>>
>>Please look at the tutorial session
>>
>>
> 
> http://www.itk.org/CourseWare/Training/GettingStarted-II.pdf
> 
>>
>>
>>
>>Regards,
>>
>>
>>   Luis
>>
>>
===========================================================



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


#include "itkImageFileReader.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"
#include "vtkImageActor.h"
#include "vtkImageShiftScale.h"
#include "vtkMarchingCubes.h"

#include "vtkProperty.h"
#include "vtkPolyDataNormals.h"
#include "vtkContourFilter.h"
#include "vtkImageViewer.h"
#include "vtkImageReader2.h"
#include "vtkVolumeRayCastFunction.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkPiecewiseFunction.h"
#include "vtkVolumeProperty.h" 
#include "vtkColorTransferFunction.h"
#include "vtkImageViewer2.h"
#include "vtkPolyData.h"
#include "vtkImageData.h"
#include "vtkDICOMImageReader.h"
#include "vtkImageFlip.h"
#include "vtkContourFilter.h"
#include "vtkPolyDataMapper.h"


#include "itkImageToVTKImageFilter.h"
#include "itkVTKImageToImageFilter.h"

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


  typedef unsigned char   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 >  ITK2VTKConnectorFilterType;

  typedef itk::VTKImageToImageFilter< InputImageType     >  VTK2ITKConnectorFilterType;




    vtkDICOMImageReader *Reader=vtkDICOMImageReader::New(); 
	vtkImageViewer2 *viewer = vtkImageViewer2::New();     
//	Reader->SetDirectoryName("D:/imagedata/Dicom/1Nesma");              
	Reader->SetDirectoryName(argv[1]);
    Reader->Update();										  
		vtkImageFlip *flipY = vtkImageFlip::New();
flipY->SetInput (Reader->GetOutput());
flipY->SetFilteredAxis (1);

	viewer->SetInput(flipY->GetOutput());				  
int VolData_Images = viewer->GetWholeZMax ();			  


for (int i=1;i<VolData_Images;i++)
{
	
	viewer->SetZSlice(i);		  
    viewer->SetSize(512,512);
	viewer->SetPosition(500,0);
	viewer->SetColorWindow(512);
	viewer->SetColorLevel(256);
	viewer->Render();

// TODO: Add your command handler code here
}		

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

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

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

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

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


  VTK2ITKConnectorFilterType::Pointer VTK2ITKconnector = VTK2ITKConnectorFilterType::New();

  VTK2ITKconnector->SetInput( Reader ->GetOutput() );

  cast->SetInput( VTK2ITKconnector->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] = 50;
  seed[1] = 50;
  seed[2] = 5;

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

  confidence->SetInitialNeighborhoodRadius( 3);

  ITK2VTKConnectorFilterType::Pointer ITK2VTKconnector = ITK2VTKConnectorFilterType::New();

  ITK2VTKconnector->SetInput(confidence ->GetOutput());
  ITK2VTKconnector->GetImporter()->SetDataScalarTypeToUnsignedChar();
  //----------------------------------
  //           VTK Pipeline
  //----------------------------------

  vtkRenderer               * renderer               = vtkRenderer::New();
  vtkRenderWindow           * renderWindow           = vtkRenderWindow::New();
  vtkRenderWindowInteractor * renderWindowInteractor = vtkRenderWindowInteractor::New();
vtkImageActor* imageactor = vtkImageActor::New();
  renderWindow->AddRenderer( renderer );
  renderWindow->SetInteractor( renderWindowInteractor );
 vtkMarchingCubes *mc = vtkMarchingCubes::New();

	mc->SetInput( ITK2VTKconnector->GetOutput() );  
	mc->SetValue(0,127);

	vtkPolyDataMapper* mcmap=vtkPolyDataMapper::New();
	mcmap->SetInput(mc->GetOutput());
	mcmap->ScalarVisibilityOff();
	vtkActor* mcactor=vtkActor::New();
	mcactor->SetMapper(mcmap); 
	renderer->AddActor(mcactor);
  renderer->SetBackground( 0.1, 0.2, 0.4 );

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

}






--------------030508050907060409010106--