c++opencvimage-processingdcmtk

Convert OFString to string or char in DCMTK


I am trying to extract the instance number from a DICOM image using dcmtk. The code is shown below:

DcmFileFormat fileformat;
    OFCondition status = fileformat.loadFile(src_path);
    if (status.good())
      {
        OFString instanceNumber=0;
        if (fileformat.getDataset()->findAndGetOFString(DCM_InstanceNumber, instanceNumber).good())
        {
            std::cout << "instance Number N: " << instanceNumber << std::endl;
            sprintf(instanceNum, "%s\n", instanceNumber);

            printf("%s\n", instanceNum);
        }
        else
            std::cerr << "Error: cannot access instance Number!" << std::endl;
       }
    else
        std::cerr << "Error: cannot read DICOM file (" << status.text() << ")" << std::endl;

Now I got the instance number,but I want to copy the instance number into a char or string( for further programming).But as the number is an OFString,how do I convert it into the required datatype. Any thoughts?


Solution

  • You can use dcmtk to directly read into the requested data type with the findAndGetXXX-methods. In your particular case the VR of the Instance Number is IS, so the method to use is findAndGetSint32. See: here.

    Applied to your code:

    DcmFileFormat fileformat;
    OFCondition status = fileformat.loadFile(src_path);
    if (status.good())
      {
        SInt32 instanceNumber=0;
        if (fileformat.getDataset()->findAndGetSint32(DCM_InstanceNumber, &instanceNumber).good())
        {
            std::cout << "instance Number N: " << instanceNumber << std::endl;
            sprintf(instanceNum, "%d\n", instanceNumber);
    
            printf("%d\n", instanceNum);
        }
        else
            std::cerr << "Error: cannot access instance Number!" << std::endl;
       }
    else
        std::cerr << "Error: cannot read DICOM file (" << status.text() << ")" << std::endl;
    

    shoud be working...