问题描述
对于某些测试,我试图操纵以dicom格式存储的CT图像的PixelData
元素,并使用C#中的 Fellow Oak Dicom 将其写回到文件中.经过一些研究,我发现我要处理的矩阵位于存储在byte
数组中的PixelData
的Buffer
中.所以我写了下面的代码:
For some tests I'm trying to manipulate the PixelData
element of a CT image stored in dicom format and write it back in the file with Fellow Oak Dicom in C#. After some research I found out that the matrix I want to deal with is in the Buffer
of PixelData
stored in a byte
-array. So I wrote following code:
DicomFile ctFile = DicomFile.Open(image);
var pixDat = ctFile.Dataset.Get<byte[]>(DicomTag.PixelData);
for (int i = 0; i < pixData.Length; i++)
{
pixDat[i] = Convert.ToByte(200);
}
ctFile.Dataset.AddOrUpdate<byte[]>(DicomTag.PixelData, pixDat);
ctFile.Save("new file folder");
这是我的第一次尝试,在AddOrUpdate
命令中得到了Exception
,因为它无法将byte
数组转换为OB.阅读例如Pianykh关于DICOM的书,OB表示 Other Byte String .但是到目前为止,我仍无法将操纵的byte
数组转换为OB.当我尝试以下代码片段时:
This was my first try and I got an Exception
at the AddOrUpdate
command because it was unable to convert a byte
-array to OB.Reading e.g. Pianykh's book about DICOM, OB means Other Byte String. But up to now I'm unable to convert the manipulated byte
-array to OB. When I tried this code snippet:
DicomOtherByte dob = new DicomOtherByte(DicomTag.PixelData, pixDat);
ctFile.Dataset.AddOrUpdate<DicomOtherByte>(DicomTag.PixelData, dob);
Exception
仍在调用AddOrUpdate
,因为无法将项目转换为OB.在这里在stackoverflow上搜索,在git或google中的fo-dicom文档中,我仍然不知道如何处理它的线索.所以我想知道如何将操纵矩阵转换为OB,因为我认为DicomOtherByte
是OB.
the Exception
still is calling at AddOrUpdate
for being unable to convert the item to OB. Searching here at stackoverflow, fo-dicom documentation in git or with google I still didn't get a clue how to deal with it. So I'm wondering how to convert my manipulated matrix to OB, because I thought DicomOtherByte
is OB.
Exception
是无法创建类型为Dicom.DicomOtherByte类型的值的OB类型的DICOM元素"-System.InvalidOperationException
The Exception
is "Unable to create DICOM element of type OB with values of type Dicom.DicomOtherByte" - System.InvalidOperationException
谢谢.
推荐答案
Dicom数据集中的像素数据非常特殊.它不能轻易地作为单个标签读取或写入. Fo-Dicom具有用于处理像素数据的特殊功能和类.
The pixel data in a Dicom Dataset is something very special. It cannot easily be read or written as a single Tag. Fo-Dicom has special functions and classes to work with pixel data.
这里是一个例子:
DicomFile ctFile = DicomFile.Open(@"C:\Temp\original.dcm");
// Create PixelData object to represent pixel data in dataset
DicomPixelData pixelData = DicomPixelData.Create(ctFile.Dataset);
// Get Raw Data
byte[] originalRawBytes = pixelData.GetFrame(0).Data;
// Create new array with modified data
byte[] modifiedRawBytes = new byte[originalRawBytes.Length];
for (int i = 0; i < originalRawBytes.Length; i++)
{
modifiedRawBytes[i] = (byte)(originalRawBytes[i] + 100);
}
// Create new buffer supporting IByteBuffer to contain the modified data
MemoryByteBuffer modified = new MemoryByteBuffer(modifiedRawBytes);
// Write back modified pixel data
ctFile.Dataset.AddOrUpdatePixelData(DicomVR.OB, modified);
ctFile.Save(@"C:\Temp\Modified.dcm");
请注意,还有更多的帮助程序类可以直接以特定格式(例如PixelDataConverter
和PixelDataFactory
)处理像素数据.
Note that there more helper classes to work with pixel data directly in a specific format like PixelDataConverter
and PixelDataFactory
.
此外,如果要处理实际图像,请使用DicomImage
类.
Also, if you want to work with actual images, use the DicomImage
class.
DicomImage image = new DicomImage(ctFile.Dataset);
这篇关于在C#中使用fo-dicom处理和转换CT图像的PixelData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!