How to read Dicom file metadata using dcm4che in Java.
dcm4che is a collection of open-source applications and utilities for the healthcare enterprise application for processing, manipulating, and analysis of medical images.
Please follow the tutorial for setting the dcm4che in our java application.
Let's create a java class DicomMetadataReader.java to read the available metadata in the given Dicom file.
public static void main(String[] args) {
String inputDicomFilePath = "path/to/dicom/N2D_0001.dcm";
try {
readMetadata(inputDicomFilePath);
} catch (IOException e) {
System.out.println("Error due to: "+e.getMessage());
}
}
private static void readMetadata(String dicomFilePath) throws IOException {
File file = new File(dicomFilePath);
DicomInputStream dis = new DicomInputStream(file);
Attributes attributes = dis.readDataset();
int[] tags = attributes.tags();
System.out.println("Total tag found in dicom file: "+tags.length);
for (int tag: tags) {
String tagAddress = TagUtils.toString(tag);
String tagValue = attributes.getString(tag);
System.out.println("Tag Address: " + tagAddress + " Value: " + tagValue);
}
dis.close();
}
Here, we are using standard classes from the dcm4che core library jar file. DicomInputStream will read the file. We are reading the available dataset of the file and getting tags. After that, we are looping through the available tags and get the tag address and corresponding tag value.
The sample Output:
Total tag found in dicom file: 54
Tag Address: (0008,0008) Value: DERIVED
Tag Address: (0008,0016) Value: 1.2.840.10008.5.1.4.1.1.4
Tag Address: (0008,0018) Value: 1.2.826.0.1.3680043.2.1143.1590429688519720198888333603882344634
Tag Address: (0008,0020) Value: 20130717
Tag Address: (0008,0021) Value: 20130717
Tag Address: (0008,0022) Value: 20130717
Tag Address: (0008,0023) Value: 20130717
Tag Address: (0008,0030) Value: 141500
Tag Address: (0008,0031) Value: 142035.93000
Tag Address: (0008,0032) Value: 132518
Tag Address: (0008,0033) Value: 142035.93
Tag Address: (0008,0050) Value: null
Tag Address: (0008,0060) Value: MR
Tag Address: (0008,0070) Value: BIOLAB
Tag Address: (0008,0080) Value: null
Tag Address: (0008,0090) Value: null
Tag Address: (0008,1030) Value: Hanke_Stadler^0024_transrep
Tag Address: (0008,103E) Value: anat-T1w
Tag Address: (0008,1090) Value: nifti2dicom
Tag Address: (0010,0010) Value: Jane_Doe
Tag Address: (0010,0020) Value: 02
Tag Address: (0010,0030) Value: 19660101
Tag Address: (0010,0040) Value: F
Tag Address: (0010,1000) Value: null
Tag Address: (0010,1010) Value: 42
Tag Address: (0010,1030) Value: 75
Tag Address: (0010,21C0) Value: 4
Tag Address: (0018,0050) Value: 0.666666686534882
Tag Address: (0018,0088) Value: 0.666666686534882
Tag Address: (0018,1020) Value: 0.4.11
Tag Address: (0018,1030) Value: anat-T1w
Tag Address: (0020,000D) Value: 1.2.826.0.1.3680043.2.1143.2592092611698916978113112155415165916
Tag Address: (0020,000E) Value: 1.2.826.0.1.3680043.2.1143.515404396022363061013111326823367652
Tag Address: (0020,0010) Value: 433724515
Tag Address: (0020,0011) Value: 401
Tag Address: (0020,0012) Value: 1
Tag Address: (0020,0013) Value: 1
Tag Address: (0020,0020) Value: L
Tag Address: (0020,0032) Value: -91.4495864331908
Tag Address: (0020,0037) Value: 0.999032176441525
Tag Address: (0020,0052) Value: 1.2.826.0.1.3680043.2.1143.6856184167807409206647724161920598374
Tag Address: (0028,0002) Value: 1
Tag Address: (0028,0004) Value: MONOCHROME2
Tag Address: (0028,0010) Value: 384
Tag Address: (0028,0011) Value: 274
Tag Address: (0028,0030) Value: 0.666666686534882
Tag Address: (0028,0100) Value: 16
Tag Address: (0028,0101) Value: 16
Tag Address: (0028,0102) Value: 15
Tag Address: (0028,0103) Value: 1
Tag Address: (0028,1052) Value: 0
Tag Address: (0028,1053) Value: 1
Tag Address: (0028,1054) Value: US
Tag Address: (7FE0,0010) Value: 0
Please follow the Dicom standard library for tag and its description.
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 DicomMetadataReader {
public static void main(String[] args) {
String inputDicomFilePath = "path/to/dicom/N2D_0001.dcm";
try {
readMetadata(inputDicomFilePath);
} catch (IOException e) {
System.out.println("Error due to: "+e.getMessage());
}
}
private static void readMetadata(String dicomFilePath) throws IOException {
File file = new File(dicomFilePath);
DicomInputStream dis = new DicomInputStream(file);
Attributes attributes = dis.readDataset();
int[] tags = attributes.tags();
System.out.println("Total tag found in dicom file: "+tags.length);
for (int tag: tags) {
String tagAddress = TagUtils.toString(tag);
String tagValue = attributes.getString(tag);
System.out.println("Tag Address: " + tagAddress + " Value: " + tagValue);
}
dis.close();
}
}