Setting sagittal orientation for DICOM Dataset

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
keytooker
Posts: 1
Joined: Tue, 2024-10-08, 10:20

Setting sagittal orientation for DICOM Dataset

#1 Post by keytooker »

Hi,

I'm converting multiple JPEG images into a single DCM file. The source images are in sagittal projection.

Here is my code:

Code: Select all

    #include <QCoreApplication>

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dcfilefo.h"
#include "dcmtk/dcmdata/libi2d/i2d.h"
#include "dcmtk/dcmdata/libi2d/i2djpgs.h"
#include "dcmtk/dcmdata/libi2d/i2dplsc.h"
#include "dcmtk/dcmdata/dctk.h"

#include <iostream>
#include <filesystem>
#include <string>

namespace fs = std::filesystem;

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

    Image2Dcm i2d;
    E_TransferSyntax writeXfer;
    DcmDataset* dcmDataset = nullptr;
    bool firstFileProcessed = false;  // to track if the first JPEG file has been processed
    size_t i = 1; // Counter for frames (JPEG files)

    std::string dirPath = "C:/Users/user/Pictures/0__dicoms/live"; // Directory containing JPEG images

    I2DOutputPlug* outPlug = new I2DOutputPlugSC();

    // Iterate over all files in the directory
    for (const auto& entry : fs::directory_iterator(dirPath))
    {
        const auto& path = entry.path(); // Get the path for the current image

        I2DJpegSource* jpegSrc = new I2DJpegSource();

        // Convert file path to DCMTK string type
        OFString imagePath(path.string().c_str());
        jpegSrc->setImageFile(imagePath);

        if (!firstFileProcessed)
        {
            // Convert the first frame (from the first file)
            i2d.convertFirstFrame(jpegSrc, outPlug, i++, dcmDataset, writeXfer);
            firstFileProcessed = true;
        }
        else
        {
            // Convert subsequent frames
            i2d.convertNextFrame(jpegSrc, i++);
        }

        delete jpegSrc;
    }

    delete outPlug;

    std::cout << "Processed " << i - 1 << " JPEG files" << std::endl;

    dcmDataset->putAndInsertString(DCM_NumberOfFrames, std::to_string(i - 1).c_str());
    dcmDataset->putAndInsertString(DCM_PatientName, "John Doe");
    dcmDataset->putAndInsertString(DCM_StudyDate, "2024-10-08");
    dcmDataset->putAndInsertString(DCM_Manufacturer, "Manufacturer Name");
    dcmDataset->putAndInsertString(DCM_PatientID, "987654320");
    dcmDataset->putAndInsertString(DCM_PatientSex, "Male");
    dcmDataset->putAndInsertString(DCM_PatientAge, "38 Years");
    dcmDataset->putAndInsertString(DCM_PatientWeight, "95 Kilograms");
    dcmDataset->putAndInsertString(DCM_SOPClassUID, UID_SecondaryCaptureImageStorage);

    // unique SOP Instance UID
    char uid[100];
    dcmDataset->putAndInsertString(DCM_SOPInstanceUID, dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT));

    // Constants for setting image orientation
    const std::string CORONAL = "1.0\\0.0\\0.0\\0.0\\0.0\\-1.0";
    const std::string SAGITTAL = "0.0\\1.0\\0.0\\0.0\\0.0\\-1.0";

    // Set the image orientation
    dcmDataset->putAndInsertString(DCM_ImageOrientationPatient, SAGITTAL.c_str());

    DcmFileFormat dcmff(dcmDataset);

    /**
     *  Save the DICOM output file
     */
    std::string outputFilePath = dirPath + "/output.dcm";
    OFCondition status = dcmff.saveFile(outputFilePath.c_str(), writeXfer);

    if (status.good())
    {
        std::cout << "DICOM file saved successfully at " << outputFilePath << std::endl;
    }
    else
    {
        std::cerr << "Error saving DICOM file: " << status.text() << std::endl;
    }


    return a.exec();
}
I'm using the following line to ensure the source frames are in the sagittal area:

Code: Select all

dcmDataset->putAndInsertString(DCM_ImageOrientationPatient, SAGITTAL.c_str());
However, viewers like 3D Slicer or ITK-SNAP display my frames in axial orientation instead:
Image Image
In other words, I want the viewers to show my images in the sagittal section, but they are displayed in axial.

What am I doing wrong when setting the sagittal orientation for the DICOM dataset?
Is there a way to create a DCM file such that 3D Slicer or ITK-SNAP will display my images in their sagittal section?

Thank you for your help!

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

Re: Setting sagittal orientation for DICOM Dataset

#2 Post by Marco Eichelberg »

You are generating DICOM files using either the Multi-frame Grayscale Byte SC Image Storage or the Multi-frame True Color SC Image Storage SOP Class. Neither of these SOP classes support the Image Plane Module, where the (0020,0037) Image Orientation (Patient) attribute is defined. In brief, you are adding an attribute that does not belong there and that will thus be ignored by many viewers.

The Multi-frame SC SOP Classes instead support some functional group macros where this information can be stored. See DICOM Part 3, Table A.8-5b.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest