在用itk的ImageFileWriter讀寫文件時,有時候會出現有的文件格式無法讀入或者輸出的問題,是因爲未引用對應圖像格式的IO庫。
比如將itk格式的文件輸出爲mhd文件時,程序首先要引用Meta圖像格式的IO庫,比如ITKIOMeta-4.10.lib。不僅如此,僅僅引用庫還不能正確輸出,在程序中還要調用註冊函數itk::MetaImageIOFactory::RegisterOneFactory()後才能正確輸出文件。
itk::MetaImageIOFactory::RegisterOneFactory();
typedef itk::ImageFileWriter<OutputImageType> WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("./image.mhd");
writer->SetInput(filter->GetOutput());
writer->Update();
RegisterOneFactory()在讀寫文件前只需要調用一次即可。