c++ - 在多帧 dicom 图像中插入修改后的像素数据?

标签 c++ image dicom

首先我应该提到我为此目的使用了 Dcmtk 库。

我已经学会了如何修改单帧 dicom 图像的像素数据。现在,我正在尝试对多帧图像执行相同的操作。我可以提取所有必要的信息,甚至可以为每一帧单独提取像素数据并修改它们。但是当我必须插入修改后的像素数据时,问题就出现了。如果是单帧,我使用 DcmDataset 中的方法:

putAndInsertUint8Array()

但是对于多帧图像,我看不到任何类似的选项。我在 DcmElement 中使用此方法获取每一帧的像素数据:

getUncompressedFrame()

我只需将帧索引放入其中即可获取相应的像素数据。但是在插入时我找不到任何这样的选项。我的编程代码如下:

int main()
{
   MdfDatasetManager file;
   if(EC_Normal==file.loadFile("test.dcm",ERM_autoDetect,EXS_Unknown))
   {
      DcmDataset *dataset = file.getDataset();
      E_TransferSyntax xfer= dataset->getOriginalXfer();
      bool OriginallyCompressed=false;
      if(xfer!=0 && xfer !=1 && xfer!=2 && xfer!=3)
      {
         OriginallyCompressed=true;
         DJDecoderRegistration::registerCodecs();
         if(EC_Normal==dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL))
         {
            if(dataset->canWriteXfer(EXS_LittleEndianExplicit))
            {
               cout<<"Originally it's a compressed image, but now decompressed!\n";
            }
         }
      }
      DcmElement* element=NULL;
      Uint16 rows = 0;
      Uint16 cols = 0;
      Uint16 samplePerPixel = 0;
      Uint16 planarConfiguration = 0;
      int index=0;
      // I've fixed these values but later I will change them to dinaymic and make it work as per user's wish.
      int ymin=50;//minimum rows
      int ymax=500;//maximum rows
      int xmin=100;//Minimum columns
      int xmax=600;//Maximum columns
      if(EC_Normal==dataset->findAndGetUint16(DCM_Rows, rows))
      {
         if(EC_Normal==dataset->findAndGetUint16(DCM_Columns, cols))
         {
            if(EC_Normal==dataset->findAndGetUint16(DCM_SamplesPerPixel,samplePerPixel))
            {   
               if(EC_Normal==dataset->findAndGetUint16(DCM_PlanarConfiguration,planarConfiguration))
               {
                  if(EC_Normal==dataset->findAndGetElement(DCM_PixelData,element))
                  {
                     Uint32 startFragment=0;
                     Uint32 sizeF=0;
                     element->getUncompressedFrameSize(dataset,sizeF);
                     long int numOfFrames=0;
                     dataset->findAndGetLongInt(DCM_NumberOfFrames,numOfFrames);
                     for(int i=0;i<int(numOfFrames);i++)
                     {
                        Uint8 * buffer = new Uint8[int(sizeF)];
                        OFString decompressedColorModel=NULL;
                        DcmFileCache * cache=NULL;
                        if(EC_Normal==element->getUncompressedFrame(dataset,i,startFragment,buffer,sizeF,decompressedColorModel,cache))
                        {
                           Uint8 * newBuffer = new Uint8[int(sizeF)];
                           if(buffer != NULL)
                           {
                              for(unsigned long y = 0; y < rows; y++)
                              {
                                 for(unsigned long x = 0; x < cols; x++)
                                 {
                                    if(planarConfiguration==0)
                                    {
                                       if(x>xmin && x<xmax && y>ymin && y<ymax)
                                       {
                                          index=(x + y +  y*(cols-1))*samplePerPixel;
                                          newBuffer[index]  = 0;
                                          newBuffer[index + 1]  = 0;
                                          newBuffer[index +2]  = 0;
                                       }
                                       else
                                       {
                                          index=(x + y +  y*(cols-1))*samplePerPixel;
                                          newBuffer[index]  = buffer[index];
                                          newBuffer[index + 1]  = buffer[index + 1];
                                          newBuffer[index + 2]  = buffer[index + 2];
                                       }
                                    }
                                 }
                              }
                           }
                           delete newBuffer;
                        }
                        delete buffer;
                     }
                  }
               }
            }
         }
      }
   }
   return 0;
}

如果我设法找到一种方法为每一帧插入修改后的像素数据,这个程序就完成了。请建议我应该做什么。或者请告诉我,如果你知道,多帧 dicom 图像中所有帧的整个像素数据是如何存储在一起的。然后也许我可以从所有帧中获取整个像素数据并修改它们,然后尝试将整个修改后的像素数据插入在一起。

最佳答案

案例#1,未压缩的像素数据:

///. get PixelData element in DCM dataset
pDcmDataSet->findAndGetPixelData(...);
///. get pixels in PixelData element
pDcmPixelDataSet->findAndGetOW(...);

您将获得所有帧的全部像素数据。

案例#2,压缩像素数据:

///. get PixelData element in DCM dataset
pDcmDataSet->findAndGetPixelData(...);
///. get PixelSequence in PixelData element
pPixelData->getEncapsulatedRepresentation(...)
///. get PixelItem in PixelSequence
pDcmPixelSequence->getItem(...);
///. get frame in Pixel Item
pPixelItem->getUint8Arrary(...);

您将获得一帧压缩图像。

关于c++ - 在多帧 dicom 图像中插入修改后的像素数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16984215/

相关文章:

c++ - 如何在 C++/Linux 中创建目录树?

c++ - 如何将数组存储在 Sqlite3 的一列中?

c++ - 如何进行 Gtk TextIter 算术

image - ASIHttpRequest上传图片

c - SDL_SetColorKey不设置背景透明

javascript - 简单图像测验困惑 : Next Button Triggers a New Full Screen Image

ios - 如何在支持8位无符号整数的设备上合法显示DICOM 16无符号整数?

c++ - DCMTK 字符集 Unicode 错误

c++ - Eclipse C/C++ 找不到编译器包含目录 - 甚至无法构建 hello world

体积数据的 DICOM 基础知识