ITK读取并打印DICOM的所有TAG和值

该文章详细解释了如何使用ITK库从DICOM切片中获取包括年龄、生日在内的患者信息及特定TAG的值。
摘要由CSDN通过智能技术生成

ITK打印DICOM切片的所有TAG和对应值

dicom切片当中包含了很多患者信息,包括:年龄,生日,就诊时间,切片维度信息等,可以使用以下函数获取到相关信息

代码如下

int GetDicomTagValue(std::string DirPath)
{
	using PixelType = signed short;
	constexpr unsigned int Dimension = 3;

	using ImageType = itk::Image<PixelType, Dimension>;
	using ReaderType = itk::ImageSeriesReader<ImageType>;
	ReaderType::Pointer reader = ReaderType::New();

	using ImageIOType = itk::GDCMImageIO;
	ImageIOType::Pointer dicom = ImageIOType::New();
	reader->SetImageIO(dicom);//至此为创建DICOM reader的过程

	using NameGeneratorType = itk::GDCMSeriesFileNames;
	NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
	nameGenerator->SetInputDirectory(DirPath);

	using FileNamesContainer = std::vector<std::string>;
	FileNamesContainer fileNames = nameGenerator->GetInputFileNames();
	reader->SetFileNames(fileNames);

	try
	{
		reader->Update();
	}
	catch (itk::ExceptionObject & ex)
	{
		std::cout << ex << std::endl;
		return EXIT_FAILURE;
	}
	//切片序列读取成功

	using DictionaryType = itk::MetaDataDictionary;
	const DictionaryType & dictionary = dicom->GetMetaDataDictionary();

	using MetadataStringType = itk::MetaDataObject<std::string>;

	auto itr = dictionary.Begin();
	auto end = dictionary.End();
//打印所有的TAG和值
	while (itr != end)
	{
		itk::MetaDataObjectBase::Pointer entry = itr->second;

		MetadataStringType::Pointer entryvalue = dynamic_cast<MetadataStringType*>(entry.GetPointer());

		if (entryvalue)
		{
			std::string tagkey = itr->first;
			std::string tagvalue = entryvalue->GetMetaDataObjectValue();
			std::cout << tagkey << "  = " << tagvalue << std::endl;
		}
		++itr;
	}
//打印特定tag值对应的value	
	std::string entryId = "0008|0020";
	auto tagItr = dictionary.Find(entryId);
	if (tagItr == end)
	{
		std::cerr << " Tag" << entryId;
		std::cerr << " not found in the Dicom header" << std::endl;
		return EXIT_FAILURE;
	}

	MetadataStringType::ConstPointer entryValue = dynamic_cast<const MetadataStringType*>(tagItr->second.GetPointer());

	if (entryValue)
	{
		std::string tagvalue = entryValue->GetMetaDataObjectValue();
		std::cout << "Data generated time: (" << entryId << ")";
		std::cout << " is:  " << tagvalue << std::endl;

	}
	return 0}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值