itk 读取dcm 或nii.gz数据

关键代码如下,判断如果是dcm,则转换为nii.gz:

	#include "itkGDCMSeriesFileNames.h"

     
    using NamesGeneratorType = itk::GDCMSeriesFileNames;
	NamesGeneratorType::Pointer nameGenarator = NamesGeneratorType::New();
	using FilenamesContainer = std::vector;
    using ImageIOTypeDCM = itk::GDCMImageIO;
	using ImageIOTypeNII = itk::NiftiImageIO;
    using ReaderTypeNII = itk::ImageFileReader< Short3DImageType  >;
	using ReaderTypeDCM = itk::ImageSeriesReader;

     string sPatientFileDir = "";
	
	 sPatientFileDir = FolderNames.at(i) + pPatientFileName;

	 nameGenarator->SetInputDirectory(FolderNames.at(i));
	 nameGenarator->Update();
	 using FilenamesContainer = std::vector;
	 FilenamesContainer filenames = nameGenarator->GetInputFileNames();

	if ((_access(sPatientFileDir.data(), 0) != 0) && filenames.size()==0)//如果没有指定文件名的nii.gz并且检测到的dcm文件数为0,则判断不存在。
	{
		cout << sPatientFileDir << "not exist";
		continue;
	}
	bool bIsDcm = false;
	if (filenames.size() != 0)
	{
		bIsDcm = true;
	}
   

   Short3DImageType::Pointer OriImg;
	if (bIsDCM)
	{
		ReaderTypeDCM::Pointer Reader = ReaderTypeDCM::New();
		ImageIOTypeDCM::Pointer gNiftiImageIORead = ImageIOTypeDCM::New();
		Reader->SetImageIO(gNiftiImageIORead);
		Reader->SetFileNames(filenames);
		Reader->Update();
		OriImg = Reader->GetOutput();

		WriterType::Pointer WriterOri = WriterType::New();
		ImageIOTypeNII::Pointer NiftiImageIOOri = ImageIOTypeNII::New();
		WriterOri->SetImageIO(NiftiImageIOOri);
		WriterOri->SetInput(OriImg);
		string filepath = InputFileName;
		string InputFullFileName = filepath + "first_baseline_NCCT.nii.gz";
		WriterOri->SetFileName(InputFullFileName);
		WriterOri->Update();
	}
	else
	{
		ReaderTypeNII::Pointer Reader = ReaderTypeNII::New();
		Reader->SetFileName(InputFileName);
		ImageIOTypeNII::Pointer gNiftiImageIORead = ImageIOTypeNII::New();
		Reader->SetImageIO(gNiftiImageIORead);
		Reader->Update();
		OriImg = Reader->GetOutput();
		
	}

 

你可能感兴趣的:(ITK)