目前我正在构建一个可以处理 DICOM 文件的小型桌面应用程序。我使用 C# 和 .NET 进行编码并使用 ClearCanvas 库。我需要做的一件事是能够显示文件的完整内容,包括所有序列。但序列是以递归方式完成的,因此每个序列内部可以有更多序列。现在我的代码可以访问前两个级别,但我只是作为测试人员执行此操作,因为我需要能够访问序列的第 n 个级别。所以我需要以某种方式自动化这一点。这就是我的代码现在的前两个级别的样子。
DicomSequenceItem[] seq = attrs2[i].Values as DicomSequenceItem[];
if (seq != null)
{
for (int j = 0; j < seq.Length; j++)
{
for (int n = 0; n < seq[j].Count; n++)
{
DicomSequenceItem[] level2 = seq[j].ElementAt(n).Values as DicomSequenceItem[];
if(seq[j].ElementAt(n).GetValueType().ToString().Equals("ClearCanvas.Dicom.DicomSequenceItem"))
{
for (int k = 0; k < level2.Length; k++)
{
for (int l = 0; l < level2[k].Count; l++)
{
text += "\t\t" + level2[k].ElementAt(l) + "\r\n";
}
}
}
else
{
text += "\t" + seq[j].ElementAt(n) + "\r\n";
}
}
}
}
任何帮助(代码示例)将不胜感激。
谢谢!
最佳答案
这是一个简单的递归例程,用于遍历属性集合中的标签,包括递归地单步遍历集合中可能存在的任何 Sequence 元素:
void Dump(DicomAttributeCollection collection, string prefix, StringBuilder sb)
{
foreach (DicomAttribute attribute in collection)
{
var attribSQ = attribute as DicomAttributeSQ;
if (attribSQ != null)
{
for (int i=0; i< attribSQ.Count; i++)
{
sb.AppendLine(prefix + "SQ Item: " + attribSQ.ToString());
DicomSequenceItem sqItem = attribSQ[i];
Dump(sqItem, prefix + "\t", sb);
}
}
else
{
sb.AppendLine(prefix + attribute.ToString());
}
}
}
DicomAttributeCollection 是可枚举的,因此您只需使用 foreach 循环即可遍历集合中的所有属性。属性本身存储在 SortedDictionary 中,因此在枚举时它们也将按升序标记顺序排列。
请注意,如果您下载了 ClearCanvas 库的源代码,您还可以查看真正的 Dump() 方法,该方法是 DicomAttributeCollection 类的一部分。它遍历集合并将集合中的所有标签写入 StringBuilder 实例。
关于c# - 如何使用clearcanvas访问DICOM序列的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10804130/