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;
}