dcm4che - Creation of multi-frame dicom image using DCM4CHE3(JAVA) -
i trying create multiframe dicom file multiple jpeg file writing them using imagewriter writer.canwritesequence() giving false not able write jpeg file multiframe dicom file there way can create multiframe dicom file jpeg image or bufferedimage
public attributes createdicomheader(bufferedimage sampleframe, int numberofframes) { // image information sample image: // frames should have same information once. int colorcomponents = sampleframe.getcolormodel().getnumcolorcomponents(); int bitsperpixel = sampleframe.getcolormodel().getpixelsize(); int bitsallocated = (bitsperpixel / colorcomponents); int samplesperpixel = colorcomponents; // dicom object hold our frames attributes dicom = new attributes(); // add patient related information dicom dataset dicom.getstring(tag.patientname, "aditya^g"); dicom.getstring(tag.patientid, "1234id"); dicom.getdate(tag.patientbirthdate, new java.util.date()); dicom.getstring(tag.patientsex, "m"); // add study related information dicom dataset dicom.getstring(tag.accessionnumber, "1234ac"); dicom.getstring(tag.studyid, "1"); dicom.getstring(tag.studydescription, "multiframe study"); dicom.setdate(tag.studydate, new java.util.date()); dicom.setdate(tag.studytime, new java.util.date()); // add series related information dicom dataset dicom.setint(tag.seriesnumber,vr.us, 1); dicom.setdate(tag.seriesdate, new java.util.date()); dicom.setdate(tag.seriestime, new java.util.date()); dicom.getstring(tag.seriesdescription, "multiframe series"); dicom.getstring(tag.modality, "sc"); // secondary capture // add image related information dicom dataset dicom.setint(tag.instancenumber, vr.us, 1); dicom.setint(tag.samplesperpixel, vr.us, samplesperpixel); dicom.setstring(tag.photometricinterpretation, vr.cs, "ybr_full_422"); dicom.setint(tag.rows, vr.us, sampleframe.getheight()); dicom.setint(tag.columns, vr.us, sampleframe.getwidth()); dicom.setint(tag.bitsallocated, vr.us, bitsallocated); dicom.setint(tag.bitsstored, vr.us, bitsallocated); dicom.setint(tag.highbit, vr.us, bitsallocated-1); dicom.setint(tag.pixelrepresentation, vr.us, 0); // add unique identifiers dicom.setstring(tag.sopclassuid, vr.ui, uid.secondarycaptureimagestorage); dicom.setstring(tag.studyinstanceuid, vr.ui, uidutils.createuid()); dicom.setstring(tag.seriesinstanceuid, vr.ui, uidutils.createuid()); dicom.setstring(tag.sopinstanceuid, vr.ui, uidutils.createuid()); //start of multiframe information: dicom.setint(tag.starttrim, vr.us, 1); dicom.setint(tag.stoptrim, vr.us, numberofframes); dicom.getstring(tag.frametime, 0, "33.33"); dicom.getstring(tag.framedelay, "0.0"); dicom.setint(tag.numberofframes, vr.us, numberofframes); // number of frames dicom.setint(tag.recommendeddisplayframerate, vr.us, 3); dicom.setint(tag.frameincrementpointer, vr.us, tag.frametime); //end of multiframe information. // add default character set dicom.setstring(tag.specificcharacterset, vr.cs, "iso_ir 100"); // init meta information jpeg lossless transfer syntax dicom.createfilemetainformation(uid.jpegbaseline1); return dicom; } public void encodemultiframe(file[] frames, file dest) throws ioexception { // status message system.out.println("creating multiframe file..."); // create dicom image writer instance , set output imagereadparam dicomr=new dicomimagereadparam(); imagewriter writer = imagewriterfactory.getimagewriter(imagewriterfactory.getimagewriterparam(uid.jpegbaseline1)); fileimageoutputstream output = new fileimageoutputstream(dest); writer.setoutput(output); // image sample array of images bufferedimage sample = imageio.read(frames[0]); // create new dataset (header/metadata) our dicom image writer attributes ds = this.createdicomheader(sample, frames.length); attributes fmi = ds.createfilemetainformation(uid.jpegbaseline1); // set metadata our dicom image writer , prepare encode multiframe sequence // imagewriteparam iwp= writer.getdefaultwriteparam() ; dicommetadata writemeta = new dicommetadata(fmi, ds); writemeta.getattributes().addall(ds); if(writer.canwritesequence()) writer.preparewritesequence(writemeta); else system.out.println("can not write sequence"); // dicommetadata writemeta = (dicommetadata) writer.getdefaultstreammetadata(null); // writemeta.getattributes().addall(ds); // writer.preparewritesequence(writemeta); // status message system.out.println("start of write sequence..."); // each extracted jpeg images... (int = 0; < frames.length; i++) { // status message system.out.println("encoding frame # "+ (i+1)); // read jpeg file bufferedimage object bufferedimage frame = imageio.read(frames[i]); // create new iioimage saved dicom multiframe sequence iioimage iioimage = new iioimage(frame, null, null); // write our image dicom multiframe sequence writer.writetosequence(iioimage, null); } // status message system.out.println("end of write sequence."); // our multiframe file created. end sequence , close output stream. writer.endwritesequence(); output.close(); // status message system.out.println("multiframe file created."); } /** * @param args * @throws ioexception */ public static void main(string[] args) throws ioexception { multiframeimagecreation mf=new multiframeimagecreation(); file[] frames = new file("/root/desktop/multi").listfiles(); // create dicom multiframe file mf.encodemultiframe(frames, new file("/root/desktop/multiframe.dcm")); }
}
try use code
import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.iioimage; import javax.imageio.imageio; import javax.imageio.imagewriter; import javax.imageio.stream.fileimageoutputstream; import org.dcm4che2.data.basicdicomobject; import org.dcm4che2.data.dicomobject; import org.dcm4che2.data.tag; import org.dcm4che2.data.uid; import org.dcm4che2.data.vr; import org.dcm4che2.imageio.plugins.dcm.dicomstreammetadata; import org.dcm4che2.imageioimpl.plugins.dcm.dicomimagewriterspi; import org.dcm4che2.util.uidutils; public class mpeg2dicom { public mpeg2dicom() { } public dicomobject createdicomheader(bufferedimage sampleframe, int numberofframes) { int colorcomponents = sampleframe.getcolormodel().getnumcolorcomponents(); int bitsperpixel = sampleframe.getcolormodel().getpixelsize(); int bitsallocated = (bitsperpixel / colorcomponents); int samplesperpixel = colorcomponents; // dicom object hold our frames dicomobject dicom = new basicdicomobject(); // add patient related information dicom dataset dicom.putstring(tag.patientname, null, "patient name"); dicom.putstring(tag.patientid, null, "1234id"); dicom.putdate(tag.patientbirthdate, null, new java.util.date()); dicom.putstring(tag.patientsex, null, "m"); // add study related information dicom dataset dicom.putstring(tag.accessionnumber, null, "1234ac"); dicom.putstring(tag.studyid, null, "1"); dicom.putstring(tag.studydescription, null, "multiframe study"); dicom.putdate(tag.studydate, null, new java.util.date()); dicom.putdate(tag.studytime, null, new java.util.date()); // add series related information dicom dataset dicom.putint(tag.seriesnumber, null, 1); dicom.putdate(tag.seriesdate, null, new java.util.date()); dicom.putdate(tag.seriestime, null, new java.util.date()); dicom.putstring(tag.seriesdescription, null, "multiframe series"); dicom.putstring(tag.modality, null, "sc"); // secondary capture // add image related information dicom dataset dicom.putint(tag.instancenumber, null, 1); dicom.putint(tag.samplesperpixel, null, samplesperpixel); dicom.putstring(tag.photometricinterpretation, vr.cs, "ybr_full_422"); dicom.putint(tag.rows, null, sampleframe.getheight()); dicom.putint(tag.columns, null, sampleframe.getwidth()); dicom.putint(tag.bitsallocated, null, bitsallocated); dicom.putint(tag.bitsstored, null, bitsallocated); dicom.putint(tag.highbit, null, bitsallocated-1); dicom.putint(tag.pixelrepresentation, null, 0); // add unique identifiers dicom.putstring(tag.sopclassuid, null, uid.secondarycaptureimagestorage); dicom.putstring(tag.studyinstanceuid, null, uidutils.createuid()); dicom.putstring(tag.seriesinstanceuid, null, uidutils.createuid()); dicom.putstring(tag.sopinstanceuid, vr.ui, uidutils.createuid()); //start of multiframe information: dicom.putint(tag.starttrim, null, 1); // start @ frame 1 dicom.putint(tag.stoptrim, null, numberofframes); // stop @ frame n dicom.putstring(tag.frametime, null, "33.33"); // milliseconds (30 frames per second) dicom.putstring(tag.framedelay, null, "0.0"); // no frame dalay dicom.putint(tag.numberofframes, null, numberofframes); // number of frames dicom.putint(tag.recommendeddisplayframerate, null, 3); dicom.putint(tag.frameincrementpointer, null, tag.frametime); //end of multiframe information. // add default character set dicom.putstring(tag.specificcharacterset, vr.cs, "iso_ir 100"); // init meta information jpeg lossless transfer syntax // dicom.initfilemetainformation(uid.jpeglossless); dicom.initfilemetainformation(uid.jpegbaseline1); return dicom; } public void encodemultiframe(file[] frames, file dest) throws ioexception { // status message system.out.println("creating multiframe file..."); // create dicom image writer instance , set output imagewriter writer = new dicomimagewriterspi().createwriterinstance(); fileimageoutputstream output = new fileimageoutputstream(dest); writer.setoutput(output); // image sample array of images bufferedimage sample = imageio.read(frames[0]); // create new dataset (header/metadata) our dicom image writer dicomobject ds = this.createdicomheader(sample, frames.length); // set metadata our dicom image writer , prepare encode multiframe sequence dicomstreammetadata writemeta = (dicomstreammetadata) writer.getdefaultstreammetadata(null); writemeta.setdicomobject(ds); writer.preparewritesequence(writemeta); system.out.println("start of write sequence..."); (int = 0; < frames.length; i++) { // status message system.out.println("encoding frame # "+ (i+1)); // read png file bufferedimage object bufferedimage frame = imageio.read(frames[i]); // create new iioimage saved dicom multiframe sequence iioimage iioimage = new iioimage(frame, null, null); // write our image dicom multiframe sequence writer.writetosequence(iioimage, null); } // status message system.out.println("end of write sequence."); // our multiframe file created. end sequence , close output stream. writer.endwritesequence(); output.close(); // status message system.out.println("multiframe file created."); } public static void main(string[] args) { try { // create instance of our class mpeg2dicom f = new mpeg2dicom(); // create array of files extracted ffmpeg images file[] frames = new file("d://aaa").listfiles(); // create dicom multiframe file f.encodemultiframe(frames, new file("d:///multiframe.dcm")); } catch (exception e) { // print exceptions e.printstacktrace(); } } }
before run code please install java advanced imaging image i/o tools 32 bit version 1.0_01 java advanced imaging image , try run code using java 32 bit version
- a) jai imageio library not installed;
- b) jai imageio installed has no values set in path environment variable;
- c) dll files jai native code cannot found (wrong installation);
- d) windows 64bits (it work win32);
Comments
Post a Comment