2
votes

I'm new to DICOM and to fo-dicom library. I'm trying to create a DICOM file out of an OCT eye scan video. First I'm extracting of the frames of the video and then create a DICOM file using fo-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");
}

I'm expecting to receive a DICOM file with all the frames from the video, but getting a DICOM file with only one frame.

1
By any chance, is the frame in the dcm file the last one?Thomas Ayoub
@ThomasAyoub it was the last one indeed. Amit's answer solved it, only the pixelData.AddFrame line should have been left within the frame loop, and all other configurations prior to the loop were based on the first frame.Eli Co

1 Answers

4
votes

I am not fo-DICOM expert but there are clearly few syntax issues with your code.

DicomPixelData pixelData = DicomPixelData.Create(dataset, true);

You are creating new instance of pixel data in loop each time. Move that to outside the loop above. Use same instance of DicomPixelData every time (in loop) for pixelData.AddFrame.

Same is true for few other elements. PhotometricInterpretation, Rows, Columns, BitsAllocated etc needs to be assigned only once. Move those assignments outside the loop above.

Basically, your loop should only keep adding new frame to existing pixel data instance.