如何从字节创建DICOM图像(DCMTK)

时间:2015-01-27 13:41:21

标签: c++ visual-studio-2013 compression dicom dcmtk

我想在可以创建DICOM图像的现有项目中使用DCMTK 3.6.1库。我想使用这个库,因为我想对DICOM图像进行压缩。在新的解决方案(Visual Studio 2013 / C ++)中遵循DCMTK官方文档中的示例,我有这个代码,它可以正常工作。

using namespace std;

int main()
{

    DJEncoderRegistration::registerCodecs();
    DcmFileFormat fileformat;

    /**** MONO FILE ******/

    if (fileformat.loadFile("Files/test.dcm").good())
    {
        DcmDataset *dataset = fileformat.getDataset();
        DcmItem *metaInfo = fileformat.getMetaInfo();
        DJ_RPLossless params; // codec parameters, we use the defaults

        // this causes the lossless JPEG version of the dataset 
        //to be created EXS_JPEGProcess14SV1
        dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);   

        // check if everything went well
        if (dataset->canWriteXfer(EXS_JPEGProcess14SV1))
        {
            // force the meta-header UIDs to be re-generated when storing the file
            // since the UIDs in the data set may have changed
            delete metaInfo->remove(DCM_MediaStorageSOPClassUID);
            delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID);

            metaInfo->putAndInsertString(DCM_ImplementationVersionName, "New Implementation Version Name");
            //delete metaInfo->remove(DCM_ImplementationVersionName);
            //dataset->remove(DCM_ImplementationVersionName);

            // store in lossless JPEG format
            fileformat.saveFile("Files/carrellata_esami_compresso.dcm", EXS_JPEGProcess14SV1);
        }
    }

    DJEncoderRegistration::cleanup();

    return 0;
}

现在我想在现有的C ++应用程序中使用相同的代码

if (infoDicom.arrayImgDicom.GetSize() != 0)  //Things of existing previous code
      {

     //I have added here the registration
     DJEncoderRegistration::registerCodecs(); // register JPEG codecs

     DcmFileFormat fileformat;
     DcmDataset *dataset = fileformat.getDataset();
     DJ_RPLossless params;

      dataset->putAndInsertUint16(DCM_Rows, infoDicom.rows);
     dataset->putAndInsertUint16(DCM_Columns, infoDicom.columns,);
     dataset->putAndInsertUint16(DCM_BitsStored, infoDicom.m_bitstor);
     dataset->putAndInsertUint16(DCM_HighBit, infoDicom.highbit);
     dataset->putAndInsertUint16(DCM_PixelRepresentation, infoDicom.pixelrapresentation);
     dataset->putAndInsertUint16(DCM_RescaleIntercept, infoDicom.rescaleintercept);
     dataset->putAndInsertString(DCM_PhotometricInterpretation,"MONOCHROME2");
     dataset->putAndInsertString(DCM_PixelSpacing, "0.086\\0.086");  
     dataset->putAndInsertString(DCM_ImagerPixelSpacing, "0.096\\0.096");
     BYTE* pData = new BYTE[sizeBuffer];

     LPBYTE   pSorg;

     for (int nf=0; nf<iNumberFrames; nf++)
     {
        //this contains all the PixelData and I put it into the dataset
        pSorg = (BYTE*)infoDicom.arrayImgDicom.GetAt(nf);
        dataset->putAndInsertUint8Array(DCM_PixelData, pSorg, sizeBuffer);

        dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);
        //and I put it in my data set            


         //but this IF return false so che canWriteXfer fails...
        if (dataset->canWriteXfer(EXS_JPEGProcess14SV1))
        {
           dataset->remove(DCM_MediaStorageSOPClassUID);
           dataset->remove(DCM_MediaStorageSOPInstanceUID);
        }

            //the saveFile fails too, and the error is "Pixel 
//rappresentation  non found" but I have set the Pixel rep with 
//dataset->putAndInsertUint16(DCM_PixelRepresentation, infoDicom.pixelrapresentation);

   OFCondition status = fileformat.saveFile("test1.dcm", EXS_JPEGProcess14SV1);

     DJEncoderRegistration::cleanup();

        if (status.bad())
        {
           int error = 0; //only for test
        }

        thefile.Write(pSorg, sizeBuffer); //previous code
     }

实际上我用一帧上的图像进行测试,因此for循环只进行一次。我不明白为什么如果我选择dataset->chooseRepresentation(EXS_LittleEndianImplicit, &params);dataset->chooseRepresentation(EXS_LittleEndianEXplicit, &params);完美无缺,但选择dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);

时则不然

如果我在第一个应用程序中使用相同的图像,我可以毫无问题压缩图像......

编辑:我认为要解决的主要问题是返回“找不到标记”的status = dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &rp_lossless)。我怎么知道这个标签丢失了?

EDIT2 :根据DCMTK论坛的建议,我添加了关于分配位的标签,现在工作用于少数图像,但不是全部。对于某些图像,我再次“找不到标签”:我怎么知道其中一个标签丢失了?通常最好插入所有标签?

2 个答案:

答案 0 :(得分:2)

我解决了添加标签DCM_BitsAllocated和DCM_PlanarConfiguration的问题。这是错过的标签。我希望这对某人有用。

答案 1 :(得分:0)

在应用数据后,至少应该调用函数chooseRepresentation。

**dataset->putAndInsertUint8Array(DCM_PixelData, pSorg, sizeBuffer);**
dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);