1. 程式人生 > >DICOM影象標籤讀寫

DICOM影象標籤讀寫

需求:修改DICOM影象中的tag

方法:用ITK中的GDCM

#include "itkImage.h"
#include "itkImageSeriesReader.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkMetaDataObject.h"
#include "itkImageFileWriter.h"
ReaderType::Pointer m_pReader;
	m_pReader = ReaderType::New();

	typedef itk::MetaDataDictionary DictionaryType;
	DictionaryType &dictionary = m_pReader->GetMetaDataDictionary();

	std::string tagkey = "0010|0021"; // Conversion Type
	std::string value = "xxxx";

	itk::EncapsulateMetaData<std::string>(dictionary, tagkey, value);

	typedef itk::MetaDataObject<std::string> MetaDataStringType;//管理字串型別
	//從資料字典中可以獲得幾乎所有dicom標籤對應的數值
	DictionaryType::ConstIterator itr=dictionary.Begin();
	DictionaryType::ConstIterator end=dictionary.End();
	while(itr!=end)
	{
		itk::MetaDataObjectBase::Pointer entry=itr->second;
		MetaDataStringType::Pointer entryValue=dynamic_cast<MetaDataStringType *>(entry.GetPointer());
		//dynamic_cast將一個基類物件指標(或引用)cast到繼承類指標 MetaDataStringType::Pointer
		if(entryValue)
		{
			std::string tagKey=itr->first;
			std::string labelId;
			bool found=itk::GDCMImageIO::GetLabelFromTag(tagKey,labelId);
			std::string tagValue = entryValue->GetMetaDataObjectValue();
			if(found)
			{
				std::cout<<"("<<tagKey<<")"<<labelId;
				std::cout<<"="<<tagValue.c_str()<<std::endl;
			}
			else
			{
				std::cout<<"("<<tagKey<<")"<<"unknown";
				std::cout<<"="<<tagValue.c_str()<<std::endl;
			}
			++itr;
		}
	}

	ImageType::Pointer input_data = ImageType::New();
	input_data = m_pReader->GetOutput();
	input_data->SetMetaDataDictionary(dictionary);

	itk::GDCMImageIO::Pointer gdcmImageIO =itk::GDCMImageIO::New();
	gdcmImageIO->SetMetaDataDictionary(dictionary);
	gdcmImageIO->SetKeepOriginalUID(false);

	typedef itk::ImageFileWriter< ImageType > WriterType;
	WriterType::Pointer writer = WriterType::New();
	writer->SetMetaDataDictionary(dictionary);
	writer->SetImageIO(gdcmImageIO);	
	writer->SetFileName( "D:/111" );
	writer->SetInput( input_data );
	writer->Update();

ps:部落格的編輯器真難用!