简体   繁体   English

如何转储我的用户输入指定的单个dicom标签

[英]How to dump a single dicom tag specified my user input

I am setting up a DICOM reader that I will be using to process CT scanner configurations. 我正在设置一个DICOM阅读器,我将使用它来处理CT扫描仪配置。 I am writing the application so that I can easily view DICOM either by dumping all tags or I would like to specify which tag I would like to look at. 我正在编写该应用程序,以便可以通过转储所有标签或指定要查看的标签来轻松查看DICOM。 It is a C# console application using the fo-dicom library. 它是使用fo-dicom库的C#控制台应用程序。

My problem is in my method definition I want to pass an argument in, I want the value of that argument to come from the user. 我的问题是在方法定义中,我想传入一个参数,我希望该参数的值来自用户。 Capture it through the console with a Console.Read(). 使用Console.Read()通过控制台捕获它。

I am able to dump all tags but can't seem to figure out just dumping the one. 我能够转储所有标签,但似乎无法弄清楚仅转储一个标签。

        public static void DumpAllTags(string dicomFile)
        {
            try
            {
                Console.WriteLine($"Attempting to dump all tags from DICOM 
                file:{dicomFile}...");

                var file = DicomFile.Open(dicomFile);
                foreach (var tag in file.Dataset)
                {
                    Console.WriteLine($" {tag} 
                   '{file.Dataset.GetValueOrDefault(tag.Tag, 0, "")}'");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error occured during DICOM file dump 
                operation -> {e.StackTrace}");
            }
        }

        public static void DumpSingleDicomTag(string dicomFile, string tag)
        {
            try
            {
                Console.WriteLine($"Attempting to dump {tag} from DICOM file: 
                {dicomFile}...");
                var file = DicomFile.Open(dicomFile);
                var dicomDataset = file.Dataset;
                var result = dicomDataset.Get<string>(DicomTag.tag);

                Console.WriteLine(result);


            }
            catch (Exception e)
            {
                Console.WriteLine($"Error occured during DICOM file dump 
                operation -> {e.StackTrace}");
            }
        }

When trying to pass the tag variable that is captured by the keyboard and apply it to the DicomTag method I get an error. 尝试传递键盘捕获的标签变量并将其应用于DicomTag方法时,出现错误。

DicomTag does not contain a definition for 'tag' DicomTag不包含“标签”的定义

Your code cannot compile because your variable tag is a string value and not a property of class DicomTag . 您的代码无法编译,因为您的变量tag是一个字符串值,而不是DicomTag类的DicomTag

Generally spoken: in DICOM a tag is defined by two number: group and element. 一般说来:在DICOM中,标签由两个数字定义:组和元素。 So if you are doing some kind of configration, then you should not store texts like "Patientname" but the numbers like "0010,0010". 因此,如果要进行某种配置,则不应存储“ Patientname”之类的文本,而应存储“ 0010,0010”之类的数字。

So this code will work: 因此,此代码将起作用:

public static void DumpSingleDicomTag(string dicomFile, string tagNumber)
{
    var dataset = DicomFile.Open(dicomFile).Dataset;
    var tag = Dicom.DicomTag.Parse(tagNumber);
    var result = dataset.GetString(tag);
    Console.Writeline(result);
}

// calling it with
DumpSignleDicomTag(dicomFile, "0010,0010");

If you have some good reasons why you need to store the names of the tags and not the numbers, then you need to iterate the DicomDictionary, which may cost quite some time. 如果出于某些原因需要存储标签名称而不是数字,那么您需要迭代DicomDictionary,这可能会花费很多时间。 There is a class DicomDictionary which holds a list of all defined DicomTags. 有一个DicomDictionary类,其中包含所有已定义的DicomTag的列表。 This list is loadef from a resource file on demand, so when the class is first accessed. 该列表是从资源文件中按需加载的,因此在首次访问该类时。 You then can iterate this whole list and find the first where the name matches: 然后,您可以遍历整个列表,并找到名称匹配的第一个列表:

using System.Linq;

public static void DumpSingleDicomTag(string dicomFile, string tagName)
{
    var dataset = DicomFile.Open(dicomFile).Dataset;

    var entry = Dicom.DicomDictionary.Default.FirstOrDefault(t => t.Keyword == tagName);
    var result = dataset.GetString(entry.Tag);

    Console.Writeline(result);
}

// calling it with
DumpSignleDicomTag(dicomFile, "PatientName");

But as I said, this code iterates the whole list of tags, which are currently about 4650 entries. 但是正如我所说,这段代码会迭代整个标签列表,目前大约有4650个条目。

https://github.com/fo-dicom/fo-dicom/issues/942 https://github.com/fo-dicom/fo-dicom/issues/942

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM