bool writeMhdFile(const char* dicomPath, const char *mhdFileName, bool useCompression)
{
typedef itk::Image<short, 3> ImageType;
ImageType::Pointer image;
if (!dicomSeriesToITKImage(dicomPath, image))
{
return false;
}
typedef itk::ImageFileWriter<ImageType> ImageWriterType;
ImageWriterType::Pointer writer = ImageWriterType::New();
writer->SetFileName(mhdFileName);
writer->SetInput(image);
//writer->SetImageIO(itk::MetaImageIO::New());
itk::MetaImageIO::Pointer metaWriter = itk::MetaImageIO::New();
itk::MetaDataDictionary outputDictionaty = metaWriter->GetMetaDataDictionary();
itk::EncapsulateMetaData<std::string>(
outputDictionaty, std::string("0028|1051"),
std::to_string(350));//TODO from dcm
itk::EncapsulateMetaData<std::string>(
outputDictionaty, std::string("0028|1050"),
std::to_string(40));//TODO from dcm
metaWriter->SetMetaDataDictionary(outputDictionaty);
writer->SetImageIO(metaWriter);
image->SetMetaDataDictionary(outputDictionaty);
writer->SetUseCompression(useCompression);
try
{
writer->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << " failed to write mhd file to folder: " << mhdFileName << ex << std::endl;
return false;
}
return true;
}
参考代码