简体   繁体   English

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

[英]How create a DICOM image from byte (DCMTK)

I want to use the DCMTK 3.6.1 library in an existing project that can create DICOM image. 我想在可以创建DICOM图像的现有项目中使用DCMTK 3.6.1库。 I want to use this library because I want to make the compression of the DICOM images. 我想使用该库,因为我想对DICOM图像进行压缩。 In a new solution (Visual Studio 2013/C++) Following the example in the DCMTK official documentation, I have this code, that works properly. 在一个新的解决方案(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;
}

Now I want to use the same code in an existing C++ application where 现在,我想在现有的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
     }

Actually I made test with image that have on one frame, so the for cycle is done only one time. 实际上,我对一帧图像进行了测试,因此for循环仅执行一次。 I don't understand why if I choose dataset->chooseRepresentation(EXS_LittleEndianImplicit, &params); 我不明白为什么我选择数据dataset->chooseRepresentation(EXS_LittleEndianImplicit, &params); or dataset->chooseRepresentation(EXS_LittleEndianEXplicit, &params); 或数据dataset->chooseRepresentation(EXS_LittleEndianEXplicit, &params); works perfectly but not when I choose dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params); 工作正常,但当我选择数据dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &params);

If I use the same image in the first application, I can compress the image without problems... 如果在第一个应用程序中使用相同的图像,则可以无问题地压缩图像...

EDIT: I think the main problem to solve is the status = dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &rp_lossless) that return "Tag not found". 编辑:我认为要解决的主要问题是status = dataset->chooseRepresentation(EXS_JPEGProcess14SV1, &rp_lossless)返回“找不到标记”。 How can I know wich tag is missed? 我怎么知道缺少wich标签?

EDIT2 : As suggest in the DCMTK forum I have added the tag about the Bits Allocated and now works for few images, but non for all. EDIT2 :如DCMTK论坛中的建议,我添加了有关“已分配的比特”的标记,现在仅适用于少量图像,但不适用于所有图像。 For some images I have again "Tag not found": how can I know wich one of tags is missing? 对于某些图像,我再次遇到“找不到标签”:如何知道其中一个标签缺失? As a rule it's better insert all the tags? 通常最好插入所有标签?

I solve the problem adding the tags DCM_BitsAllocated and DCM_PlanarConfiguration. 我解决了添加标签DCM_BitsAllocated和DCM_PlanarConfiguration的问题。 This are the tags that are missed. 这是丢失的标签。 I hope that is useful for someone. 我希望这对某人有用。

At least you should call the function chooseRepresentation, after you have applied the data. 应用数据后,至少应该调用函数selectRepresentation。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM