Reading *.mhd/*.raw format 3D images in ITK

2019-07-30 13:47发布

问题:

How to load and write .mhd/.raw format 3D images in ITK? I have tried to use the following code but it is not getting loaded as the dimension of the loaded image is displayed as 0,0,0.

Can someone please point out the mistake I am making?

typedef float InputPixelType;
const unsigned int  DimensionOfRaw = 3;
typedef itk::Image< InputPixelType, DimensionOfRaw > InputImageType;

//typedef itk::RawImageIO<InputPixelType, DimensionOfRaw> ImageIOType;
typedef itk::ImageFileReader<InputImageType >   ReaderType;

/*
 * --------------------Loader and saver of Raws, as well the function that takes a resulting (from inference matrix/vector) and creates a Raw out of it.-----------------------
 */
InputImageType::Pointer loadRawImageItk( std::string RawFullFilepathname, ReaderType::Pointer & RawImageIO ) {
    //http://www.itk.org/Doxygen/html/classitk_1_1Image.html
    //http://www.itk.org/Doxygen/html/classitk_1_1ImageFileReader.html

    typedef itk::ImageFileReader<InputImageType> ReaderType;

    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(RawFullFilepathname);


    //ImageIOType::Pointer RawImageIO = ImageIOType::New();
    reader->SetImageIO( RawImageIO );

    try {
        reader->Update();
    } catch (itk::ExceptionObject& e) {
        std::cerr << e.GetDescription() << std::endl;
        exit(1);  // You can choose to do something else, of course.
    }

    //InputImageType::Pointer inputImage = reader->GetOutput();
    InputImageType::Pointer inputImage = reader->GetOutput();

    return inputImage;

}


int saveRawImageItk( std::string RawFullFilepathname, InputImageType::Pointer & outputImageItkType , ImageIOType::Pointer & RawImageIO) {
  std::cout << "Saving image to: " << RawFullFilepathname << "\n";

  typedef itk::ImageFileWriter< InputImageType >  Writer1Type;
  Writer1Type::Pointer writer1 = Writer1Type::New();

  writer1->SetInput( outputImageItkType );
  writer1->SetFileName( RawFullFilepathname );
  writer1->SetImageIO( RawImageIO ); //seems like this is useless.

  // Execution of the writer is triggered by invoking the \code{Update()} method.
  try
    {
    writer1->Update();
    }
  catch (itk::ExceptionObject & e)
    {
    std::cerr << "exception in file writer " << std::endl;
    std::cerr << e.GetDescription() << std::endl;
    std::cerr << e.GetLocation() << std::endl;
    return 1;
    }

  return 0;
}

回答1:

I have just read the mhd and raw files in Python successfully using the following SimpleITK code:

import SimpleITK as sitk
import numpy as np
def load_itk_image(filename):
    itkimage = sitk.ReadImage(filename)
    numpyImage = sitk.GetArrayFromImage(itkimage)
    return numpyImage

Maybe you can use it as a reference.

Whether you should use the ReadImage function instead of the ImageFileReader? You can have a try.



回答2:

A few good examples of file reading depending on a known format are found here.

reader->SetImageIO( RawImageIO );

seems the incorrect thing to do here if you are loading both .mhd and .raw files as they are seperate formats, MetaImage vs Raw format where you do and don't know the image size, origin, spacing etc based on the absense or presense of a header.

How are you determining the size of the image and getting (0,0,0)? image->GetSize()?

Can you provide test data?

https://itk.org/Wiki/ITK/Examples/IO/ReadUnknownImageType

https://itk.org/ITKExamples/src/IO/ImageBase/RegisterIOFactories/Documentation.html