[Insight-users] Re: 2D slice extraction

Luis Ibanez luis.ibanez at kitware.com
Wed Sep 29 14:41:56 EDT 2004


Hi Nawab,

Your code for extracting a single slice looks fine.

The only problem I can see is with the filename of
the input image. The extension ".dat" will not be
recognized by any of the default ImageIO readers.

Please look at the FAQ for a list of the accepted
filename extensions.

Also, you should put the line

                 reader->Update()

inside a try/catch block, so you can verify if the
image was actually read. You will find multiple
examples on this in the ITK Software Guide

     http://www.itk.org/ItkSoftwareGuide.pdf


For an example on how to extract one slice from a
Volume, please look at the file


       Insight/Examples/IO/
           ImageReadExtractWrite.cxx


It expects as input:

   inputVolumeFileName  outputSliceFileName   zSliceNumber




   Regards,


      Luis


---------------------------
Nawab Singh Baghel wrote:

> Dear Luis,
> 
> I would like to extract one slice from 3D volume.
> What is wrong is this code? Please help..
> 
> The error is unhandled exception in .exe file: access violation
> 
> Regards
> 
>   typedef itk::Image< unsigned short, 3 > ImageType;
>   typedef itk::ImageFileReader< ImageType >  ReaderType;
>   ReaderType::Pointer reader = ReaderType::New();
>   reader->SetFileName( "d:\\SH_DCM_NM.dat");
> 
> /*
>   typedef itk::ImageFileWriter< ImageType >  WriterType;
>   WriterType::Pointer writer = WriterType::New();
>   writer->SetFileName( "d:\\SH_DCM_NM.vtk");
> */
>   reader->Update();
> 
>   typedef itk::Image< unsigned short, 2 >    OutputImageType;
>   typedef itk::ImageFileWriter< OutputImageType >  Writer2DType;
>   Writer2DType::Pointer writer2d = Writer2DType::New();
>   writer2d->SetFileName( "d:\\Slice.png");
> 
>   typedef itk::ExtractImageFilter< ImageType, OutputImageType > SliceType;
>   SliceType::Pointer slice = SliceType::New();
>   ImageType::RegionType inputRegion =
> reader->GetOutput()->GetLargestPossibleRegion();
>   ImageType::SizeType size = inputRegion.GetSize();
>   size[2] = 0;
>   ImageType::IndexType start = inputRegion.GetIndex();
>   const unsigned int sliceNumber = 6;
>   start[2] = sliceNumber;
>   ImageType::RegionType desiredRegion;
>   desiredRegion.SetSize(  size  );
>   desiredRegion.SetIndex( start );
>   slice->SetExtractionRegion( desiredRegion );
>   slice->SetInput( reader->GetOutput() );
>   slice->Update();
>   writer2d->SetInput( slice->GetOutput() );
> 
> 
> 
> 
> /*
>   typedef itk::ThresholdImageFilter< ImageType >  FilterType;
>   FilterType::Pointer filter = FilterType::New();
>   filter->SetOutsideValue( 0 );
>   filter->ThresholdBelow( 100 );
>   filter->SetInput(reader->GetOutput());
> 
>   writer->SetInput(filter->GetOutput());
> */
>   try
>     {
>     writer2d->Update();
>  AfxMessageBox("Done...");
>     }
> 
>   catch( itk::ExceptionObject & exception )
>     {
>     std::cout << "Problems reading file format" << std::endl;
>     std::cout << exception << std::endl;
>  //   return 1;
>     }
> 
> 
> 
> 
> 
> 






More information about the Insight-users mailing list