如何将元数据添加到图像(使用Java代码),然后将其转换为dicom [英] how to add metadata to an image (with java code) and then convert it to dicom

查看:601
本文介绍了如何将元数据添加到图像(使用Java代码),然后将其转换为dicom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个Java代码,该文件将jpg和Dicom(需要一个元数据)转换为最终的Dicom文件。我想做的是将jpg图像转换为Dicom图像,并使用Java代码生成元数据。

I found a java code that converts a jpg and a Dicom(it takes the metadata fri¡om that one) files to a final Dicom one. What I want to do is convert the jpg image into a Dicom one, generating the metadata with java code.

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

// Convert the image to a byte array    
DataBuffer buff = jpg.getData().getDataBuffer();
DataBufferUShort buffer = new DataBufferUShort(buff.getSize());

for (int i = 0; i < buffer.getSize(); ++i)
    buffer.setElem(i, buff.getElem(i));

short[] data = buffer.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);



// 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();


推荐答案

我不是工具包专家(当然

I am not expert in toolkit (and of-course Java as well).

您的 //复制标题部分将读取源DICOM文件,并将所有属性保存在 Attributes attribs 变量。

Your "// Copy a header" section reads the source DICOM file and holds all the attributes in Attributes attribs variable.

然后,您的 //更改行和列部分将根据需要修改少量属性。

Then, your "// Change the rows and columns" section modifies few attributes as per need.

然后,您的 //写文件部分只需将从源文件读取的属性添加到目标文件。

Then, your "// Write the file" section simply add the attributes read from source file to destination file.

现在,您要绕过源DICOM文件,并通过添加属性自行将纯JPEG转换为DICOM。

Now, you want to bypass the source DICOM file and convert plain JPEG to DICOM with adding attributes yourself.

将 //复制标题部分替换为构建实例 属性

Replace your "// Copy a header" section to build the instance of Attributes.

Attributes attribs = new Attributes();
attribs.setString(Tag.StudyDate, VR.DA, "20110404");
attribs.setString(Tag.StudyTime, VR.TM, "15");

上面示例中提到的标签仅作为示例。您必须自己决定要包括哪些标签。请注意,规范已根据要处理的SOP类为标签定义了类型1、1C,2、2C和3。

添加标签时,还必须注意正确的VR。规范也谈到了这件事。

我在这里无法解释所有这些;太宽泛。

The tags mentioned in above example are for example only. You have to decide yourself which tags you want to include. Note that specifications have defined Types 1, 1C, 2, 2C and 3 for tags depending on the SOP class you are dealing with.
While adding the tags, you have to take care of correct VR as well. Specifications talk about that thing as well.
I cannot explain all this here; too broad.

这篇关于如何将元数据添加到图像(使用Java代码),然后将其转换为dicom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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