<div dir="ltr"><div class="gmail_quote"><div dir="ltr">Hello ITK community,<div><br></div><div>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.</div>
<div>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.</div>
<div>For our project we have to work with 3D MRI data that is provided as an .mhd file.</div><div>We need to extract slices orthogonal to every axis from that data.</div><div>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.</div>
<div>So I tried to make the code extract a slice in x direction by simply changing the lines</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
size[2] = 0;<br>InputImageType::IndexType start = inputRegion.GetIndex();<br>const unsigned int sliceNumber = atoi( argv[3] );<br>start[2] = sliceNumber;</blockquote><div><br></div><div>to</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
size[0] = 0;<br>InputImageType::IndexType start = inputRegion.GetIndex();<br>const unsigned int sliceNumber = atoi( argv[3] );<br>start[0] = sliceNumber; </blockquote><div><br></div><div>But this code throws an exception:</div>
</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Description: itk::ERROR: ExtractImageFilter(037F46F0): Invalid submatrix extracted for collapsed direction.</blockquote>
<div><br></div><div>If I change</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">filter->SetDirectionCollapseToSubmatrix();</blockquote>
<div>to </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> filter->SetDirectionCollapseToIndentity();</blockquote>
<div>the code runs cleanly, but produces an awkward result image because seemingly the slice spacing information is lost.</div></div><div><br></div><div>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?</div>
<div><br></div><div>The whole code:</div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">#include "itkImageFileReader.h"<br>
#include "itkImageFileWriter.h"<br>#include "itkExtractImageFilter.h"<br>#include "itkImage.h"<br><br>int main( int argc, char ** argv )<br>{<br> // Verify the number of parameters in the command line<br>
if( argc < 3 )<br> {<br> std::cerr << "Usage: " << std::endl;<br> std::cerr << argv[0] << " input3DImageFile output2DImageFile " << std::endl;<br> std::cerr << " sliceNumber " << std::endl;<br>
return EXIT_FAILURE;<br> }<br> typedef signed short InputPixelType;<br> typedef signed short OutputPixelType;<br> typedef itk::Image< InputPixelType, 3 > InputImageType;<br> typedef itk::Image< OutputPixelType, 2 > OutputImageType;<br>
typedef itk::ImageFileReader< InputImageType > ReaderType;<br> typedef itk::ImageFileWriter< OutputImageType > WriterType;<br> // Here we recover the file names from the command line arguments<br> //<br>
const char * inputFilename = argv[1];<br> const char * outputFilename = argv[2];<br> ReaderType::Pointer reader = ReaderType::New();<br> WriterType::Pointer writer = WriterType::New();<br> reader->SetFileName( inputFilename );<br>
writer->SetFileName( outputFilename );<br> typedef itk::ExtractImageFilter< InputImageType,<br> OutputImageType > FilterType;<br> FilterType::Pointer filter = FilterType::New();<br>
filter->InPlaceOn();<br> filter->SetDirectionCollapseToSubmatrix();<br> reader->UpdateOutputInformation();<br> InputImageType::RegionType inputRegion =<br> reader->GetOutput()->GetLargestPossibleRegion();<br>
InputImageType::SizeType size = inputRegion.GetSize();<br> size[0] = 0;<br> InputImageType::IndexType start = inputRegion.GetIndex();<br> const unsigned int sliceNumber = atoi( argv[3] );<br> start[0] = sliceNumber;<br>
InputImageType::RegionType desiredRegion;<br> desiredRegion.SetSize( size );<br> desiredRegion.SetIndex( start );<br> filter->SetExtractionRegion( desiredRegion );<br> filter->SetInput( reader->GetOutput() );<br>
writer->SetInput( filter->GetOutput() );<br> try<br> {<br> writer->Update();<br> }<br> catch( itk::ExceptionObject & err )<br> {<br> std::cerr << "ExceptionObject caught !" << std::endl;<br>
std::cerr << err << std::endl;<br> return EXIT_FAILURE;<br> }<br><br> return EXIT_SUCCESS;<br>}</blockquote></div><div><br></div><div>Regards,</div><div><br></div><div>Simon</div><div><br></div></div>
</div><br></div>