简体   繁体   English

迭代 DICOM 文件

[英]Iterate DICOM files

Im using pydicom to read dicom files (the patient name), but all my dicom files are name as i34, i67 and so on (no specific order or sequence for these names).我使用 pydicom 来读取 dicom 文件(患者姓名),但我所有的 dicom 文件都命名为 i34、i67 等(这些名称没有特定的顺序或顺序)。 My problem: i have a list of patients names in a txt file and i want search every patient name in this txt file with all the dicom files, looking to their PatientName in the dicom file (for those who are not familiar, every dicom file has a number of informations of a patient, one of them being the PatientName)我的问题:我在一个 txt 文件中有一个患者姓名列表,我想用所有 dicom 文件搜索这个 txt 文件中的每个患者姓名,在 dicom 文件中查找他们的 PatientName(对于那些不熟悉的人,每个 dicom 文件有许多患者的信息,其中之一是患者姓名)

Exemple:例子:

txt file: John George Ringo Paul txt 文件:约翰·乔治·林戈·保罗

Dicom files: i54 i98 i64 i12 Dicom 文件:i54 i98 i64 i12

What I want: Take John and search i54, i98, i64, i12 for the PatientName match.我想要什么:以 John 为例,搜索 i54、i98、i64、i12 以查找 PatientName 匹配项。 And do it for every name in the txt file.并对 txt 文件中的每个名称执行此操作。 Hope I made my self clear.希望我说清楚了。 Thks in advance!提前谢谢!

EDIT编辑

i try this code but didnt work我试过这段代码,但没有用

import dicom
import os

dir = "/path/to/dir_where_dicom_file_are"
txt = open('/path/to/txt_with_patientsnames.txt','r')
for line in txt:
    for sub, dirs, files in os.walk(dir):
        names = line.strip()
        for dcm in files:
           ds = dicom.read_file(dir+dcm) #necessary for read dicom files                            
           patient = ds.PatientName
           if names in patient:
           #do something

I took a sample of 20 dicom images and a sample of 20 names.我抽取了 20 个 dicom 图像的样本和 20 个名称的样本。 I knew that only one dicom file of this sample belong to one patient name so my ouput should be only one match righ?我知道这个样本中只有一个 dicom 文件属于一个患者姓名,所以我的输出应该只有一个匹配,对吗? but im getting 20 match results out of this.但我从中得到了 20 场比赛结果。 Whats wrong?怎么了?

The problem was logic (more specifically the lack of it)问题是逻辑(更具体地说是缺乏逻辑)

dir = "/path/where/dcm/files/are"
arq = open('path/where/file with pacient names are/patient.txt','r')
for linha in arq:
     c = linha.strip()
     for sub,p,a in os.walk(dir):
        for dcm in a:
          ds = dicom.read_file(dir+dcm)
          pctname = ds.PatientName           
          if c in pctname:
          #do something

To be honest i dont know what was wrong in the first place.老实说,我一开始不知道出了什么问题。

You could also consider approaching this problem from a different angle.您也可以考虑从不同的角度解决这个问题。 You can organize your DICOM files into a directory hierarchy that includes PatientName at one of the levels.您可以将 DICOM 文件组织到一个目录层次结构中,该层次结构在其中一个级别包含 PatientName。 This will simplify locating relevant files quickly.这将简化快速查找相关文件的过程。 One script that can help you with this task is dicomsort .可以帮助您完成此任务的一个脚本是dicomsort

Using that tool, the command below will sort the DICOM files in the input directory into the PatientName > StudyDate > SeriesDescription-InstanceNumber named directory structure.使用该工具,下面的命令会将输入目录中的 DICOM 文件排序到 PatientName > StudyDate > SeriesDescription-InstanceNumber 命名的目录结构中。 I often use such sorting as a prerequisite for subsequent processing.我经常使用这种排序作为后续处理的先决条件。 Of course, if you are working with a very large dataset, and only need to access small subset of it, this will be a suboptimal solution, but I thought it may be handy for your situation.当然,如果您正在处理一个非常大的数据集,并且只需要访问它的一小部分,这将是一个次优的解决方案,但我认为它可能适合您的情况。

dicomsort data sorted/%PatientName/%StudyDate/%SeriesDescription-%InstanceNumber.dcm

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

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