在c#中读取dicom文件标签

发布于 2024-10-16 07:02:19 字数 41 浏览 2 评论 0原文

你能告诉我如何在 C# 中读取所有 dicom 标签及其 VR 吗?

Can you tell me how to read all the dicom tag and its VR in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

又爬满兰若 2024-10-23 07:02:19

Evil Dicom 中,这非常简单:

        //All the work is done with this constructor
        DicomFile df = new DicomFile("fileToRead.dcm");
        foreach (DicomObject d in df.DicomObjects)
        {
            Console.WriteLine(string.Format("I have a tag id of {0}, a VR of {1}", d.Tag.Id, d.VR));          
        }

        //To access the data in a native format use .Data property
        string name = df.PATIENT_NAME.Data;

In Evil Dicom it is really easy:

        //All the work is done with this constructor
        DicomFile df = new DicomFile("fileToRead.dcm");
        foreach (DicomObject d in df.DicomObjects)
        {
            Console.WriteLine(string.Format("I have a tag id of {0}, a VR of {1}", d.Tag.Id, d.VR));          
        }

        //To access the data in a native format use .Data property
        string name = df.PATIENT_NAME.Data;
无悔心 2024-10-23 07:02:19

当然,这完全取决于您使用的 DICOM 库。

使用 ClearCanvas 你会得到这样的结果:

public foo(DicomFile dfile)
{
   DicomAttributeCollection dac;
   dac = dfile.DataSet;

   DicomUid uid;
   bool success;
   success = dac[DicomTags.SopInstanceUid].TryGetUid(0, out uid);

   if (success)
   {
      // The attribute was present in the collection.  The variable uid
      // contains the SopInstanceUid extracted from the DICOM file
   }
}

具有不同 VR 的其他属性将使用 DicomTags 的适当字段以及 VR 的适当 getter 函数来提取。例如,如果您想要将 EchoTime(具有 DS 值表示形式的属性)提取为双精度型,则可以使用 TryGetFloat64 而不是 TryGetUid。 TryGetFloat64(和其他类似函数)的第一个整数参数指示您想要获取的特定值。对于值重数为1的属性,该参数将始终为0。对于VM>VM的属性,该参数将始终为0。 1,您可以通过将参数设置为 n-1 来提取第 n 个值。

This is, of course, totally dependent upon what DICOM library you're using.

Using ClearCanvas you'd have something like this:

public foo(DicomFile dfile)
{
   DicomAttributeCollection dac;
   dac = dfile.DataSet;

   DicomUid uid;
   bool success;
   success = dac[DicomTags.SopInstanceUid].TryGetUid(0, out uid);

   if (success)
   {
      // The attribute was present in the collection.  The variable uid
      // contains the SopInstanceUid extracted from the DICOM file
   }
}

Other attributes with different VRs would be extracted using the appropriate field of DicomTags and with an appropriate getter function for the VR. For example, if you wanted to extract EchoTime (an attribute with a value representation of DS) as a double, you'd use TryGetFloat64 instead of TryGetUid. The first integer parameter to TryGetFloat64 (and other similar functions) indicates the particular value that you want to obtain. For an attribute with value multiplicity 1, this parameter would always be 0. For an attribute with VM > 1, you'd extract the nth value by setting the parameter to n-1.

雾里花 2024-10-23 07:02:19

如果您使用 GDCM + C# 绑定:

http://gdcm.sourceforge.net/html/ SimplePrint_8cs-example.html

public class SimplePrint
{
  public static void RecurseDataSet(File f, DataSet ds, string indent)
    {
    CSharpDataSet cds = new CSharpDataSet(ds);
    while(!cds.IsAtEnd())
      {
      DataElement de = cds.GetCurrent();
      // Compute VR from the toplevel file, and the currently processed dataset:
      VR vr = DataSetHelper.ComputeVR(f, ds, de.GetTag() );

      if( vr.Compatible( new VR(VR.VRType.SQ) ) )
        {
        uint uvl = (uint)de.GetVL(); // Test cast is ok
        System.Console.WriteLine( indent + de.GetTag().toString() + ":" + uvl ); // why not ?
        //SequenceOfItems sq = de.GetSequenceOfItems();
        // GetValueAsSQ handle more cases than GetSequenceOfItems
        SmartPtrSQ sq = de.GetValueAsSQ();
        uint n = sq.GetNumberOfItems();
        for( uint i = 1; i <= n; i++) // item starts at 1, not 0
          {
          Item item = sq.GetItem( i );
          DataSet nested = item.GetNestedDataSet();
          RecurseDataSet( f, nested, indent + "  " );
          }
        }
      else
        {
        System.Console.WriteLine( indent + de.toString() );
        }
      cds.Next();
      }
    }

  public static int Main(string[] args)
    {
    string filename = args[0];
    Reader reader = new Reader();
    reader.SetFileName( filename );
    bool ret = reader.Read();
    if( !ret )
      {
      return 1;
      }
    File f = reader.GetFile();
    DataSet ds = f.GetDataSet();

    RecurseDataSet( f, ds, "" );

    return 0;
    }
}

If you are using GDCM + C# binding:

http://gdcm.sourceforge.net/html/SimplePrint_8cs-example.html

public class SimplePrint
{
  public static void RecurseDataSet(File f, DataSet ds, string indent)
    {
    CSharpDataSet cds = new CSharpDataSet(ds);
    while(!cds.IsAtEnd())
      {
      DataElement de = cds.GetCurrent();
      // Compute VR from the toplevel file, and the currently processed dataset:
      VR vr = DataSetHelper.ComputeVR(f, ds, de.GetTag() );

      if( vr.Compatible( new VR(VR.VRType.SQ) ) )
        {
        uint uvl = (uint)de.GetVL(); // Test cast is ok
        System.Console.WriteLine( indent + de.GetTag().toString() + ":" + uvl ); // why not ?
        //SequenceOfItems sq = de.GetSequenceOfItems();
        // GetValueAsSQ handle more cases than GetSequenceOfItems
        SmartPtrSQ sq = de.GetValueAsSQ();
        uint n = sq.GetNumberOfItems();
        for( uint i = 1; i <= n; i++) // item starts at 1, not 0
          {
          Item item = sq.GetItem( i );
          DataSet nested = item.GetNestedDataSet();
          RecurseDataSet( f, nested, indent + "  " );
          }
        }
      else
        {
        System.Console.WriteLine( indent + de.toString() );
        }
      cds.Next();
      }
    }

  public static int Main(string[] args)
    {
    string filename = args[0];
    Reader reader = new Reader();
    reader.SetFileName( filename );
    bool ret = reader.Read();
    if( !ret )
      {
      return 1;
      }
    File f = reader.GetFile();
    DataSet ds = f.GetDataSet();

    RecurseDataSet( f, ds, "" );

    return 0;
    }
}
花伊自在美 2024-10-23 07:02:19

您有各种用于读取 DICOM 文件的 .NET 开源库,但其中包括:

You have various .NET open-source libraries for reading DICOM files, but among others:

拥抱没勇气 2024-10-23 07:02:19

我使用 LeadTools 实现它,

 private DicomDataSet _objLTDicomDataSet = null;
private void OpenDataset(string file, bool loadDefaultImage)
        {
            _objLTDicomDataSet =new DicomDataSet();
            _objLTDicomDataSet.Load(file, DicomDataSetLoadFlags.None);
            DicomElement element, _ele = null;
             element = _objLTDicomDataSet.FindFirstElement(null, DicomTag.PatientName, true);
            string tagName = _objLTDicomDataSet.GetStringValue(element, 0);
        }

leadtools 还支持获取各种标签的各种方法,您可以使用这些方法并阅读 dicom 文件 方法如下,

DicomDataSet.GetRootElement

DicomDataSet.GetParentElement

DicomDataSet.GetChildElement

DicomDataSet.GetFirstElement

DicomDataSet.GetLastElement

DicomDataSet.GetPreviousElement

DicomDataSet.GetNextElement

了解更多信息 LeadTools 网站

I implement it using LeadTools

 private DicomDataSet _objLTDicomDataSet = null;
private void OpenDataset(string file, bool loadDefaultImage)
        {
            _objLTDicomDataSet =new DicomDataSet();
            _objLTDicomDataSet.Load(file, DicomDataSetLoadFlags.None);
            DicomElement element, _ele = null;
             element = _objLTDicomDataSet.FindFirstElement(null, DicomTag.PatientName, true);
            string tagName = _objLTDicomDataSet.GetStringValue(element, 0);
        }

also leadtools supports various methods for Get various tags you can use that methods and read the dicom file Methods as below

DicomDataSet.GetRootElement

DicomDataSet.GetParentElement

DicomDataSet.GetChildElement

DicomDataSet.GetFirstElement

DicomDataSet.GetLastElement

DicomDataSet.GetPreviousElement

DicomDataSet.GetNextElement

for more info LeadTools Site

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文