本文主要是介绍ITK学习笔记——dicom序列读取写入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
由于dicom通常都是一系列的图片,为方便后续处理,需要一次性将许多dicom图像一起读取写入。
维数变成了三维,除了长宽,多了切片数。
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"#include <vector>
#include "itksys/SystemTools.hxx"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;typedef itk::GDCMImageIO ImageIOType;typedef itk::GDCMSeriesFileNames NamesGeneratorType;ImageIOType::Pointer gdcmIO = ImageIOType::New();NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();namesGenerator->SetInputDirectory( "C:/input" ); //输入目录const ReaderType::FileNamesContainer & filenames =namesGenerator->GetInputFileNames();unsigned int numberOfFilenames = filenames.size();std::cout << numberOfFilenames << std::endl;for(unsigned int fni = 0; fni<numberOfFilenames; fni++){std::cout << "filename # " << fni << " = ";std::cout << filenames[fni] << std::endl;}ReaderType::Pointer reader = ReaderType::New();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;return EXIT_FAILURE;}const char * outputDirectory = "C:/output"; //输出目录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;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学习笔记——dicom序列读取写入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!