在ITK中阅读* .mhd / *。原始格式​​的3D图像 [英] Reading *.mhd/*.raw format 3D images in ITK

查看:525
本文介绍了在ITK中阅读* .mhd / *。原始格式​​的3D图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ITK中加载和编写 .mhd / .raw格式的3D图像?我试图使用以下代码,但它没有加载,因为加载图像的尺寸显示为0,0,0。

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;
}


推荐答案

我刚读过mhd Python中的原始文件和成功使用以下SimpleITK代码:

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.

是否应该使用ReadImage函数而不是ImageFileReader?你可以尝试一下。

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

这篇关于在ITK中阅读* .mhd / *。原始格式​​的3D图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆