I have been using the dicom-contour package to convert DICOM images and contours to numpy arrays. When I call the cfile2pixels function to return a list of pairs of img_arr and contour_arr for a given contour file, I'm getting this error: FileNotFound
[Errno 2] No such file or directory: '/content/drive/MyDrive/MRIplusGd-with-Contour/1.2.840.113619.2.388.10502719.2140823.14003.1697945075.89.dcm'
The contours are in the RTSS file format, while the MRI images in my directory are named like "IMG0000000000.dcm". However, the function looks for images with a different naming convention. How can I resolve this problem?
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)
It appears the dicom_contour library you are using expects the image files to be named according to the
ReferencedSOPInstanceUID
from the contour dataset. The relevant lines start here and are:You would have to modify that library yourself to have it work a different way. I would suggest doing something like (untested):
Then change the second line in the library to:
Note here I have used
dcmread
rather thanread_file
which is deprecated. You'll also have toimport Path
andfrom pydicom import dcmread
in the other library.There are other ways to do this, of course. For example, in the code above, the image files have already been read in, so you could change the parameters to the
dicom_contour
function and just pass the image_datasets in directly, rather than reading them again.