如何使用dcm4che-3.2.1将JPG图像转换为DICOM文件? [英] How to convert JPG image to DICOM file using dcm4che-3.2.1?

查看:362
本文介绍了如何使用dcm4che-3.2.1将JPG图像转换为DICOM文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以设置属性并创建dicom文件,但不能将图像写入dicom文件吗?

I can set the attributes and create the dicom file, but I can not write the image to the dicom file?

推荐答案

我已经尝试过使用现有的图像并且它可以工作,但是我希望它不适用于RGB图像。

I've tried it with an image I have and it works but I expect it won't work for RGB images. Something like this though

    BufferedImage jpg = ImageIO.read(new File("myjpg.jpg"));

    //Convert the image to a byte array 
    DataBufferUShort buff = (DataBufferUShort) jpg.getData().getDataBuffer();
    short[] data = buff.getData();
    ByteBuffer byteBuf = ByteBuffer.allocate(2*data.length);
    int i = 0;
    while (data.length > i) {
        byteBuf.putShort(data[i]);
        i++;
    }

    //Copy a header 
    DicomInputStream dis = new DicomInputStream(new File("fileToCopyheaderFrom.dcm"));
    Attributes meta = dis.readFileMetaInformation();
    Attributes attribs = dis.readDataset(-1, Tag.PixelData);
    dis.close();

    //Change the rows and columns
    attribs.setInt(Tag.Rows, VR.US, jpg.getHeight());
    attribs.setInt(Tag.Columns, VR.US, jpg.getWidth());
    System.out.println(byteBuf.array().length);
    //Attributes attribs = new Attributes();

    //Write the file
    attribs.setBytes(Tag.PixelData, VR.OW, byteBuf.array());
    DicomOutputStream dcmo = new DicomOutputStream(new File("myDicom.dcm"));
    dcmo.writeFileMetaInformation(meta);
    attribs.writeTo(dcmo);
    dcmo.close();

编辑1

我假设您的图像在此处具有无符号的短数据缓冲区。

I've assumed your image has an Unsigned short Data Buffer here.

DataBufferUShort buff = (DataBufferUShort) jpg.getData().getDataBuffer();

要处理其他数据缓冲区,应检查类型,相应地进行强制转换,然后转换为字节数组。对于字节缓冲区,应该很容易

To handle other data buffers you should check the type , cast acordingly and then convert to a byte array. For a byte Buffer it should be easy

DataBufferByte buff = (DataBufferByte) jpg.getData().getDataBuffer();

然后

buff.getData(numOfBank)

其中 numOfBank 为您的图片是0

应返回字节数组

这篇关于如何使用dcm4che-3.2.1将JPG图像转换为DICOM文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆