Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

dicom to jpeg conversion produces overexposed image

338 views
Skip to first unread message

Alexey Peskov

unread,
Sep 18, 2019, 5:27:02 AM9/18/19
to dcm4che

Hi.

I'm trying to convert a dicom file to jpeg using a dcm4che in Java but having an issue with one particular file (see attached 12346Ani.dcm). Resulted jpeg is overexposed (in photography terms) - see attached 1236Ani.jpeg.

I used this example as a starting point and my class look like this:

package com.bla;

import org.dcm4che3.image.BufferedImageUtils;
import org.dcm4che3.imageio.plugins.dcm.DicomImageReadParam;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class Example {

   
private static ImageReader imageReader;
   
private static int overlayActivationMask = 0xffff;
   
private static int overlayGrayscaleValue = 0xffff;
   
   
public static void main(String[] args) throws IOException {
       
imageReader =
               
ImageIO.getImageReadersByFormatName("DICOM").next();
       
convert(new File("/1236Ani.dcm"), new File("/1236Ani.jpeg"));
    }

   
private static void convert(File src, File dest) throws IOException {
       
try (ImageInputStream iis = ImageIO.createImageInputStream(src)) {
           
BufferedImage bi = readImage(iis);
            bi
= convert(bi);
            dest
.delete();
           
try (ImageOutputStream ios = ImageIO.createImageOutputStream(dest)) {
               
writeImage(ios, bi);
           
}
       
}
   
}

   
private static BufferedImage convert(BufferedImage bi) {
       
ColorModel cm = bi.getColorModel();
       
return cm.getNumComponents() == 3 ? BufferedImageUtils.convertToIntRGB(bi) : bi;
   
}
   
   
private static BufferedImage readImage(ImageInputStream iis) throws IOException {
       
imageReader.setInput(iis);
       
return imageReader.read(0, readParam());
   
}

   
private static ImageReadParam readParam() {
       
DicomImageReadParam param =
               
(DicomImageReadParam) imageReader.getDefaultReadParam();
//        param.setWindowCenter(windowCenter);
//        param.setWindowWidth(windowWidth);
//        param.setAutoWindowing(autoWindowing);
//        param.setWindowIndex(windowIndex);
//        param.setVOILUTIndex(voiLUTIndex);
//        param.setPreferWindow(preferWindow);
//        param.setPresentationState(prState);
        param.setOverlayActivationMask(overlayActivationMask);
        param
.setOverlayGrayscaleValue(overlayGrayscaleValue);
       
return param;
   
}
   
   
private static void writeImage(ImageOutputStream ios, BufferedImage bi)
           
throws IOException {
       
ImageWriter imageWriter;
       
Iterator<ImageWriter> imageWriters =
               
ImageIO.getImageWritersByFormatName("JPEG");
        imageWriter
= imageWriters.next();
        imageWriter
.setOutput(ios);
        imageWriter
.write(null, new IIOImage(bi, null, null), imageWriter.getDefaultWriteParam());
   
}
}

I have a feeling that this whitening is caused by the white corners on the top of the image. If I crop dicom in the editor to remove white corners - resulted jpeg looks ok. The question is if it is possible to tune up my code somehow to have correct jpeg even with the white corners.

Thank you for your suggestions
1236Ani.dcm
1236Ani.jpeg

Tatsunidas

unread,
Sep 18, 2019, 8:16:34 AM9/18/19
to dcm...@googlegroups.com
Hi, I am the dcm4che.v5 beginner too,

I'm read byte stream directly.
For example, if you can use ImageJ,
//following code for only use lossless(littleendian)
//read byte[] as pixel
Attributes dcm = dis.readDataset(-1, -1);
VR.Holder holder = new VR.Holder();
byte[] pixels = (byte[]) dcm.getValue(Tag.PixelData, holder);
int w = dcm.getInt(Tag.Columns, 0);
int h = dcm.getInt(Tag.Rows, 0);
//create image(16 bit only)
short[] shortArray = new short[pixels.length/2];
ByteBuffer byteBuf = ByteBuffer.wrap(pixels).order(ByteOrder.LITTLE_ENDIAN);
byteBuf.asShortBuffer().get(shortArray);
//https://forum.image.sc/t/how-to-initialize-the-shortprocessor/23689/2
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);//allways byte for colormodel!
ShortProcessor sp = new ShortProcessor(w, h, shortArray, cm);
ImagePlus imp = new ImagePlus("",sp);
imp.show();

you can add ij lib to maven pom.xml,
  <dependency>
<groupId>net.imagej</groupId>
<artifactId>ij</artifactId>
<version>1.52p</version>
</dependency>

tatsuaki

2019年9月18日(水) 18:27 Alexey Peskov <aspe...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "dcm4che" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dcm4che+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dcm4che/59d7498c-61db-46d6-bb59-b50af59172d1%40googlegroups.com.


--
==========================================
Tatsuaki KOBAYASHI
TEL:080-1274-6433
Good Luck
==========================================

Gunter Zeilinger

unread,
Sep 22, 2019, 1:16:35 PM9/22/19
to dcm...@googlegroups.com
The image does not contain a VOI LUT (neither explicit nor a linear by Window Center/Width), but specifies

$ dcmdump ~/testdata/1236Ani.dcm | grep PixelValue
830: (0028,0106) US #2 [0] SmallestImagePixelValue
840: (0028,0107) US #2 [65535] LargestImagePixelValue

But actually the Pixel Value of the image are  in the range [60978,65535]

If you apply a window which just clamp this range:

$ dcm2jpg -c63256 -w4557 ~/testdata/1236Ani.dcm /tmp/1236Ani.jpg
/home/gunter/testdata/1236Ani.dcm -> /tmp/1236Ani.jpg

you will get attached rendered image.

Opened Issue #546


1236Ani.jpg

Alexey Peskov

unread,
Sep 23, 2019, 4:48:58 AM9/23/19
to dcm4che
Great! Thank you so much for creating a ticket for that. Will be waiting for release of 5.19.0. Do you have any date in mind where this version is going to be released so we can set reminder and switch?

Thank you

Gunter Zeilinger

unread,
Sep 23, 2019, 4:50:09 AM9/23/19
to dcm...@googlegroups.com
~in 2 weeks.


Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
--
You received this message because you are subscribed to the Google Groups "dcm4che" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dcm4che+u...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages