2016-06-14 99 views
1

如何加載和編寫.mhd/ .raw在ITK中格式化3D圖像?我試圖使用下面的代碼,但它沒有得到加載,因爲加載圖像的維度顯示爲0,0,0。ITK中的原始格式3D圖像

有人能指出我犯的錯嗎?

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

也許這是你的數據有問題。您至少應該提供一些示例數據。 – Trilarion

回答

1

剛纔我看到成功地使用下面的代碼SimpleITK Python中的MHD和原始文件:

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

也許你可以使用它作爲參考。

是否應該使用ReadImage函數而不是ImageFileReader?你可以試試。

0

根據已知格式,可以找到一些很好的文件讀取示例。

reader->SetImageIO(RawImageIO);

似乎不正確的東西在這裏做,如果你同時裝入.mhd和.RAW文件,因爲它們是獨立的格式,MetaImage VS Raw格式,你這樣做,不知道圖像尺寸,產地,間距等基於缺乏或缺乏標題。

你如何確定圖像的大小和獲得(0,0,0)?圖像 - >的getSize()?

你能提供測試數據嗎?

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

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