JPG to DICOM with dfcm4che3

219 views
Skip to first unread message

Enrique ruiz

unread,
Mar 9, 2015, 9:50:21 AM3/9/15
to dcm...@googlegroups.com
I try convert jpg to dicom using dcm4che3. In images monochrome it's successful however with rgb images, the result is incorrect. It change the colors 



someone idea?

My code is:

        File fileJpg = new File(path + "tmp_" + numImagen + ".jpg");
       
File fileDicomFinal = new File(path + "tmp_" + numImagen + ".dcm");
       
File fileDicomOrig = new File(headerOriginal);
       
File fileDicomType = new File(headerType);
       
       
BufferedImage jpg = ImageIO.read(fileJpg);
               
       
//Convert the image to a byte array        
       
Raster raster = jpg.getData();
       
DataBuffer data = raster.getDataBuffer();        
         
       
//Copy a header
       
DicomInputStream dis = new DicomInputStream(fileDicomType);
       
Attributes meta = dis.readFileMetaInformation();
       
Attributes attribs = dis.readDataset(-1, Tag.PixelData);
        dis
.close();
       
DicomInputStream disOrig = new DicomInputStream(fileDicomOrig);
       
Attributes attribsOrig = disOrig.readDataset(-1, Tag.PixelData);
        disOrig
.close();
       
       
//get properties of image
       
int colorComponents = jpg.getColorModel().getNumColorComponents();
       
int bitsPerPixel = jpg.getColorModel().getPixelSize();
       
int bitsAllocated = (bitsPerPixel / colorComponents);
       
int samplesPerPixel = colorComponents;
       
       
//Change the rows and columns
        attribs
.setString(Tag.SpecificCharacterSet, VR.CS, "ISO_IR 100");  
        attribs
.setString(Tag.PhotometricInterpretation, VR.CS, samplesPerPixel == 3 ? "RGB" : "MONOCHROME2");
       
        attribs
.setInt(Tag.SamplesPerPixel, VR.US, samplesPerPixel);          
        attribs
.setInt(Tag.Rows, VR.US, jpg.getHeight());  
        attribs
.setInt(Tag.Columns, VR.US, jpg.getWidth());  
        attribs
.setInt(Tag.BitsAllocated, VR.US, bitsAllocated);  
        attribs
.setInt(Tag.BitsStored, VR.US, bitsAllocated);  
        attribs
.setInt(Tag.HighBit, VR.US, bitsAllocated-1);  
        attribs
.setInt(Tag.PixelRepresentation, VR.US, 0);


       
/*Also, our Dicom header needs information about date and time of creation:*/
        attribs
.setDate(Tag.InstanceCreationDate, VR.DA, new Date());  
        attribs
.setDate(Tag.InstanceCreationTime, VR.TM, new Date());  
       
/* Every Dicom file has a unique identifier.
        * Here we’re generating study, series and Sop instances UIDs.
        * You may want to modify these values, but you should to care about their uniqueness.
        */

        attribs
.setString(Tag.SeriesInstanceUID, VR.UI, UIDUtils.createUID());  
        attribs
.setString(Tag.SOPInstanceUID, VR.UI, UIDUtils.createUID());  
        attribs
.setString(Tag.StudyInstanceUID, VR.UI, UIDUtils.createUID());
        attribs
.setString(Tag.AccessionNumber, VR.IS, attribsOrig.getString(Tag.AccessionNumber));
        attribs
.setString(Tag.PatientName, VR.CS, attribsOrig.getString(Tag.PatientName));
        attribs
.setString(Tag.InstitutionName, VR.CS, attribsOrig.getString(Tag.InstitutionName));
        attribs
.setString(Tag.PatientID, VR.CS, attribsOrig.getString(Tag.PatientID));
        attribs
.setString(Tag.PatientBirthDate, VR.DT, attribsOrig.getString(Tag.PatientBirthDate));
        attribs
.setString(Tag.PatientSex, VR.CS, attribsOrig.getString(Tag.PatientSex));
        attribs
.setString(Tag.OtherPatientIDs, VR.CS, attribsOrig.getString(Tag.OtherPatientIDs));
        attribs
.setString(Tag.PatientAge, VR.AS, attribsOrig.getString(Tag.PatientAge));
        attribs
.setString(Tag.AcquisitionDateTime, VR.DT, attribsOrig.getString(Tag.AcquisitionDateTime));
        attribs
.setString(Tag.AcquisitionDate, VR.DT, attribsOrig.getString(Tag.AcquisitionDate));      
        attribs
.setString(Tag.AcquisitionTime, VR.DT, attribsOrig.getString(Tag.AcquisitionTime));
        attribs
.setString(Tag.StudyInstanceUID , VR.UI, attribsOrig.getString(Tag.StudyInstanceUID));
               
       
DataBufferByte b = (DataBufferByte)data;
       
//Write the file                
        attribs
.setValue(Tag.PixelData, VR.OW, b.getData());
       
DicomOutputStream dcmo = new DicomOutputStream(fileDicomFinal);
        dcmo
.writeFileMetaInformation(meta);
        attribs
.writeTo(dcmo);
        dcmo
.close();

Dimitri Pianeta

unread,
Mar 12, 2015, 12:29:49 PM3/12/15
to dcm...@googlegroups.com
Seeing the file enclose,
Use :

public static void main(String[] args) throws IOException, Exception {
 
       
       
       
                     JFileChooser choix = new JFileChooser();
     choix.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//sélectionne que le répertoire
                 
        
        int retour = choix.showOpenDialog(null);
     if(retour == JFileChooser.APPROVE_OPTION){
                   
        File fileInput = choix.getSelectedFile();
        JFileChooser saveDicom = new JFileChooser();
                saveDicom.setFileSelectionMode(JFileChooser.FILES_ONLY);
        saveDicom.setMultiSelectionEnabled(false);
        saveDicom.showSaveDialog(null);
        File fileOutput =saveDicom.getSelectedFile();
jpgdcm jpg = new jpgdcm(fileInput,fileOutput);

   
    }
}


jpgdcm.java
Reply all
Reply to author
Forward
0 new messages