Friday, January 21, 2022

Read the Dicom header information using dcm4che in Java

In this tutorial, we are going to learn how to get the Dicom file header information using dcm4che.

DICOM (Digital Imaging and Communications in Medicine) is the ubiquitous standard in the radiology and cardiology imaging industry for the exchange and management of images and image-related information.

For Dicom standard information please visit Dicom Library.

Before start writing code we need to use a couple of jar files. Download all the Dicom library files from the bundle. This bundle contains a lot of jar files we will use only the desired one.

Extract the downloaded file, go to the lib folder and grab two jar files. dcm4che-core-5.25.1.jar and another is slf4j-api-1.7.32.jar, the verison might be different at the time of implementation.

Now, create a folder called libs inside the project directory and add the jar file and load it from the IDE.

Loading Jar file in Maven Project:

<dependency>
          <groupId>com.dcm4che</groupId>
          <artifactId>dcm4che</artifactId>
          <version>20220117</version>
          <scope>system</scope>
          <systemPath>${basedir}/libs/dcm4che-core-5.25.1.jar</systemPath>
      </dependency>
<dependency>
          <groupId>com.slf4j</groupId>
          <artifactId>slf4j</artifactId>
          <version>20220217</version>
          <scope>system</scope>
          <systemPath>${basedir}/libs/slf4j-api-1.7.32.jar</systemPath>
      </dependency>

Note: use the downloaded jar file name.

Loading Jar in Gradle Project:

Add the following inside dependencies in build.gradle file.

dependencies {
//other dependencies
 
compile fileTree(dir: 'libs', include: '*.jar')
}

Let's create the java class ReadHeaderInfo.java to read the header information

public static void main(String[] args) {
        String inputDicomFilePath = "path/to/dicom/N2D_0001.dcm";
        try {
            readHeader(inputDicomFilePath);
        } catch (IOException e) {
            System.out.println("Error due to: "+e.getMessage());
        }
    }
private static void readHeader(String inputPath) throws IOException {
        File file = new File(inputPath);
        DicomInputStream dis = new DicomInputStream(file);
        Attributes attributes = dis.readDataset();
        int[] tags = attributes.tags();
        System.out.println("Total tag found in the given dicom file: "+tags.length);
        for (int tag: tags) {
            String tagAddress = TagUtils.toString(tag);
            String vr = attributes.getVR(tag).toString();
            System.out.println("Tag Address: " + tagAddress + " VR: " + vr);
        }
    }

Basically, we are using the Dicom-core classes from the jar file to read the file information. We are providing the sample Dicom file and reading the Tag Address and VR available in that file. 

DicomInputStream is used to read the file. dis.readDataset() read all the tags available in that file and finally, we are getting Tag Address and VR and outputting the values.

Output:

Total tag found in the given dicom file: 54
Tag Address: (0008,0008) VR: CS
Tag Address: (0008,0016) VR: UI
Tag Address: (0008,0018) VR: UI
Tag Address: (0008,0020) VR: DA
Tag Address: (0008,0021) VR: DA
Tag Address: (0008,0022) VR: DA
Tag Address: (0008,0023) VR: DA
Tag Address: (0008,0030) VR: TM
Tag Address: (0008,0031) VR: TM
Tag Address: (0008,0032) VR: TM
Tag Address: (0008,0033) VR: TM
Tag Address: (0008,0050) VR: SH
Tag Address: (0008,0060) VR: CS
Tag Address: (0008,0070) VR: LO
Tag Address: (0008,0080) VR: LO
Tag Address: (0008,0090) VR: PN
Tag Address: (0008,1030) VR: LO
Tag Address: (0008,103E) VR: LO
Tag Address: (0008,1090) VR: LO
Tag Address: (0010,0010) VR: PN
Tag Address: (0010,0020) VR: LO
Tag Address: (0010,0030) VR: DA
Tag Address: (0010,0040) VR: CS
Tag Address: (0010,1000) VR: LO
Tag Address: (0010,1010) VR: AS
Tag Address: (0010,1030) VR: DS
Tag Address: (0010,21C0) VR: US
Tag Address: (0018,0050) VR: DS
Tag Address: (0018,0088) VR: DS
Tag Address: (0018,1020) VR: LO
Tag Address: (0018,1030) VR: LO
Tag Address: (0020,000D) VR: UI
Tag Address: (0020,000E) VR: UI
Tag Address: (0020,0010) VR: SH
Tag Address: (0020,0011) VR: IS
Tag Address: (0020,0012) VR: IS
Tag Address: (0020,0013) VR: IS
Tag Address: (0020,0020) VR: CS
Tag Address: (0020,0032) VR: DS
Tag Address: (0020,0037) VR: DS
Tag Address: (0020,0052) VR: UI
Tag Address: (0028,0002) VR: US
Tag Address: (0028,0004) VR: CS
Tag Address: (0028,0010) VR: US
Tag Address: (0028,0011) VR: US
Tag Address: (0028,0030) VR: DS
Tag Address: (0028,0100) VR: US
Tag Address: (0028,0101) VR: US
Tag Address: (0028,0102) VR: US
Tag Address: (0028,0103) VR: US
Tag Address: (0028,1052) VR: DS
Tag Address: (0028,1053) VR: DS
Tag Address: (0028,1054) VR: LO
Tag Address: (7FE0,0010) VR: OW

Extracting the header information is important to visualize the Dicom file information.

The overall code implementation looks as below:

package dicom.dcm4che;

import org.dcm4che3.data.Attributes;
import org.dcm4che3.io.DicomInputStream;
import org.dcm4che3.util.TagUtils;

import java.io.File;
import java.io.IOException;

public class ReadHeaderInfo {

    public static void main(String[] args) {
        String inputDicomFilePath = "path/to/dicom/N2D_0001.dcm";
        try {
            readHeader(inputDicomFilePath);
        } catch (IOException e) {
            System.out.println("Error due to: "+e.getMessage());
        }
    }

    private static void readHeader(String inputPath) throws IOException {
        File file = new File(inputPath);
        DicomInputStream dis = new DicomInputStream(file);
        Attributes attributes = dis.readDataset();
        int[] tags = attributes.tags();
        System.out.println("Total tag found in the given dicom file: "+tags.length);
        for (int tag: tags) {
            String tagAddress = TagUtils.toString(tag);
            String vr = attributes.getVR(tag).toString();
            System.out.println("Tag Address: " + tagAddress + " VR: " + vr);
        }
        dis.close();
    }

}
Share:

0 comments:

Blog Archive