dicom转jpg图像相关代码

文章介绍了如何利用dcm4che库将DICOM医学图像文件转换为JPEG格式。主要涉及Java代码示例,包括Dcm2jpgIOStreamOutput和Dcm2JpgMain两个类,以及转换过程中所需的Maven依赖。转换过程包括设置窗口中心、宽度、自动窗口化等参数,并读取DICOM文件内容,最后编码为JPEG并保存至目标文件。
摘要由CSDN通过智能技术生成

本文章使用dcm4che将dicom文件转成jpg图片

maven依赖

        <!-- dcm4che start -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.dcm4che.tool</groupId>
            <artifactId>dcm4che-tool-common</artifactId>
            <version>5.25.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/dcm4che.tool/dcm4che-tool-dcmqr -->
        <dependency>
            <groupId>dcm4che.tool</groupId>
            <artifactId>dcm4che-tool-dcmqr</artifactId>
            <version>2.0.29</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/dcm4che.tool/dcm4che-tool-dcmrcv -->
        <dependency>
            <groupId>dcm4che.tool</groupId>
            <artifactId>dcm4che-tool-dcmrcv</artifactId>
            <version>2.0.29</version>
        </dependency>
        <dependency>
            <groupId>org.dcm4che</groupId>
            <artifactId>dcm4che-imageio</artifactId>
            <version>5.27.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--dcm end-->

Dcm2jpgIOStreamOutput.java



import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.format.TextStyle;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.dcm4che3.data.Attributes;
import org.dcm4che3.imageio.plugins.dcm.DicomImageReadParam;
import org.dcm4che3.util.SafeClose;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import static java.time.format.TextStyle.*;

public class Dcm2jpgIOStreamOutput {
    private final ImageReader imageReader = ImageIO.getImageReadersByFormatName("DICOM").next();
    private float windowCenter;
    private float windowWidth;
    private boolean autoWindowing = true;
    private int windowIndex;
    private int voiLUTIndex;
    private boolean preferWindow = true;
    private Attributes prState;
    private int overlayActivationMask = 0xffff;
    private int overlayGrayscaleValue = 0xffff;
    private int frame = 1;
    public int getFrame() {
        return frame;
    }

    /*
    *
    **/
    public  String test(){

        return null;

    }
    public void setFrame(int frame) {
        this.frame = frame;
    }

    public void setWindowCenter(float windowCenter) {
        this.windowCenter = windowCenter;
    }

    public void setWindowWidth(float windowWidth) {
        this.windowWidth = windowWidth;
    }

    public void setAutoWindowing(boolean autoWindowing) {
        this.autoWindowing = autoWindowing;
    }

    public void setWindowIndex(int windowIndex) {
        this.windowIndex = windowIndex;
    }

    public void setVoiLUTIndex(int voiLUTIndex) {
        this.voiLUTIndex = voiLUTIndex;
    }

    public void setPreferWindow(boolean preferWindow) {
        this.preferWindow = preferWindow;
    }

    public void setPrState(Attributes prState) {
        this.prState = prState;
    }

    public void setOverlayActivationMask(int overlayActivationMask) {
        this.overlayActivationMask = overlayActivationMask;
    }

    public void setOverlayGrayscaleValue(int overlayGrayscaleValue) {
        this.overlayGrayscaleValue = overlayGrayscaleValue;
    }

    public void convert(File src, File dest) throws IOException{
        Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
        ImageReader reader = iter.next();
        ImageInputStream iis = ImageIO.createImageInputStream(src);
        BufferedImage bi;
        OutputStream out = null;
        try{
            reader.setInput(iis, false);
            bi = readImage(iis);
            if (bi == null) {
                System.out.println("\nError: " + src + " - couldn't read!");
                return;
            }
            out = new BufferedOutputStream(new FileOutputStream(dest));
            JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(out);  //这里也可以使用流将图像导出到web应用,用来搭建web版的PACS等。
            enc.encode(bi);
        }finally{
            SafeClose.close(iis);
            SafeClose.close(out);
        }
    }

    private 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 BufferedImage readImage(ImageInputStream iis) throws IOException{
        imageReader.setInput(iis);
        return imageReader.read(frame -1, readParam());
    }
}

Dcm2JpgMain.java

import java.io.File;
import java.io.IOException;

public class Dcm2JpgMain {
    public static void main(String[] args) throws IOException {
        try{
            for(int i = 1;i < 2;i++){
                System.out.print("No: " + String.format("%06d", i) + "   Started  ");
                String tempStr = String.format("%06d", i);
                File src = new File("D:\\temp\\1.2.840.113619.2.203.4.2147483647.1680552493.584217"); //这里我原本测试的是大概400个dcm文件,最后一次测试只实验了一张,这里填上dcm文件的具体位置就行。
                File dest = new File("D:\\dcmjpg\\" + tempStr + ".jpg");  //生成的JPEG图像位置
                Dcm2jpgIOStreamOutput dcm2jpg = new Dcm2jpgIOStreamOutput();
                dcm2jpg.convert(src, dest);
                System.out.println("------>   Result:Completed");
            }
        }catch(IOException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值