【WPF学习手记】利用fo-dicom读取DICOM文件

利用开源库 fo-dicom 读取 DICOM 文件基本操作。

  • 读取DICOM信息

    原始数据经过窗宽、窗位校正后显示成图。

private void Button_Click1(object sender, RoutedEventArgs e)
{
    ImageManager.SetImplementation(WPFImageManager.Instance);
    var dcmFile = DicomFile.Open(@"C:\Users\Administrator\Desktop\2.dcm");
    var dcmDataSet = dcmFile.Dataset;
    //int height = dcmDataSet.GetValue(DicomTag.Rows, 0);
    //int width = dcmDataSet.GetValue(DicomTag.Columns, 0);
    //int windowsWidth = dcmDataSet.GetValue(DicomTag.WindowWidth, 0);
    //int windowsCenter = dcmDataSet.GetValue(DicomTag.WindowCenter, 0);
    // parameters
    var dcmImage = new DicomImage(dcmDataSet, 0);
    int height = dcmImage.Height;
    int width = dcmImage.Width;
    int windowsWidth = (int)dcmImage.WindowWidth;
    int windowsCenter = (int)dcmImage.WindowCenter;
    var functionalShared = dcmDataSet.GetSequence(DicomTag.SharedFunctionalGroupsSequence);
    var pixelMeasure = functionalShared.Items.FirstOrDefault(i => i.Contains(DicomTag.PixelMeasuresSequence))?.GetSequence(DicomTag.PixelMeasuresSequence).FirstOrDefault();
    var pixelspacing = pixelMeasure.GetValues(DicomTag.PixelSpacing);
    // raw data
    var header = DicomPixelData.Create(dcmDataSet);
    var pixelData = PixelDataFactory.Create(header, 0);
    if (pixelData is GrayscalePixelDataU16)
    {
        ushort[] pixels = ((GrayscalePixelDataU16)pixelData).Data;
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);
        int low = windowsCenter - windowsWidth / 2;
        int high = windowsCenter + windowsWidth / 2;
        for (int i = 0; i < height; ++i)
        {
            for (int j = 0; j < width; ++j)
            {
                int r, g, b;
                int temp = pixels[(width - j - 1) * height + i];
                int val = temp > high ? 255 : (temp < low ? 0 : ((temp - low) * 255 / windowsWidth));
                r = g = b = val;
                bitmap.SetPixel(i, width - j - 1, System.Drawing.Color.FromArgb(r, g, b));
            }
        }
        // show image
        MemoryStream memoryStream = new MemoryStream();
        bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
        byte[] bytes = memoryStream.GetBuffer();
        // byte[] bytes = ms.ToArray();
        memoryStream.Close();
        //Convert it to BitmapImage
        System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
        image.BeginInit();
        image.StreamSource = new MemoryStream(bytes);
        image.EndInit();
        imageDCM.Source = image;
    }
}
  • 图像显示

    可以直接改变窗宽窗位然后显示结果。

private void Button_Click2(object sender, RoutedEventArgs e)
{
    ImageManager.SetImplementation(WPFImageManager.Instance);
    var dcmFile = DicomFile.Open(@"C:\Users\Administrator\Desktop\2.dcm");
    var dcmDataSet = dcmFile.Dataset;
    var dcmImage = new DicomImage(dcmDataSet)
    {
        WindowWidth = 200,    // change
        WindowCenter = 500
    };
    var iImage = dcmImage.RenderImage();
    imageDCM.Source = iImage.AsWriteableBitmap();
}
  • 对比图

    原图、窗宽窗位调节:

【WPF学习手记】利用fo-dicom读取DICOM文件_第1张图片【WPF学习手记】利用fo-dicom读取DICOM文件_第2张图片

 

 

 

 

 

你可能感兴趣的:(WPF学习手记)