MPEG-4 to DICOM File?

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
shalas1
Posts: 7
Joined: Thu, 2022-08-04, 15:08

MPEG-4 to DICOM File?

#1 Post by shalas1 » Thu, 2022-08-11, 15:26

Hello,

I had seen some older posts on this forum asking about this, but I want to post in case there were any new features added in newer DCMTK versions that may make this easier. I am trying to create a DICOM file from an MPEG-4 video file. What are some possible ways to approach this issue? I would like to support the Video Endoscopic Image Storage SOP in my application.

Thank you,
Steve

shalas1
Posts: 7
Joined: Thu, 2022-08-04, 15:08

Re: MPEG-4 to DICOM File?

#2 Post by shalas1 » Tue, 2022-08-16, 21:35

More specifically, I am interested in adding an existing MPEG-4 file to a DcmDataset object, but I'm not sure if that is possible.

If that is not possible, I am looking for information about how to extract the frames of a given MPEG-4 file, and use some of the DCMTK classes like "DcmPixelItem", DcmPixelData", "DcmPixelSequence", etc. to create/insert Pixel Data into a DcmDataset object. Honestly, I do not know where to begin.

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1352
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: MPEG-4 to DICOM File?

#3 Post by Marco Eichelberg » Wed, 2022-08-17, 13:47

This is entirely possible with DCMTK, there is just no command line tool to do it.

With regard to using the various classes for compressed images, see Image2Dcm::insertEncapsulatedPixelDataFirstFrame() (file dcmdata/libi2d/i2d.cc) as an example.

shalas1
Posts: 7
Joined: Thu, 2022-08-04, 15:08

Re: MPEG-4 to DICOM File?

#4 Post by shalas1 » Wed, 2022-08-17, 17:27

Thank you for your reply. It doesn't seem I am able to load the MPEG-4 file at all - loading it into a DcmFileFormat object does not work, and I receive the following error:
E: DcmElement: Unknown Tag & Data (0000,2000) larger (1887007846) than remaining bytes in file
E: Failed to load file: I/O suspension or premature end of stream


I'm not sure how to begin at this point. I tried following the guide at https://support.dcmtk.org/redmine/proje ... ressedData and I am seeing the above error after the "loadFile" line.

shalas1
Posts: 7
Joined: Thu, 2022-08-04, 15:08

Re: MPEG-4 to DICOM File?

#5 Post by shalas1 » Thu, 2022-08-18, 16:32

I found some resources regarding creating a pixel sequence and inserting it into a dataset. I have the following code:

Code: Select all

    OFCondition result;
    QString mpegFileName = caseDir.path() + "/" + "testMpeg4.mp4";
    OFString mpegFileNameOF = mpegFileName.toStdString().c_str();
    // Length of the MPEG-4 video file in bytes
    Uint32 length = 580000;
    DcmDataset* dataset = new DcmDataset();
    DcmInputFileStream dcmFileStream(mpegFileNameOF, 0);
    DcmPixelSequence* pixelSequence = new DcmPixelSequence(DCM_PixelSequenceTag);
    DcmPixelItem* offsetTable = new DcmPixelItem(DCM_PixelItemTag);
    result = pixelSequence->insert(offsetTable);
    if (result.bad()) {
        DCMNET_ERROR("Could not insert to pixel sequence: " << result.text());
    }
    DcmPixelItem* frame = new DcmPixelItem(DCM_PixelItemTag);
    result = frame->createValueFromTempFile(dcmFileStream.newFactory(), length, EBO_LittleEndian);
    if (result.bad()) {
        DCMNET_ERROR("Could not create value from temp file: " << result.text());
    }
    result = pixelSequence->insert(frame);
    if (result.bad()) {
        DCMNET_ERROR("Could not insert frames to pixel sequence: " << result.text());
    }
    
    DcmPixelData* pixelData = new DcmPixelData(DCM_PixelData);
    pixelData->putOriginalRepresentation(EXS_MPEG4HighProfileLevel4_1, nullptr, pixelSequence);
    result = dataset->insert(pixelData, true);
    if (result.bad()) {
        DCMNET_ERROR("Could not insert pixel data into dataset: " << result.text());
    }

    dataset->putAndInsertOFStringArray(DCM_PatientName, patientName.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_PatientID, patientID.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_PatientBirthDate, patientDOB.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_PatientSex, patientSex.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_AccessionNumber, accessionNumber.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_ReferringPhysicianName, referringPhysician.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_InstitutionName, institutionName.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_StudyDate, studyDate.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_StudyTime, studyTime.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_StudyID, studyID.toStdString().c_str(), true);
    dataset->putAndInsertOFStringArray(DCM_SOPClassUID, UID_VideoEndoscopicImageStorage);
    dataset->putAndInsertOFStringArray(DCM_TransferSyntaxUID, UID_MPEG4HighProfileLevel4_1TransferSyntax);

    DcmFileFormat fileformat(dataset);
    fileformat.writeXML(std::cout);
    result = fileformat.saveFile(firstDcmFileNameOF);
    if (result.bad()) {
        DCMNET_ERROR("Unable to save file: " << result.text());
    }
I see the following output from fileformat.writeXML():

<file-format>
<meta-header xfer="1.2.840.10008.1.2.1" name="Little Endian Explicit">
</meta-header>
<data-set xfer="1.2.840.10008.1.2.1" name="Little Endian Explicit">
<element tag="0002,0010" vr="UI" vm="1" len="24" name="TransferSyntaxUID">1.2.840.10008.1.2.4.102</element>
<element tag="0008,0016" vr="UI" vm="1" len="32" name="SOPClassUID">1.2.840.10008.5.1.4.1.1.77.1.1.1</element>
<element tag="0008,0020" vr="DA" vm="1" len="8" name="StudyDate">20220624</element>
<element tag="0008,0030" vr="TM" vm="1" len="4" name="StudyTime">5:48</element>
<element tag="0008,0050" vr="SH" vm="1" len="10" name="AccessionNumber">987654321</element>
<element tag="0008,0080" vr="LO" vm="1" len="8" name="InstitutionName">HospTest</element>
<element tag="0008,0090" vr="PN" vm="1" len="8" name="ReferringPhysicianName">Dr. Test</element>
<element tag="0010,0010" vr="PN" vm="1" len="12" name="PatientName">PatientSteve</element>
<element tag="0010,0020" vr="LO" vm="1" len="10" name="PatientID">123456789</element>
<element tag="0010,0030" vr="DA" vm="1" len="10" name="PatientBirthDate">199610317</element>
<element tag="0010,0040" vr="CS" vm="1" len="2" name="PatientSex">M</element>
<element tag="0020,0010" vr="SH" vm="1" len="6" name="StudyID">12346</element>
<sequence tag="7fe0,0010" vr="OB" card="2" name="PixelData">
<pixel-item len="0" binary="hidden"></pixel-item>
<pixel-item len="580000" loaded="no" binary="hidden"></pixel-item>
</sequence>
</data-set>
</file-format>

And from fileformat.saveFile(), I receive the following error:
E: Unable to save file: Illegal call, perhaps wrong parameters

I'm not sure what is going wrong here. Does anyone have any suggestions?

shalas1
Posts: 7
Joined: Thu, 2022-08-04, 15:08

Re: MPEG-4 to DICOM File?

#6 Post by shalas1 » Mon, 2022-08-29, 17:44

I just want to share that I figured this out a couple weeks ago. I was able to create a DICOM file from an MPEG-4 H264 Level 4.1 input file.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 1 guest