在c#中读取dicom文件标签

时间:2011-02-09 07:05:19

标签: c# dicom

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

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

当然,这完全取决于您正在使用的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
   }
}

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

答案 2 :(得分:1)

如果您使用的是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;
    }
}

答案 3 :(得分:1)

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;

答案 4 :(得分:1)

我使用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 网站