问题
Can you tell me how to read all the dicom tag and its VR in C#?
回答1:
You have various .NET open-source libraries for reading DICOM files, but among others:
- DICOM#
- mdcm
回答2:
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.
回答3:
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;
}
}
回答4:
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;
回答5:
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
来源:https://stackoverflow.com/questions/4942184/read-dicom-file-tags-in-c-sharp