如何使用fo-DICOM替换DICOM文件的像素数据? [英] How to replace the Pixel Data of DICOM file using fo-DICOM?

查看:86
本文介绍了如何使用fo-DICOM替换DICOM文件的像素数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个替换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);
    }
}

输出的DICOM文件丢失了许多会影响图像显示的标签.

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

我提到了答案.但是该答案中使用的 AddOrUpdatePixelData 方法在我使用的v4.0.0-rc1版本中已被弃用.这样的答案对我没有帮助.

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.

还有其他方法可以使用fo-DICOM更改DICOM文件的像素数据吗?

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.

但是主要的更改是使用 System.Drawing.Imaging.ImageFormat.Bmp 而不是其他格式.这取决于实际的输入图像格式.使用与输入图像相同的格式.

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.

有关详细的见解,请参阅github上的源代码.

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

这篇关于如何使用fo-DICOM替换DICOM文件的像素数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆