我是 DICOM 和 fo-dicom
库的新手。我正在尝试从 OCT 眼部扫描视频创建 DICOM 文件。首先,我提取视频帧,然后使用 fo-dicom 创建 DICOM 文件。
public static void CreateDicomFromVideo(string path)
{
List<Bitmap> videoFrames = GetVideoFrames(path);
CreateDicomFromFrames(videoFrames);
}
private static List<Bitmap> GetVideoFrames(string path)
{
List<Bitmap> videoFrames = new List<Bitmap>();
using (var vFReader = new VideoFileReader())
{
vFReader.Open(path);
for (int i = 0; i < vFReader.FrameCount; i++)
{
Bitmap bmpBaseOriginal = vFReader.ReadVideoFrame();
videoFrames.Add(bmpBaseOriginal);
}
vFReader.Close();
}
if (videoFrames.Count > 0)
return videoFrames;
else return null;
}
private static void CreateDicomFromFrames(List<Bitmap> videoFrames)
{
DicomDataset dataset = new DicomDataset();
FillDataset(dataset);
int i = 0;
foreach (Bitmap item in videoFrames)
{
Bitmap bitmap = new Bitmap(item);
bitmap = GetValidImage(bitmap);
int rows, columns;
byte[] pixels = GetPixels(bitmap, out rows, out columns);
MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
if (i == 0)
{
dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
dataset.Add(DicomTag.Rows, (ushort)rows);
dataset.Add(DicomTag.Columns, (ushort)columns);
dataset.AddOrUpdate(DicomTag.BitsAllocated, (ushort)8);
}
DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
pixelData.BitsStored = 8;
//pixelData.BitsAllocated = 8;
pixelData.SamplesPerPixel = 3;
pixelData.HighBit = 7;
pixelData.PixelRepresentation = 0;
pixelData.PlanarConfiguration = 0;
pixelData.AddFrame(buffer);
i++;
}
DicomFile dicomfile = new DicomFile(dataset);
dicomfile.Save(@"D:\DICOM\files\Video files\dicomfile.dcm");
}
我希望收到包含视频中所有帧的 DICOM 文件,但得到的却是仅包含一帧的 DICOM 文件。
最佳答案
我不是 fo-DICOM 专家,但您的代码显然很少有语法问题。
DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
您每次都会在循环中创建像素数据的新实例。将其移至上面的循环之外。每次(在循环中)为 pixelData.AddFrame
使用相同的 DicomPixelData
实例。
对于其他一些元素也是如此。 PhotometricInterpretation
、Rows
、Columns
、BitsAlowned
等只需分配一次。将这些分配移到上面的循环之外。
基本上,您的循环应该只不断向现有像素数据实例添加新帧。
关于c# - 从视频创建 Dicom 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040845/