[ITK Community] Fwd: Problems with slice extraction from a 3D image for directions different from z
Simon Wimmer
wimmersimon at gmail.com
Sat Mar 8 15:01:12 EST 2014
Hello ITK community,
I've started to use ITK two weeks ago for an university project and have
been quite satisfied with what I achieved with it so far.
But today, I stumbled across a problem that I could not resolve by
consulting the existing resources on the web and old mailing list threads.
For our project we have to work with 3D MRI data that is provided as an
.mhd file.
We need to extract slices orthogonal to every axis from that data.
For that matter I consulted the docs and found the example code in
ImageReadExtractWrite.cxx which makes use of the ExtractImageFilter. This
method works perfectly for extracting a slice in z direction as the example
code does.
So I tried to make the code extract a slice in x direction by simply
changing the lines
size[2] = 0;
> InputImageType::IndexType start = inputRegion.GetIndex();
> const unsigned int sliceNumber = atoi( argv[3] );
> start[2] = sliceNumber;
to
> size[0] = 0;
> InputImageType::IndexType start = inputRegion.GetIndex();
> const unsigned int sliceNumber = atoi( argv[3] );
> start[0] = sliceNumber;
But this code throws an exception:
Description: itk::ERROR: ExtractImageFilter(037F46F0): Invalid submatrix
> extracted for collapsed direction.
If I change
> filter->SetDirectionCollapseToSubmatrix();
to
> filter->SetDirectionCollapseToIndentity();
the code runs cleanly, but produces an awkward result image because
seemingly the slice spacing information is lost.
Do you have any hints on how I could resolve the issue/where I have gone
wrong or can you point out different approaches I could try to pursue?
The whole code:
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkExtractImageFilter.h"
> #include "itkImage.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 output2DImageFile " <<
> std::endl;
> std::cerr << " sliceNumber " << std::endl;
> return EXIT_FAILURE;
> }
> typedef signed short InputPixelType;
> typedef signed short OutputPixelType;
> typedef itk::Image< InputPixelType, 3 > InputImageType;
> typedef itk::Image< OutputPixelType, 2 > OutputImageType;
> typedef itk::ImageFileReader< InputImageType > ReaderType;
> typedef itk::ImageFileWriter< OutputImageType > WriterType;
> // Here we recover the file names from the command line arguments
> //
> 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 );
> typedef itk::ExtractImageFilter< InputImageType,
> OutputImageType > FilterType;
> FilterType::Pointer filter = FilterType::New();
> filter->InPlaceOn();
> filter->SetDirectionCollapseToSubmatrix();
> reader->UpdateOutputInformation();
> InputImageType::RegionType inputRegion =
> reader->GetOutput()->GetLargestPossibleRegion();
> InputImageType::SizeType size = inputRegion.GetSize();
> size[0] = 0;
> InputImageType::IndexType start = inputRegion.GetIndex();
> const unsigned int sliceNumber = atoi( argv[3] );
> start[0] = sliceNumber;
> InputImageType::RegionType desiredRegion;
> desiredRegion.SetSize( size );
> desiredRegion.SetIndex( start );
> filter->SetExtractionRegion( desiredRegion );
> filter->SetInput( reader->GetOutput() );
> writer->SetInput( filter->GetOutput() );
> try
> {
> writer->Update();
> }
> catch( itk::ExceptionObject & err )
> {
> std::cerr << "ExceptionObject caught !" << std::endl;
> std::cerr << err << std::endl;
> return EXIT_FAILURE;
> }
>
> return EXIT_SUCCESS;
> }
Regards,
Simon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20140308/819e5e81/attachment-0002.html>
More information about the Community
mailing list