当前位置:   article > 正文

DCMTK读取DICOM文件头信息的三种方法_dcmtk dicom 读取

dcmtk dicom 读取

Howto: Load File Meta-Header

Here’s an example that shows how to load the File Meta Information Header of a DICOMfile without reading the dataset. This could be useful if you are e.g. only interested in the SOP Class UID and Transfer Syntax UID of the file.

There are three different approaches:

  1. Use the loadFile() method of the DcmMetaInfo class.

  2. Use the loadFile() method of the DcmFileFormat class with read mode ERM_metaOnly.

  3. Use the read() method of the DcmFileFormat class in order to successively load the meta-header and the dataset.

The third approach allows for reading the dataset only if certain criteria (based on the data elements in the meta-header) are met. Please note that in this case the same input stream is used and that the meta-header is only read once.

Source Code

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmdata/dcistrmf.h"   

int main(int argc, char *argv[])
{
    /* approach 1 */
    OFCondition status;   
    DcmMetaInfo metainfo;
    status = metainfo.loadFile("test.dcm");
    if (status.good())
    {
        OFString sopClassUID, xferUID;
        if (metainfo.findAndGetOFString(DCM_MediaStorageSOPClassUID, sopClassUID).good())
            COUT << "SOP Class UID: " << sopClassUID << OFendl;
        if (metainfo.findAndGetOFString(DCM_TransferSyntaxUID, xferUID).good())
            COUT << "Transfer Syntax UID: " << xferUID << OFendl;
        metainfo.print(COUT);
    }   

    /* approach 2 */
    DcmFileFormat fileformat;
    status = fileformat.loadFile("test.dcm", EXS_Unknown, EGL_noChange, DCM_MaxReadLength, ERM_metaOnly);
    if (status.good())
    {
        OFString sopClassUID, xferUID;
        if (fileformat.getMetaInfo()->findAndGetOFString(DCM_MediaStorageSOPClassUID, sopClassUID).good())
            COUT << "SOP Class UID: " << sopClassUID << OFendl;
        if (fileformat.getMetaInfo()->findAndGetOFString(DCM_TransferSyntaxUID, xferUID).good())
            COUT << "Transfer Syntax UID: " << xferUID << OFendl;
        fileformat.print(COUT);
    }   

    /* approach 3 */
    fileformat.clear();
    DcmInputFileStream fileStream("test.dcm");
    status = fileStream.status();
    if (status.good())
    {
        /* first, read meta-header */
        fileformat.setReadMode(ERM_metaOnly);
        fileformat.transferInit();
        status = fileformat.read(fileStream);
        if (status.good())
        {
            OFString sopClassUID, xferUID;
            if (fileformat.getMetaInfo()->findAndGetOFString(DCM_MediaStorageSOPClassUID, sopClassUID).good())
                COUT << "SOP Class UID: " << sopClassUID << OFendl;
            if (fileformat.getMetaInfo()->findAndGetOFString(DCM_TransferSyntaxUID, xferUID).good())
                COUT << "Transfer Syntax UID: " << xferUID << OFendl;
            /* then read dataset if certain criteria are met */
            if (sopClassUID == UID_SecondaryCaptureImageStorage)
            {
                fileformat.setReadMode(ERM_autoDetect);
                status = fileformat.read(fileStream);
                if (status.good())
                {
                    fileformat.print(COUT);
                }
            }
        }
        fileformat.transferEnd();
    }   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

Note
Please note that the above sample code requires DCMTK 3.5.5 (20100608 or newer).

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/165503
推荐阅读
相关标签
  

闽ICP备14008679号