#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"
//#include "itkImageFileReader.h"//读取头文件
//#include "itkImageFileWriter.h"//写入头文件
#include "itkGDCMImageIO.h"//ImageIo头文件
int main(int argc, char* argv[])
{
typedef float InternalPixelType;
const unsigned int Dimension = 3;//输入为三维
typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
typedef float OutputPixelType;
const unsigned int OutputDimension = 2;//输出为二维
typedef itk::Image< OutputPixelType, OutputDimension > OutputImageType;
typedef itk::ImageSeriesReader< InternalImageType > ReaderType;
typedef itk::ImageSeriesWriter< InternalImageType, OutputImageType > WriterType;//自动在输入和输出之间转换
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
//DICOM数据的ImageIO
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
//文件生成器
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
namesGenerator->SetInputDirectory("D:/leg_dcm/");
//保存文件名
const ReaderType::FileNamesContainer& filenames = namesGenerator->GetInputFileNames();
reader->SetImageIO(gdcmIO);
reader->SetFileNames(filenames);
try
{
reader->Update();
}
catch (itk::ExceptionObject& excp)
{
std::cerr << "Exception thrown while writing the image" << std::endl;
std::cerr << excp << std::endl;
std::cout << "succed1 " << std::endl;
system("pause");
return EXIT_FAILURE;
}
//输出文件夹
const char* outputDirectory = "D:/124/";
itksys::SystemTools::MakeDirectory(outputDirectory);
//设置输出文件夹
namesGenerator->SetOutputDirectory(outputDirectory);
writer->SetFileNames(namesGenerator->GetOutputFileNames());
//用原来的文件头
writer->SetMetaDataDictionaryArray(reader->GetMetaDataDictionaryArray());
writer->SetInput(reader->GetOutput());
writer->SetImageIO(gdcmIO);
try
{
writer->Update();
}
catch (itk::ExceptionObject& excep)
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
std::cout << "succed " << std::endl;
system("pause");
return EXIT_FAILURE;
}
system("pause");
return EXIT_SUCCESS;
}
ITK 读写DICOM序列图像例子
最新推荐文章于 2024-08-21 15:14:38 发布