ITK-读取单张DICOM文件的tag标签

#include "itkImageFileReader.h"
#include "itkGDCMImageIO.h"
#include "itkMetaDataObject.h"
#include "gdcmGlobal.h"

int main(int argc, char *argv[]) {

    typedef signed short       PixelType;
    const unsigned int         Dimension = 2;
    typedef itk::Image< PixelType, Dimension >      ImageType;
    typedef itk::ImageFileReader< ImageType >     ReaderType;

    ReaderType::Pointer reader = ReaderType::New();
    typedef itk::GDCMImageIO       ImageIOType;
    ImageIOType::Pointer dicomIO = ImageIOType::New();
    reader->SetFileName("../data/dicom/brain_001.dcm");
    reader->SetImageIO(dicomIO);

    try {
        reader->Update();
    } catch(itk::ExceptionObject &ex) {
        std::cout << ex << std::endl;
        return EXIT_FAILURE;
    }

    typedef itk::MetaDataDictionary   DictionaryType;

    const  DictionaryType &dictionary = dicomIO->GetMetaDataDictionary();

    typedef itk::MetaDataObject< std::string > MetaDataStringType;

    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());

        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;
            }
            // Software Guide : EndCodeSnippet
            else {
                std::cout << "(" << tagkey <<  ") " << "Unknown";
                std::cout << " = " << tagvalue.c_str() << std::endl;
            }
        }
        ++itr;
    }

    std::string entryId = "0010|0010";
    DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);

    if(tagItr != end) {
        MetaDataStringType::ConstPointer entryvalue =
            dynamic_cast<const MetaDataStringType *>(
                tagItr->second.GetPointer());

        if(entryvalue) {
            std::string tagvalue = entryvalue->GetMetaDataObjectValue();
            std::cout << "Patient's Name (" << entryId <<  ") ";
            std::cout << " is: " << tagvalue.c_str() << std::endl;
        }
    }

    std::string tagkey = "0008|1050";
    std::string labelId;
    if(itk::GDCMImageIO::GetLabelFromTag(tagkey, labelId)) {
        std::string value;
        std::cout << labelId << " (" << tagkey << "): ";
        if(dicomIO->GetValueFromTag(tagkey, value)) {
            std::cout << value;
        } else {
            std::cout << "(No Value Found in File)";
        }
        std::cout << std::endl;
    } else {
        std::cerr << "Trying to access inexistant DICOM tag." << std::endl;
    }

    itk::ImageIOBase::IOPixelType pixelType
        = reader->GetImageIO()->GetPixelType();
    itk::ImageIOBase::IOComponentType componentType
        = reader->GetImageIO()->GetComponentType();
    std::cout << "PixelType: " << reader->GetImageIO()
              ->GetPixelTypeAsString(pixelType) << std::endl;
    std::cout << "Component Type: " << reader->GetImageIO()
              ->GetComponentTypeAsString(componentType) << std::endl;

    return EXIT_SUCCESS;
}

打印出来的结果:
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值