Monday 26 August 2013

Listing DICOM Header information with dcm4che 2

Hi All,

Some readers have asked me questions regarding how to access DICOM header information by DICOM Tag parameters. On this post I present you a quick tutorial on how to list all header information, including the Tag value, VR (value representation), Tag description, and the values of each field using the great dcm4che 2 toolkit.

(0008,0005) [CS] Specific Character Set [ISO_IR 100]
(0008,0008) [CS] Image Type [ORIGINAL]
(0008,0016) [UI] SOP Class UID [1.2.840.10008.5.1.4.1.1.2]
(0008,0020) [DA] Study Date [20040827]
(0008,0021) [DA] Series Date [20040827]
(0008,0022) [DA] Acquisition Date [20040827]
(0008,0023) [DA] Content Date [20040827]
(0008,0030) [TM] Study Time [100357.953000]
(0008,0031) [TM] Series Time [100607.062000]
(0008,0032) [TM] Acquisition Time [100622.688476]
(0008,0033) [TM] Content Time [100622.688476]
(0008,0050) [SH] Accession Number [null]
(0008,0060) [CS] Modality [CT]
(0008,0070) [LO] Manufacturer [SIEMENS]

Like the previous posts, we start coding a simple class with the default constructor. Let's name itListDicomHeader.


public class ListDicomHeader {

   public ListDicomHeader() {
      // TODO Auto-generated method stub
   }
   
   public static void main(String[] args) {
      // TODO Auto-generated method stub
   }
}

The next step is to code the method responsible for extracting the header info. Note that this method is recursive. This is done because some DICOM files bring encoded Items that may hold other DICOM objects denoted by de value representation SQ. So, we are handling also sequence information with this code. The method is written as follows:


public void listHeader(DicomObject object) {
   Iterator iter = object.datasetIterator();
   while(iter.hasNext()) {
      DicomElement element = iter.next();
      int tag = element.tag();
      try {
         String tagName = object.nameOf(tag);
         String tagAddr = TagUtils.toString(tag);
         String tagVR = object.vrOf(tag).toString();
         if (tagVR.equals("SQ")) {
            if (element.hasItems()) {
               System.out.println(tagAddr +" ["+  tagVR +"] "+ tagName);
               listHeader(element.getDicomObject());
               continue;
            }
         }    
         String tagValue = object.getString(tag);    
         System.out.println(tagAddr +" ["+ tagVR +"] "+ tagName +" ["+ tagValue+"]");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }  
}

Looking at the code, first we get an iterator to go through our DICOM dataset. Then we code a while loop to get each DICOM element present in the header. At each iteration a new DicomElement is kept so we can access its values. The tag variable holds the current Tag value. From then on there are some useful functions that may help us a lot. We can use the nameOf method fromDicomObject class to get the Tag description as a String. I also suggest you to have a look at the TagUtils class for other great functions. The vrOf function will return the value representation to the current element. 


Then comes the recursive part. We test the VR to see if it's a sequence (SQ), if so then we check if this element has any Items. Then if the answer is true we get the new object and call thelistHeader function again, starting the recursive loop. Each iteration then prints out the desired information.

Finally, to test this program we must a main method for this class. The method may be written as follows:


public static void main(String[] args) {
   DicomObject object = null;  
   try {
      DicomInputStream dis = new DicomInputStream(new File("c:/image.dcm"));
      object = dis.readDicomObject();
      dis.close();
   } catch (Exception e) {
      System.out.println(e.getMessage());
      System.exit(0);
   }
   ListDicomHeader list = new ListDicomHeader();
   list.listHeader(object);
}


That's it! Now we have a lot of information from our DICOM file header! Enjoy :)

No comments: