[Insight-users] writing out 2D png image from 3D data

David Brayford dbrayford at gmail.com
Fri Oct 28 17:10:05 EDT 2011


I am trying to write out a 2D image in png format from a 3D
projection, but I get the following exception thrown.

itk::InvalidRequestedRegionError (0x7fffd4e67c50)
Location: "virtual void itk::DataObject::PropagateRequestedRegion()"
File: /home/davidb/Downloads/InsightToolkit-3.20.0/Code/Common/itkDataObject.cxx
Line: 397
Description: Requested region is (at least partially) outside the
largest possible region.

However, if I try to write the data out as a 3D format the output
image is fine. In the code below.

 typedef itk::ResampleImageFilter<MovingImageType, FixedImageType>
ResamplerType;
  typedef itk::SubtractImageFilter<FixedImageType,FixedImageType,
FixedImageType>  SubtracterType;
  typedef itk::ImageFileWriter<FixedImageType> WriterType;

  typedef itk::ImageFileWriter<PNGImageType> WriterType2D;


        ResamplerType::Pointer resampler = ResamplerType::New();
        resampler->SetInput( registration->GetMovingImage() );
        resampler->SetInterpolator( registration->GetMultiInterpolator()[f] );
        resampler->SetTransform( transform );
        resampler->SetUseReferenceImage( true );
        resampler->SetReferenceImage( registration->GetFixedMultiImage()[f] );

        std::stringstream strStream;
        strStream << outDir << "/initProjection";
        strStream.width(2);
        strStream.fill('0');
        strStream << f;
        strStream.width(0);
        strStream << ".mha";

        WriterType::Pointer initProjectionWriter = WriterType::New();
        initProjectionWriter->SetFileName( strStream.str().c_str() );
        initProjectionWriter->SetInput( resampler->GetOutput() );

        std::cout << "Attempting to write the initial projection file " <<
          initProjectionWriter->GetFileName() << std::endl;
        try
          {
          initProjectionWriter->Update();
          }
        catch( itk::ExceptionObject & e )
          {
          std::cerr << e.GetDescription() << std::endl;
          }

        // write out DRR image file
        typedef itk::ExtractImageFilter< FixedImageType, PNGImageType
> FilterType;
        FilterType::Pointer filter = FilterType::New();

        FixedImageType::RegionType inputRegion =
resampler->GetReferenceImage()->GetLargestPossibleRegion();

//registration->->GetFixedMultiImage()[f]->GetLargestPossibleRegion();

        FixedImageType::SizeType size = inputRegion.GetSize();
        size[2] = 0;
        FixedImageType::IndexType start = inputRegion.GetIndex();
        const unsigned int sliceNumber = 1;
        start[2] = sliceNumber;

        FixedImageType::RegionType desiredRegion;
        desiredRegion.SetSize(  size  );
        desiredRegion.SetIndex( start );

        filter->SetExtractionRegion( desiredRegion );


        filter->SetInput( resampler->GetOutput() );

        std::stringstream strStream2;
        strStream2 << outDir << "/initProjection";
        strStream2.width(2);
        strStream2.fill('0');
        strStream2 << f;
        strStream2.width(0);
        strStream2 << ".png";

        WriterType2D::Pointer initProjectionWriter2D = WriterType2D::New();
        initProjectionWriter2D->SetFileName( strStream2.str().c_str() );
        initProjectionWriter2D->SetInput( filter->GetOutput() );

        try
          {
          std::cout << "Writing DRR image: " << std::endl;
          initProjectionWriter2D->Update();
          }
        catch( itk::ExceptionObject & err )
          {
          std::cerr << "ERROR: ExceptionObject caught !" << std::endl;
          std::cerr << err << std::endl;
          }


More information about the Insight-users mailing list