Combining multiple images into single multi-frame

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
curquhart
Posts: 2
Joined: Tue, 2020-07-21, 09:32

Combining multiple images into single multi-frame

#1 Post by curquhart » Tue, 2020-07-21, 11:33

I'm new to the health domain and DICOM. I've used dcmtk to do basic stuff such as opening and updating instances from files and streams.

But now I need to take multiple images from disk which are in the same series but should be a single multi-frame file. I've obviously Googled but now I find myself getting lost in DcmPixelItem, DcmPixelSequence and DcmPixelData classes.

I'm probably going about this completely wrong, and there may be more information required before you can provide an answer, but here's the code:

Code: Select all


DcmPixelSequence * mergedPixelSequence = new DcmPixelSequence(DcmTagKey{ DCM_PixelData });
shared_ptr<DcmFileFormat> mergedInstance = std::make_shared<DcmFileFormat>();

DcmDataset * mergedDataset = mergedInstance->getDataset();

E_TransferSyntax xferSyntax = EXS_Unknown;

// for each separate image instance, add it to the 'merged' multi-frame instance
for (const auto& instance : series.second->getInstances())
{
  DcmDataset* ds = instance->dataset->dcmFile->getDataset();

  DcmElement * element = nullptr;                        
  if (instance->dataset->dcmFile->getDataset()->findAndGetElement(DcmTagKey{ DCM_PixelData }, element).good())
  {
    if (element)
    {   
      const DcmRepresentationParameter* rep = NULL;
      // Find the key that is needed to access the right representation of the data within DCMTK
      DcmPixelData * pixelElement = OFstatic_cast(DcmPixelData*, element);
      pixelElement->getOriginalRepresentationKey(xferSyntax, rep);

      DcmPixelSequence * dseq = NULL;
      OFCondition status = pixelElement->getEncapsulatedRepresentation(xferSyntax, rep, dseq);

      if (status.good())
      {
        DcmPixelItem * pixitem = NULL;
        // Access first frame (skipping offset table)
        dseq->getItem(pixitem, 1);

        if (pixitem)
        {
          mergedPixelSequence->insert(pixitem);
        }                                    
      }
    }                            
  }
}

if (mergedDataset && mergedPixelSequence)
{
    mergedDataset->insert(mergedPixelSequence);
}

stringstream ss;
ss << "d:\\target\\" << mergedSeriesId << ".dcm";

const OFCondition fileWriteStatus = mergedInstance->saveFile(ss.str().c_str());

if (!fileWriteStatus.good())
{
    logg(fileWriteStatus.text());  // This outputs "Illegal call, perhaps wrong parameters"
}
The fileWriteStatus returned by saveFile() has error text: "Illegal call, perhaps wrong parameters"


Thanks

Michael Onken
DCMTK Developer
Posts: 1822
Joined: Fri, 2004-11-05, 13:47
Location: Oldenburg, Germany
Contact:

Re: Combining multiple images into single multi-frame

#2 Post by Michael Onken » Thu, 2020-07-23, 07:17

Hi,

I think the best approach is to look into the dcmcjpeg application or more exactly, into the class that handles the encoding in dcmjpeg/libsrc/djcodece.cc. Look for example into DJCodecEncoder::encodeColorImage(...) that shows how you can construct a pixel sequence from scratch.

Best regards,
Michael

curquhart
Posts: 2
Joined: Tue, 2020-07-21, 09:32

Re: Combining multiple images into single multi-frame

#3 Post by curquhart » Thu, 2020-07-23, 18:31

Hi
Thanks, pointing me to that source file helped.

Callum

Post Reply

Who is online

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