Friday, January 21, 2022

Modify Dicom file metadata using dcm4che in Java

How to modify Dicom file metadata using dcm4che.

Please follow our previous tutorials on dcm4che.

Read the Dicom header information using dcm4che in Java

Read Dicom file metadata using dcm4che in Java

Let's create a sample java class ModifyMetadata.java to modify the available metadata in the given Dicom file.

 public static void main(String[] args) {
        String inputDicomPath = "path/to/dicom/N2D_0001.dcm";
        String outputDicomPath = "output/path/to/dicom/N2D_0001.dcm";
        try {
            modifyMetadata(inputDicomPath, outputDicomPath);
        } catch (IOException e) {
            System.out.println("Error due to: "+e.getMessage());
        }
    }
 private static void modifyMetadata(String inputDicomPath, String outputDicomPath) throws IOException {
        File file = new File(inputDicomPath);
        DicomInputStream dis = new DicomInputStream(file);
        Attributes attributes = dis.readDataset();
        if (attributes.contains(Tag.PatientID))
            attributes.setString(Tag.PatientID, VR.LO, "L500400");
        if (attributes.contains(Tag.PatientName))
            attributes.setString(Tag.PatientName, VR.PN, "Test name");
        DicomOutputStream dos = new DicomOutputStream(new File(outputDicomPath));
        attributes.writeTo(dos);
        dis.close();
        dos.close();
    }

Here we are using the dcm4che standard library classes. Please follow our previous tutorial to set up dcm4che jar files. We have created modifyMetadata() method which accepts two arguments one for input Dicom image path and another is output Dicom image path where we are writing the file after modification.

DicomInputStream will read the file. We are reading all the available data set to get the attributes and try to modify the patient ID and the patient name.

After modification, DicomOutputStream will write the modified attributes to the output path. If we open the image and see, the name and patient id are changed. We can modify any available metadata similar to the above example. For Tag and VR follow the Dicom tag library


The overall code implementation looks as below

package dicom.dcm4che;

import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.VR;
import org.dcm4che3.io.DicomInputStream;
import org.dcm4che3.io.DicomOutputStream;

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

public class ModifyMetadata {

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

    private static void modifyMetadata(String inputDicomPath, String outputDicomPath) throws IOException {
        File file = new File(inputDicomPath);
        DicomInputStream dis = new DicomInputStream(file);
        Attributes attributes = dis.readDataset();
        if (attributes.contains(Tag.PatientID))
            attributes.setString(Tag.PatientID, VR.LO, "L500400");
        if (attributes.contains(Tag.PatientName))
            attributes.setString(Tag.PatientName, VR.PN, "Test name");
        DicomOutputStream dos = new DicomOutputStream(new File(outputDicomPath));
        attributes.writeTo(dos);
        dis.close();
        dos.close();
    }
}
Share:

0 comments:

Blog Archive