すべての dicom タグとその VR を C# で読み取る方法を教えてください。
5 に答える
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;
もちろん、これは、使用している 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 の適切なゲッター関数を使用して抽出されます。たとえば、EchoTime (DS の値表現を持つ属性) を double として抽出する場合は、TryGetUid の代わりに TryGetFloat64 を使用します。TryGetFloat64 (および他の同様の関数) の最初の整数パラメーターは、取得する特定の値を示します。値の多重度が 1 の属性の場合、このパラメーターは常に 0 になります。VM > 1 の属性の場合、パラメーターを n-1 に設定して n 番目の値を抽出します。
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サイト
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;
}
}