一、程序功能
读取一系列的CT dcm图片,然后重新写入到一个文件夹
二、代码
#pragma warning(disable:4996) #include "itkGDCMImageIO.h" #include "itkGDCMSeriesFileNames.h" #include "itkImageSeriesReader.h" #include "itkImageSeriesWriter.h" int main(int argc, char** argv) { //定义像素类型,图像类型,三维有符号数,定义指针 typedef signed short PixelType; const unsigned int Dimension = 3; typedef itk::Image< PixelType, Dimension > ImageType; typedef itk::ImageSeriesReader< ImageType > ReaderType; //声明读、写 DICOM 图 像 的 itk::GDCMImageIO对象 //itk::GDCMSeriesFileNames对象将生成并将构成所有体数据的切片的文件名进行排序 typedef itk::GDCMImageIO ImageIOType; typedef itk::GDCMSeriesFileNames NamesGeneratorType; ImageIOType::Pointer gdcmIO = ImageIOType::New(); NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New(); //设置读取路径 //用文件名发生器生成被读的文件名和被写的文件名 namesGenerator->SetInputDirectory("D:\\Files\\ITKFiles\\ITK_5_ReadSeriesDCM\\Data\\InputData"); const ReaderType::FileNamesContainer& filenames =namesGenerator->GetInputFileNames(); //设置DICOM图像IO对象和被读的文件名的列表 ReaderType::Pointer reader = ReaderType::New(); reader->SetImageIO(gdcmIO); reader->SetFileNames(filenames); try { reader->Update(); } catch (itk::ExceptionObject& ex) { std::cout << ex << std::endl; return EXIT_FAILURE; } //得到输出目录的名字 const char* outputDirectory = "D:\\Files\\ITKFiles\\ITK_5_ReadSeriesDCM\\Data\\OutputData"; //如果目录 //还不存在的话,我们就选择创建目录。 itksys::SystemTools::MakeDirectory(outputDirectory); //实例化写图像的程序 typedef signed short OutputPixelType; const unsigned int OutputDimension = 2; typedef itk::Image< OutputPixelType, OutputDimension > Image2DType; typedef itk::ImageSeriesWriter<ImageType, Image2DType > SeriesWriterType; //我们创建一个序列图像writer并从reader的输出连接writer的输入。这时我们传递GDCM //图像IO对象以便能用DICOM格式写这个图像。 SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New(); seriesWriter->SetInput(reader->GetOutput()); seriesWriter->SetImageIO(gdcmIO); namesGenerator->SetOutputDirectory(outputDirectory); seriesWriter->SetFileNames(namesGenerator->GetOutputFileNames()); seriesWriter->SetMetaDataDictionaryArray(reader->GetMetaDataDictionaryArray()); try { seriesWriter->Update(); } catch (itk::ExceptionObject& excp) { std::cerr << "Exception thrown while writing the series " << std::endl; std::cerr << excp << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }
三、注意
第一句是因为有的语法在现在不太符合规范,但是只是算是警告,而不是错误
四、参考书目
《医学图像分割与处理》ITK手册(也就是ITK软件的说明手册,官网有)