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

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

发布了52 篇原创文章 · 获赞 6 · 访问量 5132

猜你喜欢

转载自blog.csdn.net/weixin_44723106/article/details/105492906
今日推荐