简体   繁体   中英

How to replace the Pixel Data of DICOM file using fo-DICOM?

I want to replace the pixel data of a DICOM file with another one. I used this code:

public bool ImportImage(string imageFile, string newFilePah, string oldDicomFile)
{
    try
    {
        Bitmap bitmap = new Bitmap(imageFile);
        bitmap = GetValidImage(bitmap);
        int rows, columns;
        byte[] pixels = GetPixels(bitmap, out rows, out columns);
        MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
        DicomDataset dataset = new DicomDataset();
        var df = DicomFile.Open(oldDicomFile);
        FillDataset(ref dataset, df);

        DicomTransferSyntax dicomTransfer = df.Dataset.Get<DicomTransferSyntax>(DicomTag.TransferSyntaxUID, DicomTransferSyntax.JPEGProcess14);
        dataset.AddOrUpdate(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
        dataset.AddOrUpdate(DicomTag.Rows, (ushort)rows);
        dataset.AddOrUpdate(DicomTag.Columns, (ushort)columns);
        dataset.AddOrUpdate(DicomTag.BitsAllocated, (ushort)8);
        DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
        pixelData.BitsStored = 8;
        pixelData.SamplesPerPixel = 3;
        pixelData.HighBit = 7;
        pixelData.PixelRepresentation = 0;
        pixelData.PlanarConfiguration = 0;
        pixelData.AddFrame(buffer);

        DicomFile dicomfile = new DicomFile(dataset.Clone(dicomTransfer));
        dicomfile.Save(newFilePah);
        return true;
    }
    catch(Exception ddd) { return false; }
}
private void FillDataset(ref DicomDataset dataset, DicomFile df)
{
    foreach(var item in df.Dataset)
    {
        if(!item.Tag.Group.ToString().Equals("7FE0") && !item.Tag.Group.ToString().Equals("40"))
            dataset.Add(item);
    }
}

The output DICOM file loses many tags which affect image display.

I referred to this answer. But the AddOrUpdatePixelData method used in that answer is deprecated in version v4.0.0-rc1 that I am using. So that answer does not help me.

Is there any other way to change the pixel data of a DICOM file using fo-DICOM?

Following code does replace the pixel data correctly.

public static bool ImportImage(string imageFile, string newFilePah, string oldDicomFile)
{
    Bitmap bitmap = new Bitmap(imageFile);
    int rows, columns;
    byte[] pixels = GetPixels(bitmap, out rows, out columns);
    MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
    DicomDataset dataset = new DicomDataset();
    var dicomfile = DicomFile.Open(oldDicomFile);
    dataset = dicomfile.Dataset.Clone();

    dataset.AddOrUpdate(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
    dataset.AddOrUpdate(DicomTag.Rows, (ushort)rows);
    dataset.AddOrUpdate(DicomTag.Columns, (ushort)columns);
    dataset.AddOrUpdate(DicomTag.BitsAllocated, (ushort)8);

    DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
    pixelData.BitsStored = 8;
    pixelData.SamplesPerPixel = 3;
    pixelData.HighBit = 7;
    pixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;
    pixelData.PixelRepresentation = 0;
    pixelData.PlanarConfiguration = 0;
    pixelData.Height = (ushort)rows;
    pixelData.Width = (ushort)columns;
    pixelData.AddFrame(buffer);

    dicomfile = new DicomFile(dataset);
    dicomfile.Save(newFilePah);
    return true;
}

private static byte[] GetPixels(Bitmap bitmap, out int rows, out int columns)
{
    using(var stream = new MemoryStream())
    {
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        rows = bitmap.Height;
        columns = bitmap.Width;
        return stream.ToArray();
    }
}

You can see I have cleaned up your code much.

But the major change is using System.Drawing.Imaging.ImageFormat.Bmp instead of other formats. This depends on actual input image format. Use the format as that of input image.

For detailed insight, please refer to this source code on github.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM