[Insight-users] Extrac 2D slice from a 3D Image with orientation

Luis Roberto P. Paula luisrpp at gmail.com
Sat Sep 13 17:09:08 EDT 2008


Hi all,

I did a little program to read a 3D Medical Image in Anaylze format and
extract a slice, saving it in 2D, using the same format.

It works for almost all of the images I have here, but with one I had a bad
result.

Here are 2 images that I have tested:
http://www.vision.ime.usp.br/~luisrpp/analyze/

With the 061b0105_anlz.hdr, it works fine. But with the s9.hdr, I have got
an unaligned image.

When I tryied to write a 2D image slice from s9.hdr, the following message
was displayed:

"AnalyzeImageIO (0x8153950): ERROR: Analyze 7.5 File Format Only Allows RPI,
PIR, and RIP Orientation"

Using an Medical Image Viwer program, I discovered that the image s9.hdr has
an IRA Orientation. I used then an OrientImageFilter to fix this. But it
didn't work. The following message was displayed:

"terminate called after throwing an instance of
'itk::InvalidRequestedRegionError'
  what():
/home/luis/dev-tools/InsightToolkit-3.2.0/Code/Common/itkDataObject.cxx:397:
Requested region is (at least partially) outside the largest possible
region.
Aborted"

Does anybody knows how to solve this problem?

This is par of the code (without validations):

-------------------------------------------------------------------------------------------------------------------------------------------------------
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkExtractImageFilter.h"
#include "itkImage.h"
#include "itkAnalyzeImageIO.h"
#include "itkOrientImageFilter.h"
#include<sys/stat.h>

typedef float        InputPixelType4D;
typedef float        OutputPixelType3D;

typedef itk::Image< InputPixelType4D,  4 >    InputImageType4D;
typedef itk::Image< OutputPixelType3D, 3 >    OutputImageType3D;

typedef itk::ImageFileReader< InputImageType4D  >  ReaderType4D;
typedef itk::ImageFileWriter< OutputImageType3D >  WriterType3D;
typedef itk::ExtractImageFilter< InputImageType4D, OutputImageType3D >
FilterType3D;

typedef float        InputPixelType3D;
typedef float        OutputPixelType2D;

typedef itk::Image< InputPixelType3D,  3 >    InputImageType3D;
typedef itk::Image< OutputPixelType2D, 2 >    OutputImageType2D;

typedef itk::ImageFileReader< InputImageType3D  >  ReaderType3D;
typedef itk::ImageFileWriter< OutputImageType2D >  WriterType2D;
typedef itk::ExtractImageFilter< InputImageType3D, OutputImageType2D >
FilterType2D;

typedef float   PixelType;
typedef itk::Image< PixelType, 2 >  InputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;

InputImageType::Pointer extract2DImageSlice(InputImageType3D::Pointer
reader, int plane, int slice) {
    FilterType2D::Pointer filter = FilterType2D::New();

    InputImageType3D::RegionType inputRegion =
        reader->GetLargestPossibleRegion();

    InputImageType3D::SizeType size = inputRegion.GetSize();
    size[plane] = 0;

    InputImageType3D::IndexType start = inputRegion.GetIndex();
    const unsigned int sliceNumber = slice;
    start[plane] = sliceNumber;

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

    filter->SetExtractionRegion( desiredRegion );

    filter->SetInput( reader );

    InputImageType::Pointer img = filter->GetOutput();
    img->Update();

    return img;
}

InputImageType3D::Pointer ReadAnalyzeFile(const char *path)
{
    itk::AnalyzeImageIO::Pointer io = itk::AnalyzeImageIO::New();
    ReaderType3D::Pointer fileReader = ReaderType3D::New();
    fileReader->SetImageIO(io);
    fileReader->SetFileName(path);
    fileReader->Update();
    InputImageType3D::Pointer rval = fileReader->GetOutput();

    itk::OrientImageFilter<InputImageType3D,InputImageType3D>::Pointer
orienter =
    itk::OrientImageFilter<InputImageType3D,InputImageType3D>::New();
    orienter->UseImageDirectionOn();

orienter->SetDesiredCoordinateOrientation(itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_IRA);
    orienter->SetInput(rval);
    orienter->Update();
    rval = orienter->GetOutput();

    return rval;
 }

int main( int argc, char ** argv )
{
    if( argc < 2 )
    {
        std::cerr << "Usage: " << std::endl;
        std::cerr << argv[0] << " input3DImageFile " << std::endl;
        return EXIT_FAILURE;
    }

    char *inputFilename  = argv[1];

    InputImageType3D::Pointer image = ReadAnalyzeFile(inputFilename);
    InputImageType::Pointer image2D = extract2DImageSlice(image, 0, 50);

    WriterType2D::Pointer writer = WriterType2D::New();
    writer->SetFileName( "teste.hdr" );
    writer->SetInput( image2D );

    try
    {
        writer->Update();
    }
    catch( itk::ExceptionObject & err )
    {
        std::cerr << "ExceptionObject caught !" << std::endl;
        std::cerr << err << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------

Thank's in advance,
Luis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20080913/b65331e4/attachment.htm>


More information about the Insight-users mailing list