[Insight-users] cannot paste the 2D sagittal slice to the original 3D image by using castImageFilter

wjhjky wjhjky at 163.com
Sat Dec 29 03:41:51 EST 2012


Hi, All:
 
I get some problems about pasting a processed 2D slice  to the original 3D .mha image.
 
First, I read a 3D .mha file and extract one sagittal slice from it ,then I use a simple algorithm to generate a new slice ,say output, By using CastImageFilter,
 I transform this 2D output to a 3D style and use PasteImageFilter to pasting it to the original image. However,I find that if I extract an axial slice, the final result
 is correct,if I extract a sagittal slice and cast it to a 3D style then paste it to original image I will get the wrong result. I guess something wrong happened when
 I cast a sagittal slice to a 3D image. Could anyone give me an adice? thx a lot!
                          
                                                                                                                                                                    Amy
 
Here is part of my code:
 
  #include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkExtractImageFilter.h"
#include "itkPasteImageFilter.h"
#include "itkConstNeighborhoodIterator.h"
#include "itkImageRegionIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkCastImageFilter.h"

int main( int argc, char ** argv )
{
  // Verify the number of parameters in the command line
  if( argc < 3 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " input3DImageFile  output3DImageFile " << std::endl;
    std::cerr << " sliceNumber " << std::endl;
    return EXIT_FAILURE;
    }
  typedef float                       InputPixelType;
  typedef float                       MiddlePixelType;
  typedef float                      OutputPixelType;
  typedef itk::Image< InputPixelType, 3 >       InputImageType3D;
  typedef itk::Image< MiddlePixelType, 2 >    MiddleImageType2D;
  typedef itk::Image< OutputPixelType, 3 >    OutputImageType3D;
  typedef itk::ImageFileReader< InputImageType3D  >  ReaderType;
  typedef itk::ImageFileWriter< OutputImageType3D >  WriterType;
  const char * inputFilename  = argv[1];
  const char * outputFilename = argv[2];
  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
  reader->SetFileName( inputFilename  );
  writer->SetFileName( outputFilename );
  reader->Update();
 
  typedef itk::ExtractImageFilter< InputImageType3D, MiddleImageType2D > ExtractFilterType;
  ExtractFilterType::Pointer extractFilter = ExtractFilterType::New();
  extractFilter->InPlaceOn();
  extractFilter->SetDirectionCollapseToIdentity();
 
  typedef itk::PasteImageFilter< InputImageType3D, OutputImageType3D> PasteFilterType;
  PasteFilterType::Pointer pasteFilter = PasteFilterType::New();
   const InputImageType3D * inputImage = reader->GetOutput();
 InputImageType3D::RegionType inputRegion = inputImage->GetLargestPossibleRegion();
   InputImageType3D::SizeType size = inputRegion.GetSize();
   size[0]=0;
   InputImageType3D::IndexType start = inputRegion.GetIndex();
   start[0]=/*sliceNumber*/100;    
  InputImageType3D::RegionType desiredRegion;
  desiredRegion.SetSize(  size  );
  desiredRegion.SetIndex( start );
  extractFilter->SetExtractionRegion( desiredRegion );
  extractFilter->SetInput( inputImage);
  extractFilter->Update();
 
  typedef itk::CastImageFilter<MiddleImageType2D,OutputImageType3D> Cast2Dto3DImageFilter;
  Cast2Dto3DImageFilter::Pointer castFilter=Cast2Dto3DImageFilter::New();
  MiddleImageType2D::Pointer output = MiddleImageType2D::New();
  output->SetRegions(extractFilter->GetOutput()->GetRequestedRegion());
  output->Allocate();
***************************************************************//here I process the extract slice then get a new result--output; output represents a same size and direction as the extractFilter->Getoutput();
 castFilter->SetInput(output);
  pasteFilter->SetSourceImage(castFilter->GetOutput());
  pasteFilter->SetDestinationImage( inputImage );
  pasteFilter->SetDestinationIndex( start );
  castFilter->Update();
  pasteFilter->SetSourceRegion(castFilter->GetOutput()->GetBufferedRegion()  );
  writer->SetInput( pasteFilter->GetOutput() );
 
  try
  {
   writer->Update();
  }
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20121229/289d9694/attachment.htm>


More information about the Insight-users mailing list