将 DICOM 图像和轮廓转换为 numpy 数组

问题描述 投票:0回答:1

我一直在使用 dicom-contour 包将 DICOM 图像和轮廓转换为 numpy 数组。当我调用 cfile2pixels 函数返回给定轮廓文件的 img_arr 和轮廓_arr 对的列表时,我收到此错误:FileNotFound

[Errno 2]没有这样的文件或目录:'/content/drive/MyDrive/MRIplusGd-with-Contour/1.2.840.113619.2.388.10502719.2140823.14003.1697945075.89.dcm'

轮廓采用 RTSS 文件格式,而我目录中的 MRI 图像的命名类似于“IMG0000000000.dcm”。但是,该函数会查找具有不同命名约定的图像。 我该如何解决这个问题?

https://github.com/KeremTurgutlu/dicom-contour/blob/master/tutorial.ipynb

from dicom_contour.contour import \*
def get_contour_file(path):
    """
    Get contour file from a given path by searching for ROIContourSequence
    inside dicom data structure.
    """
    # handle `/` missing
    if path[-1] != '/': path += '/'
    # get .dcm contour file
    fpaths = [path + f for f in os.listdir(path) if '.dcm' in f]
    n = 0
    for fpath in fpaths:
        f = dicom.read_file(fpath)
        if 'ROIContourSequence' in dir(f):
            contour_file = fpath.split('/')[-1]
            n += 1
    if n > 1: warnings.warn("There are multiple files, returning the last one!")
    return contour_file
contour_file = get_contour_file(path)
contour_data = dicom.read_file(path + '/' + contour_file)

def cfile2pixels(file, path, ROIContourSeq=0):
    """
    Given a contour file and path of related images return pixel arrays for contours
    and their corresponding images.
    Inputs
        file: filename of contour
        path: path that has contour and image files
        ROIContourSeq: tells which sequence of contouring to use default 0 (RTV)
    Return
        contour_iamge_arrays: A list which have pairs of img_arr and contour_arr for a given contour file
    """
    # handle `/` missing
    if path[-1] != '/': path += '/'
    f = dicom.read_file(path + file)
    # index 0 means that we are getting RTV information
    RTV = f.ROIContourSequence[ROIContourSeq]
    # get contour datasets in a list
    contours = [contour for contour in RTV.ContourSequence]
    img_contour_arrays = [coord2pixels(cdata, path) for cdata in contours]
    return img_contour_arrays
contour_arrays = cfile2pixels(file=contour_file, path=path, ROIContourSeq=0)
numpy file deep-learning contour dicom
1个回答
0
投票

您正在使用的 dicom_contour 库似乎希望根据轮廓数据集中的

ReferencedSOPInstanceUID
来命名图像文件。相关行从here开始,分别是:

img_ID = contour_dataset.ContourImageSequence[0].ReferencedSOPInstanceUID
img = dicom.read_file(path + img_ID + '.dcm')

您必须自己修改该库才能使其以不同的方式工作。我建议做类似的事情(未经测试):

from pathlib import Path
from pydicom import dcmread

img_path = "your/path/to/images"
image_datasets = [dcmread(filename) for filename in Path(img_path).glob("*.dcm")]
iUID_to_filename = {ds.SOPInstanceUID: ds.filename for ds in image_datasets if 'PixelData' in ds}

然后将库中的第二行更改为:

img = dcmread(Path(path) / iUID_to_filename[img_id])

注意这里我使用了

dcmread
而不是
read_file
,后者已被弃用。您还必须在其他图书馆中
import Path
from pydicom import dcmread

当然,还有其他方法可以做到这一点。例如,在上面的代码中,图像文件已经被读入,因此您可以将参数更改为

dicom_contour
函数,直接将image_datasets传入,而不用再次读取它们。

© www.soinside.com 2019 - 2024. All rights reserved.