minimal example creating a Dicom image with short signed pixel data

722 views
Skip to first unread message

stephen fogarasi

unread,
Apr 29, 2016, 10:06:22 AM4/29/16
to dcm4che
The only example of creating a dcm4che3 dicom image is the jpg to dcm converter code. Ive tried working that for hours to get a signed short int buffer, using transfer syntax of explicit VR little endian to work but the image either cannot pass dicom validation or be viewed ( actually sometimes it does pass, but it wont display - I'm using a Weasis and Sante to test viewability and dcmdump to test for validity).

Im pretty sure that the Tag.Item marker should not be there.

My code fragment is below, and some of the options combination Ive tried are commented out. Im pretty sure that the first option should have worked, without any extra tag markers. Can some one give me some pointers here ?

short[] pixelBuffer = null;


public boolean writeBuffer ( DicomOutputStream dos ) {

boolean status = false;

   try {

   // ...transfer everything as byte stream...

    

   //dos.writeHeader(Tag.PixelData, VR.OB, pixelBuffer.length * 2);

   //dos.writeHeader(Tag.PixelData, VR.OW, pixelBuffer.length * 2);     

   //dos.writeHeader(Tag.PixelData, VR.OW, pixelBuffer.length );

   //dos.writeHeader(Tag.PixelData, VR.OW, -1);

   dos.writeHeader(Tag.PixelData, VR.OB, -1);


    //dos.writeHeader(Tag.Item, null, 0);

    dos.writeHeader(Tag.Item, null, pixelBuffer.length * 2 );

      

    System.out.println ( "Pixel buffer length : " + pixelBuffer.length * 2);

  

    // ...need to convert the short array to a byte array...

    ByteBuffer buffer = ByteBuffer.allocate( pixelBuffer.length * 2 );

    buffer.order( ByteOrder.LITTLE_ENDIAN );

    buffer.asShortBuffer().put(pixelBuffer);

    byte[] bytes = buffer.array();


    // ...wont have to pad to even byte length since we are using shorts...

    dos.write( bytes, 0, pixelBuffer.length * 2 );

    dos.writeHeader(Tag.SequenceDelimitationItem, null, 0);

    status = true;

    } catch ( IOException e ) {

        logger.error ( "Error ! Exception thrown writing pixeldata to DicomOutputStream : " + e.getMessage() );

    } catch ( Exception e ) {

        logger.error ( "Error ! Unknown Exception thrown writing pixeldata to DicomOutputStream : " + e.getMessage() );

    }

    return status;

}



stephen fogarasi

unread,
Apr 29, 2016, 10:27:44 AM4/29/16
to dcm4che
OK, so realized that the Tag.sequenceDeliminationItem at the end was causing it all. So a minimal code fragment to stick a short buffer at the end of a DicomOutputStream is :

....
     short[] pixelBuffer;
....
    dos.writeHeader(Tag.PixelData, VR.OB, pixelBuffer.length * 2);

   
   // ...need to convert the short array to a byte array...
   
   ByteBuffer buffer = ByteBuffer.allocate( pixelBuffer.length * 2 );
   buffer.order( ByteOrder.LITTLE_ENDIAN );
   buffer.asShortBuffer().put(pixelBuffer);
   byte[] bytes = buffer.array();
   
   // ...wont have to pad to even byte length since we are using shorts...
   
   dos.write( bytes, 0, pixelBuffer.length * 2 ); 

   ....

   dos.close();


Dimitri Pianeta

unread,
Apr 29, 2016, 12:46:08 PM4/29/16
to dcm4che
Seeing my code in the first method pixelData that put an input file dicom and output file.

The method  Attributes dcm = loadDicomObject(input); put to read of medata and

- int value[][] = extractData(raster); --> put matrice pixel of input image
- int value2[] =getPixelData(value);  --> create table pixel of put in the tag pixel Data

 outputPicture( dcm, value2, h, w, output); --> method that create image dicom


ChangePixelData.java

Karthik Vadla

unread,
Aug 15, 2016, 7:30:32 PM8/15/16
to dcm4che
Hi Dimitri, 

I'm exactly looking for the same piece of work you did.  I'm trying to get the pixel data using dcm4che3 in scala. I want to create a matrix out of pixel data to do some transformations.


Before reading pixel data, i tried to get file metadata. I have End of file exception .
Can you please help me with this.

My code snippet in scala:
-----------------------------------

val dicomRdd = sparkContext.wholeTextFiles("dicomimages")
      val printbytes = dicomRdd.map { case (filepath, filedata) => filedata.getBytes().length }
      printbytes.foreach(println)

      val disRdd = dicomRdd.map { case (filepath, filedata) => new DicomInputStream(new ByteArrayInputStream(filedata.getBytes())) }

      val dm = disRdd.map {
        case dis =>
           val fmi = dis.getFileMetaInformation
           val data = dis.readDataset(-1, -1)
           data
      }

      dm.foreach(println)


Error:
--------

Job aborted due to stage failure: Task 0 in stage 1.0 failed 1 times, most recent failure: Lost task 0.0 in stage 1.0 (TID 1, localhost): java.io.EOFException
at org.dcm4che3.util.StreamUtils.readFully(StreamUtils.java:63)
at org.dcm4che3.io.DicomInputStream.readFully(DicomInputStream.java:383)
at org.dcm4che3.io.DicomInputStream.readValue(DicomInputStream.java:711)
at org.dcm4che3.io.DicomInputStream.readValue(DicomInputStream.java:536)


Please see attachment for complete error.



Thanks 
Karthik
eof_exception.txt
Reply all
Reply to author
Forward
0 new messages