0

I'am trying to create a dicom video from mp4 file and add my own tags. I can create it but when I try to reproduce the video with DICOM viewer like MicroDicom i can only see a black image.

public async void VideoToDicom(string srcFilePath, string videoName, string videoId)
    {
        // var dataset = new DicomDataset(DicomTransferSyntax.MPEG4AVCH264BDCompatibleHighProfileLevel41);
        var dataset = new DicomDataset(DicomTransferSyntax.MPEG4AVCH264HighProfileLevel42For2DVideo);
        var metadata = await VideoUtils.Instance.GetVideoMetaData(srcFilePath);
 dataset.Add(DicomTag.SOPClassUID, DicomUID.CTImageStorage);
        dataset.Add(DicomTag.SOPInstanceUID, DicomUID.Generate());
        dataset.Add(DicomTag.StudyInstanceUID, DicomUID.Generate()); 
        dataset.Add(DicomTag.Series​Instance​UID, DicomUID.Generate());

        var numberOfFrames = metadata.Duration.TotalSeconds * metadata.VideoData.Fps;
        var size = metadata.VideoData.FrameSize.Split("x");
        dataset.Add(DicomTag.NumberOfFrames, (int)numberOfFrames);
        dataset.Add(DicomTag.Columns, Convert.ToUInt16(size[1]));
        dataset.Add(DicomTag.Rows, Convert.ToUInt16(size[0]));
        dataset.Add(DicomTag.BitsAllocated, "8");

        dataset.Add(DicomTag.FrameAcquisitionDuration, "40");
        dataset.Add(DicomTag.PixelRepresentation, (ushort)0);
        dataset.Add(DicomTag.VideoImageFormatAcquired, "MPEG4");
        dataset.Add(DicomTag.LossyImageCompressionMethod, "ISO_14496_10");
        dataset.Add(DicomTag.LossyImageCompression, "01");
        dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.YbrPartial420.Value);

        DicomPixelData pixelData = CreateDicomVideoPixelData(metadata, dataset);
        byte[] videoBytes = GetVideoData(srcFilePath);
        MemoryByteBuffer buffer = new MemoryByteBuffer(videoBytes);
        pixelData.AddFrame(buffer);


        var dicomFile = new DicomFile(dataset);
        dicomFile.FileMetaInfo.TransferSyntax = DicomTransferSyntax.MPEG4AVCH264HighProfileLevel42For2DVideo;
        dicomFile.Save("C:\\testvideos\\test.dcm");}


private DicomPixelData CreateDicomVideoPixelData(MetaData metadata, DicomDataset dataset)
    {
        DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
        var numberOfFrames = metadata.Duration.TotalSeconds * metadata.VideoData.Fps;
        var size = metadata.VideoData.FrameSize.Split("x");

        pixelData.Width = Convert.ToUInt16(size[0]);
        pixelData.Height = Convert.ToUInt16(size[1]);
        pixelData.NumberOfFrames = (int)numberOfFrames;

        pixelData.HighBit = 7;
        pixelData.BitsStored = 8;
        //pixelData.BitsAllocated = 8; Readonly?
        pixelData.SamplesPerPixel = 3;
        pixelData.PlanarConfiguration = 0;
        pixelData.PixelRepresentation = 0;
        //pixelData.PhotometricInterpretation = PhotometricInterpretation.YbrPartial420;
        return pixelData;
    }


private static byte[] GetVideoData(string videoFile)
    {
        byte[] buffer;
        FileStream fileStream = new FileStream(videoFile, FileMode.Open, FileAccess.Read);
        try
        {
            int length = (int)fileStream.Length;  // get file length
            buffer = new byte[length];            // create buffer
            int count;                            // actual number of bytes read
            int sum = 0;                          // total number of bytes read

            // read until Read method returns 0 (end of the stream has been reached)
            while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                sum += count;  // sum is a buffer offset for next reading
        }
        finally
        {
            fileStream.Close();
        }
        return buffer;}

At least if i can not see a video i expect to get all the frames from the video. Thanks for the help. My file on dicom viewer

Willy
  • 9
  • 3
  • 'DICOM' can contain lots of different kinds of data. Not all viewers will support all kinds of dicom files, especially once things like compression is involved. So I would suggest starting with confirming that your reader can display video, and that your MPEG4-file uses the specified codec. – JonasH Jul 19 '22 at 09:50

0 Answers0