fo-dicom / fo-dicom-samples

Sample applications associated with the fo-dicom framework
Other
153 stars 143 forks source link

Create one dicom file from mulitple frames #66

Closed Dash-Uttam closed 2 years ago

Dash-Uttam commented 3 years ago

Hello All,

I am trying to create dicom file from video. In that i loop through the video frame in bitmap item after i am uploading the frame by frame in server, uploading is working correctly but when i am trying to save dicom file in my local i am getting only last frame dcm file. i want all the frames in single dcm file. i am attaching my code here.

            foreach (Bitmap item in videoFrames)
            {
                Bitmap bitmap = new Bitmap(item);
                //bitmap = GetValidImage(bitmap);
                byte[] pixels = GetPixels(bitmap);
                MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
                ds.AddOrUpdate(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
                ds.AddOrUpdate(DicomTag.BitsAllocated, (ushort)8);
                ds.AddOrUpdate(DicomTag.Rows, (ushort)bitmap.Height);
                ds.AddOrUpdate(DicomTag.Columns, (ushort)bitmap.Width);
                ds.AddOrUpdate(DicomTag.InstanceNumber, i);
                ds.AddOrUpdate(DicomTag.SOPClassUID, "1.2.840.10008.5.1.4.1.1.2");
                ds.AddOrUpdate(DicomTag.SOPInstanceUID, "1.2.840.10008.5.1.4.1.1.2.20181120090837121314" + i);
                //ds.AddOrUpdate(DicomTag.ImagePositionPatient, 0.000);
                //ds.AddOrUpdate(DicomTag.ImageOrientationPatient, 1.000000);
                // ds.AddOrUpdate(DicomTag.SliceLocation, 55.00);
                DicomPixelData pixelData = DicomPixelData.Create(ds, true);
                pixelData.BitsStored = 8;
                pixelData.SamplesPerPixel = 3;
                pixelData.HighBit = 7;
                pixelData.PixelRepresentation = 0;
                pixelData.PlanarConfiguration = 0;
                pixelData.AddFrame(buffer);

                DicomDataset sqContent = new DicomDataset(); //Content of the sequence
                sqContent.Add(DicomTag.Modality, "US");
                sqContent.Add(DicomTag.ScheduledProcedureStepStartDate, DateTime.Now.Date);

                DicomSequence sq = new DicomSequence(DicomTag.ScheduledProcedureStepSequence, sqContent); // Create sequence, add content
                ds.AddOrUpdate(sq); //Add sequence to main dataset

                //  DicomFile file = DicomFile.Open(filename + "_" + i);
                DicomFile file = new DicomFile();
                file.Dataset.Add(ds); //Add main dataset to DicomFile

                file.FileMetaInfo.TransferSyntax = DicomTransferSyntax.ImplicitVRLittleEndian; //Specify transfer syntax
                file.Save(filename); //Save file to disk

                await client.AddRequestAsync(new DicomCStoreRequest(filename));
                await client.SendAsync();
                i++;

            }

@gofal Please if you can help me in that. thank you in advance.

gofal commented 3 years ago

but in your code you are iterating the bitmap frames and for every frame you are creating a full dataset, store this dataset and send it to the pacs. So you are handling every frame like a new separate dicomfile. if you want to have one dicomfile with multiple frames, then the only part that you have to loop through is the

            [...]
            DicomPixelData pixelData = DicomPixelData.Create(ds, true);
            pixelData.BitsStored = 8;
            pixelData.SamplesPerPixel = 3;
            pixelData.HighBit = 7;
            pixelData.PixelRepresentation = 0;
            pixelData.PlanarConfiguration = 0;
            foreach (Bitmap item in videoFrames)
            {
                  Bitmap bitmap = new Bitmap(item);
                  //bitmap = GetValidImage(bitmap);
                  byte[] pixels = GetPixels(bitmap);
                  MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
                  pixelData.AddFrame(buffer);
            }

            [...]
Dash-Uttam commented 3 years ago

@gofal Thanks for help. If you have full code or any reference some where, So that i can manage my code accordingly. Thanks in advance.