Replacing pixeldata

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

Replacing pixeldata

#1 Post by martinrame »

Hi, I would like to replace part of an image with my own pixel data, to anonymize it, for example.

I created the following code after reading many posts on the forum, but it doesn't work.

What I'm doing wrong?.

Code: Select all

/* lDataset is a DcmDataset already in memory */
const Uint8 * pixelData = NULL;
unsigned long pcount;
lDataset->findAndGetUint8Array(DCM_PixelData, pixelData, &pcount);
if(pixelData != NULL)
{
  for(unsigned long j = 0; j<pcount; j++)
  {
    pixelData = (Uint8 *)0;
    pixelData++;
  }
  if(lDataset->putAndInsertUint8Array(DCM_PixelData, pixelData, pcount) == EC_Normal)
  {
    DicomImage lNewImg(lDataset, lDataset->getOriginalXfer());
    lNewImg.writeBMP("output.bmp");
  }
}

Jörg Riesmeier
ICSMED DICOM Services
ICSMED DICOM Services
Posts: 2217
Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany

#2 Post by Jörg Riesmeier »

I'm sorry but more details would be needed in order to help you. E.g. what does "but it doesn't work" mean? Are the pixel data really stored with VR=OB (uncompressed)?

And, btw, "pixelData = (Uint8 *)0;" is probably not what you want: Setting the pixelData pointer to "0" (NULL) and then adding 1 to the address ;-)

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

#3 Post by martinrame »

Let me update the code a litle to show what I need:

Code: Select all

/* lDataset is a DcmDataset already in memory */ 
const Uint8 * pixelData = NULL; 
unsigned long pcount; 
lDataset->findAndGetUint8Array(DCM_PixelData, pixelData, &pcount); 
if(pixelData != NULL) 
{ 
  Uint8 * myPixelData[pcount];
  for(unsigned long j = 0; j<pcount; j++) 
  { 
    mypixelData = 0; 
  } 
  if(lDataset->putAndInsertUint8Array(DCM_PixelData, (const Uint8 *) mypixelData, pcount) == EC_Normal) 
  { 
    DicomImage lNewImg(lDataset, lDataset->getOriginalXfer()); 
    lNewImg.writeBMP("output.bmp"); 
  }  
}
What I want to do is to change the data contained in pixelData, but I get an exception on mypixelData = 0;

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

#4 Post by Michael Onken »

Hi,
What I want to do is to change the data contained in pixelData, but I get an exception on mypixelData = 0;
Well, you set it to 0, at least the first pixel cell, j-times! ;)

Code: Select all

  for(unsigned long j = 0; j<pcount; j++) 
  { 
    mypixelData = 0; 
  } 
Do you want to do instead

Code: Select all

  for(unsigned long j = 0; j<pcount; j++) 
  { 
    mypixelData[j] = 0; 
  } 
to set every pixel cell to the value 0? If so, there would be faster methods,e.g. with memset.

Michael

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

#5 Post by martinrame »

Finally I found what I wanted:

Code: Select all

        const Uint16 * pixelData = NULL;
        Uint16 lRows = 0;
        Uint16 lCols = 0;
        DcmDataset * lDataset = fileFormat->getDataset();
        lDataset->findAndGetUint16Array(DCM_PixelData, pixelData);
        lDataset->findAndGetUint16(DCM_Rows, lRows);
        lDataset->findAndGetUint16(DCM_Columns, lCols);
        
        int lCount = (unsigned long)lRows * lCols;
        Uint16 * mypixelData = new Uint16[lCount];

        if(pixelData != NULL)
        {
          for(unsigned long x = 0; x < lCols; x++)
          {
            for(unsigned long y = 0; y < lRows; y++)
            {
              if(((y > 20) && (y < 100)) && ((x > 20) && (x < 1000)))
              {
                mypixelData[x + y * lCols]  = 0;
              }
              else
                mypixelData[x + y * lCols]  = pixelData[x + y * lCols]; 
            } 
          }
          if(lDataset->putAndInsertUint16Array(DCM_PixelData, (const Uint16 *)mypixelData, lRows * lCols) == EC_Normal)
          {
            DicomImage lNewImg(lDataset, lDataset->getOriginalXfer());
            lNewImg.writeBMP("salida.bmp");
          }
          else
          {
            cout << "No se pudo escribir el pixeldata." << endl;
          }
       }
       delete pixelData;
       delete mypixelData;

The code above draws a white box on the image.

Now, a new question: How can I add text instead of the box?

Jörg Riesmeier
ICSMED DICOM Services
ICSMED DICOM Services
Posts: 2217
Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany

#6 Post by Jörg Riesmeier »

Maybe, the FreeType library can be used for this purpose ...

martinrame
Posts: 347
Joined: Mon, 2009-02-23, 19:57

#7 Post by martinrame »

Thanks, FreeType2 did the trick.

Post Reply

Who is online

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