Learning The "J"

Home » DICOM » DICOM Tag editing using dcm4che

DICOM Tag editing using dcm4che

dcmlogo

dcm4che is a collection of open source applications and utilities for the healthcare enterprise. These applications have been developed in the Java programming language for performance and portability, supporting deployment on JDK 1.4 and up.

At the core of the dcm4che project is a robust implementation of the DICOM standard. The dcm4che-1.x DICOM toolkit is used in many production applications across the world, while the current (2.x) version of the toolkit has been re-architected for high performance and flexibility.

Also contained within the dcm4che project is dcm4chee (the extra ‘e’ stands for ‘enterprise’). dcm4chee is an Image Manager/Image Archive (according to IHE). The application contains the DICOM, HL7 services and interfaces that are required to provide storage, retrieval, and workflow to a healthcare environment. dcm4chee is pre-packaged and deployed within the JBoss application server. By taking advantage of many JBoss features (JMS, EJB, Servlet Engine, etc.), and assuming the role of several IHE actors for the sake of interoperability, the application provides many robust and scalable services:

Following jar files are needed for this activity

    dcm4che.jar
    dcm4che-core-2.0.21.jar
    log4j-1.2.13.jar
    slf4j-api-1.5.0.jar
    slf4j-log4j12-1.5.0.jar

package dicomeditor;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dcm4che2.data.BasicDicomObject;
import org.dcm4che2.data.DicomObject;
import org.dcm4che2.data.Tag;
import org.dcm4che2.data.VR;
import org.dcm4che2.io.DicomInputStream;
import org.dcm4che2.io.DicomOutputStream;

/**
 *
 * @author awaiswaheed
 */

public class DicomEditor {

    public void UpdateTags() {

        String sourceFolder = "D:\\WRONGTAGS";
        String destinationFolder = "D:\\CRRTAGS";


        //First Delete Temp Folder Files before copy images there
        File[] tempImages = new File(destinationFolder).listFiles();
        for (int j = 0; j < tempImages.length; j++) {
            tempImages[j].delete();
        }
        //now get all files in tag folder
        File[] allFiles = new File(sourceFolder).listFiles();

        DicomObject dcmObj = new BasicDicomObject();
        DicomInputStream din = null;
        for (int i = 0; i < allFiles.length; i++) {
            System.out.println("Current Image in Progress = " + (i + 1)
                    + " out of = " + allFiles.length);
            try {
                din = new DicomInputStream(allFiles[i]);
                din.readDicomObject(dcmObj, -1);
                //System.out.println(" Tag.PatientID  *******************   " + Tag.PatientID);
                //dcmObj.putString(1048608, VR.LO, "1234");
                dcmObj.putString(Tag.PatientName, VR.LO, "XYZPatientName");
                dcmObj.putString(Tag.PatientBirthDate, VR.DA, "19690101");
                dcmObj.putString(Tag.PatientAge, VR.AS, "060Y");
                dcmObj.putString(Tag.Modality, VR.CS, "NM");
                dcmObj.putString(Tag.AccessionNumber, VR.SH, "1213456");


                this.writeFile(dcmObj, destinationFolder, "\\" + allFiles[i].getName());
                din.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } //Loop
        //get all files from tag file and delete them
    }

    public void writeFile(DicomObject obj, String copyServer, String fileName) {

        File f = new File(copyServer + fileName);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        DicomOutputStream dos = new DicomOutputStream(bos);
        try {
            dos.writeDicomFile(obj);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            try {
                dos.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static void main(String[] args) {
        DicomEditor dcmEdit = new DicomEditor();
        dcmEdit.UpdateTags();
    }
}
Advertisements

12 Comments

  1. rechlalef says:

    Thanks for the program, it is very helpful. I have just one question. Where can i find the package dicomeditor ? I really don’t know how to get it.

    Regards,

    Rechlalef

    • Learning The "J" says:

      That is a user defined package, you can write any name you want.
      This is a basic java concept, however if you are still unable to execute do let me know.

      • Rechlalef says:

        thanks. I used your code to edit the dicom tags. By cons I can not edit the tag sequences with, I tried several methods but I can not reach Elements contained within a sequence. I used loops without success. Is you have an idea?

      • Learning The "J" says:

        can you please elaborate your question

  2. Rechlalef says:

    I am trying to modify the value of tag inside a sequence. I used your code and i added some parameters to read all elements of dicom :

    Iterator iter = obj.datasetIterator();

    while (iter.hasNext()) {

    DicomElement element = (DicomElement) iter.next();
    if (element.hasItems()) {
    for (int i = 0; i < element.countItems(); i++) {
    DicomObject obj2 = element.getDicomObject(i);

    System.out.println("" + obj2);
    obj2.putString(Tag.ToleranceTableLabel, VR.SH, "Test");
    dos.writeDicomFile(obj2);

    }
    }
    }

    I can see all dicom elements but I can't change the elements in a sequence with the function writeDicomFile(obj2). I have this error messaage : "Exception in thread "main" java.lang.IllegalArgumentException: Missing (0002,0010) Transfer Syntax UID"

    Thanks

    • Learning The "J" says:

      Check your DICOM image, The tag value in the transfer syntax UID tag is null. It should not be null. There are few machines out there which intentionally remove this tag.I believe that you should get some other image for testing purpose.
      We are using the same concept/code for our in-house build PACS and facing no problems.

  3. Rechlalef says:

    Using your code, I can actually change the values of dicom tags, but not the values in the sequences. I work on beams whose dicom information that interest me are contained in sequences. I can not make or find a code that allows me to do so. Do you know methods or class that allow me to directly edit tags in sequences?

    Ps : The error is generated when I do two loops and I’m trying to change the elements in the sequence, it said that transfer syntax UID is null but it is not

    Thanks

  4. Amir says:

    Rechlalef,
    Can you share the code.

  5. Amir says:

    Try,
    String transferSyntax = obj2.getString(Tag.TransferSyntaxUID) ;
    if (transferSyntax == null) {
    System.out.println(“Transfer Syntax is missing… setting Explicit VR Little Endian”);
    obj2.putString(Tag.TransferSyntaxUID, VR.UI, “1.2.840.10008.1.2.1”);
    }

  6. Rechlalef says:

    Hello Amir,

    This is the code :

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Iterator;

    import javax.management.AttributeList;

    import org.dcm4che.util.BufferedOutputStream;
    import org.dcm4che2.data.DicomElement;
    import org.dcm4che2.data.DicomObject;
    import org.dcm4che2.data.Tag;
    import org.dcm4che2.data.VR;
    import org.dcm4che2.io.DicomInputStream;
    import org.dcm4che2.io.DicomOutputStream;

    public class DicomItem {

    public DicomItem() {

    }

    public void lecture(DicomObject obj, File f) {

    String input = “C:\\Users\\ato\\Desktop\\MLCStat2\\RTPLAN001.dcm”;
    f = new File(input);
    if (f.exists())
    System.out.println(“le fichier existe!”);

    try {
    FileInputStream fis = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DicomInputStream dis = new DicomInputStream(bis);

    obj = dis.readDicomObject();

    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    Iterator iter = obj.datasetIterator();

    while (iter.hasNext()) {

    DicomElement element = (DicomElement) iter.next();
    if (element.hasItems()) {
    for (int i = 0; i < element.countItems(); i++) {

    DicomObject obj2 = element.getDicomObject(i);

    System.out.println("" + obj2);
    obj2.putString(Tag.ToleranceTableLabel, VR.SH, "ATO");
    this.writeFile(obj2, input);
    }
    }
    }
    }

    public void writeFile(DicomObject obj, String fileName) {

    File f = new File(fileName);
    FileOutputStream fos;
    try {
    fos = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    return;
    }
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DicomOutputStream dos = new DicomOutputStream(bos);
    try {

    dos.writeDicomFile(obj);

    } catch (IOException e) {
    e.printStackTrace();
    return;
    } finally {
    try {
    dos.close();
    } catch (IOException ignore) {
    }
    }
    }
    }

  7. Rechlalef says:

    By adding your lines, I have a value assigned to uid transfer but my dicom file contains no further information except Transfer Syntax UID, Referenced SOP Class UID, Referenced SOP Instance UID and tags that I added with function writeFile ().

  8. msehg1 says:

    Hello,

    Thanks for the information in this post, in your example you use Tag.PatientName to represent the patient Name, is there a way you can refer to the patientName by using its tag, such as (0010,0010)?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Start here

%d bloggers like this: