Reputation: 203
A DICOM file contains a series of CAT scan images. Is there an implementation of a DICOM library in Java that can read the files and extract the images stored in them? I would like to store those images into a BufferedImage data type.
Upvotes: 2
Views: 16966
Reputation: 1516
Java Example: (reads patients ID from metadata)
import java.io.File;
import java.io.IOException;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.io.DicomInputStream;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File(...);
DicomInputStream dis = new DicomInputStream(file);
Attributes dcmObj = dis.readDataset(-1, -1);
String someVar = dcmObj.getString(Tag.PatientID);
System.out.println(someVar);
}
}
Maven:
<repositories>
<repository>
<id>www.dcm4che.org</id>
<name>dcm4che Repository</name>
<url>http://www.dcm4che.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-imageio</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-imageio-rle</artifactId>
<version>3.3.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.media</groupId>
<artifactId>jai_imageio</artifactId>
<version>1.2-pre-dr-b04</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.dcm4che.tool</groupId>
<artifactId>dcm4che-tool-common</artifactId>
<version>3.3.7</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Upvotes: 0
Reputation: 1938
In Java:
One of the best article for DICOM read in JAVA follow this link
https://saravanansubramanian.com/dicomtutorials/#dicom-programming-tutorials-using-java
One more thing you need extra software to extract the image without this software you can't read the image and content .
jai-1_1_2_01-lib-windows-i586-jdk.exe
jai_imageio-1_0_01-lib-windows-i586-jre.exe
Because DICOM images are so highly resolution.
You also need JDK32 bit.
In Web Project:Configured the Jre Path instated of JDK path.
Upvotes: 0
Reputation: 81
I wrote some time ago some code that permmitted me to read ".dcm" files and export them into an array of BufferedImage data type.
I used the library dcm4che that you can find here: http://www.dcm4che.org/
In the code I wrote I used those classes:
import org.dcm4che3.imageio.plugins.dcm.*;
import org.dcm4che3.data.Tag;
import org.dcm4che3.io.DicomOutputStream;
Firstly I used a method to read the DICOM file and get its pixel data into a raster:
static BufferedImage createBufferedImgdFromDICOMfile(File dicomFile) {
Raster raster = null ;
System.out.println("Input: " + dicomFile.getName());
//Open the DICOM file and get its pixel data
try {
Iterator iter = ImageIO.getImageReadersByFormatName("DICOM");
ImageReader reader = (ImageReader) iter.next();
DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();
ImageInputStream iis = ImageIO.createImageInputStream(dicomFile);
reader.setInput(iis, false);
//Returns a new Raster (rectangular array of pixels) containing the raw pixel data from the image stream
raster = reader.readRaster(0, param);
if (raster == null)
System.out.println("Error: couldn't read Dicom image!");
iis.close();
}
catch(Exception e) {
System.out.println("Error: couldn't read dicom image! "+ e.getMessage());
e.printStackTrace();
}
return get16bitBuffImage(raster);
}
Then I used this method to get the pixels from the raster previusly created from the DICOM file and put them in a BufferImage with a black an white ColorModel:
public static BufferedImage get16bitBuffImage(Raster raster) {
short[] pixels = ((DataBufferUShort) raster.getDataBuffer()).getData();
ColorModel colorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_GRAY),
new int[]{16},
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
DataBufferUShort db = new DataBufferUShort(pixels, pixels.length);
WritableRaster outRaster = Raster.createInterleavedRaster(
db,
raster.getWidth(),
raster.getHeight(),
raster.getWidth(),
1,
new int[1],
null);
return new BufferedImage(colorModel, outRaster, false, null);
}
If you want to export the BufferImage to a JPEG file you can use this method:
private static void outputJpegImage(BufferedImage outputImage, String outputPath) {
try {
File outputJpegFile = new File(outputPath);
OutputStream output = new BufferedOutputStream(new FileOutputStream(outputJpegFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(outputImage);
output.close();
} catch (IOException e) {
System.out.println("Error: couldn't write jpeg image! "+ e.getMessage());
e.printStackTrace();
}
System.out.println("Output: " + outputPath);
}
Upvotes: 3
Reputation: 4233
You can use dcm4che-3.3.X. This new rewrite of the fabulous framework comes with jai-imageio library fully integrated and you don't need to download and configure it anymore.
Reading meta-information of a Dicom file is a mater of creating a org.dcm4che3.io.DicomInputStream
from dicom file and parsing the stream with a class that implements org.dcm4che3.io.DicomInputHandler
interface. You have to implements this methods:
void readValue(DicomInputStream dis, Attributes attrs) throws IOException;
void readValue(DicomInputStream dis, Sequence seq) throws IOException;
void readValue(DicomInputStream dis, Fragments frags) throws IOException;
void startDataset(DicomInputStream dis) throws IOException;
void endDataset(DicomInputStream dis) throws IOException;
startDataset
and endDataset
methods are launched when the stream is open/closed. The read methods are invoked when attributes, sequence or fragments are found. You can see a complete sample implementation in dcm4che/dcm4che-tool/dcm4che-tool-dcmdump.
To read dicom image into a java.awt.image.BufferedImage
you need to get an javax.imageio.stream.ImageInputStream
from a dicom file this way javax.imageio.ImageIO.createImageInputStream(dicomFile)
where dicomFile
is a java.io.File
.
If you are wondering how this simple code works, this is because of dcm4che-imageio
plugin.
Once again you can see a complete example implementation in dcm4che/dcm4che-tool/dcm4che-tool-dcm2jpg.
Obviously, you need a properly configured maven pom.xml
with all dependencies you need, at least:
<dependencies>
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-imageio</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-imageio-rle</artifactId>
<version>3.3.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.media</groupId>
<artifactId>jai_imageio</artifactId>
<version>1.2-pre-dr-b04</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.dcm4che.tool</groupId>
<artifactId>dcm4che-tool-common</artifactId>
<version>3.3.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
and also dcm4che maven repo
<repositories>
<repository>
<id>www.dcm4che.org</id>
<name>dcm4che Repository</name>
<url>http://www.dcm4che.org/maven2</url>
</repository>
Hope it helps.
Upvotes: 11