c# - 从视频创建 Dicom 文件

标签 c# dicom fo-dicom

我是 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 实例。

对于其他一些元素也是如此。 PhotometricInterpretationRowsColumnsBitsAlowned 等只需分配一次。将这些分配移到上面的循环之外。

基本上,您的循环应该只不断向现有像素数据实例添加新帧。

关于c# - 从视频创建 Dicom 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040845/

相关文章:

c# - 在 C# 中使用 fo-dicom 操作和转换 CT 图像的像素数据

c# - 在 AutoCAD 中以一定角度插入 block (C#)

c# - 在枚举中使用按位或

c# - 在 C# 中同步多行文本框位置

java - dicom 小程序查看器

c# - FO-DICOM:在 C# Windows 窗体应用程序中使用呈现的位图调整窗口大小会导致崩溃

c# - 如果数据库尚不存在则创建

c++ - 使用 Qt 的 C++ 应用程序中的 IImebra 代码提供 malloc : *** error for object xxxxxx: pointer being freed was not allocated

go - 如何从MOVE中正确存储dicom的像素数据?

c# - 将 jpg 图像作为 jpg 图像添加到 DICOM 文件