Search code examples
pythonnumpydicompydicom

How access to a single NumPy array in a DICOMDIR file with pydicom?


I am using DICOM with Python and Pydicom library, when I have used .dcm files I hadn't problems. But I am using DICOMDIR data sets now.

My code is the next:

ds = dicom.read_file("dicomdir")

I tried to access to the pixel_array elements as follows:

data = ds.pixel_array

And I get the next error: TypeError: No pixel data found in this dataset.

The output for print ds is more or less the next:

(0004, 1130) File-set ID CS: 'GEMS_11_10_111' (0004, 1200) Offset of the First Directory Recor UL: 412 (0004, 1202) Offset of the Last Directory Record UL: 412 (0004, 1212) File-set Consistency Flag US: 0 (0004, 1220) Directory Record Sequence 7 item(s) ---- (0004, 1400) Offset of the Next Directory Record UL: 0 (0004, 1410) Record In-use Flag US: 65535 (0004, 1420) Offset of Referenced Lower-Level Di UL: 558 (0004, 1430) Directory Record Type CS: 'PATIENT' (0008, 0005) Specific Character Set CS: 'ISO_IR 100' (0010, 0010) Patient's Name PN: '------------------' (0010, 0020) Patient ID LO: '246992' (0010, 0030) Patient's Birth Date DA: '19630523' (0010, 0040) Patient's Sex CS: 'F'


(0004, 1400) Offset of the Next Directory Record UL: 0 (0004, 1410) Record In-use Flag US: 65535 (0004, 1420) Offset of Referenced Lower-Level Di UL: 800 (0004, 1430) Directory Record Type CS: 'STUDY' (0008, 0005) Specific Character Set CS: 'ISO_IR 100' (0008, 0020) Study Date DA: '20111011' (0008, 0030) Study Time TM: '151535.000000' (0008, 0050) Accession Number SH: '1009819901' (0008, 1030) Study Description LO: 'MG CONVENCIONAL' (0020, 000d) Study Instance UID UI: 1.2.840.113564.9.1.2005121220021252.20110608105548.21009819901 (0020, 0010) Study ID SH: '1009819901'


(0004, 1400) Offset of the Next Directory Record UL: 0 (0004, 1410) Record In-use Flag US: 65535 (0004, 1420) Offset of Referenced Lower-Level Di UL: 968 (0004, 1430) Directory Record Type CS: 'SERIES' (0008, 0005) Specific Character Set CS: 'ISO_IR 100' (0008, 0060) Modality CS: 'MG' (0008, 103e) Series Description LO: 'Screen Save' (0020, 000e) Series Instance UID UI: 1.2.840.113619.2.144.2347322947.2192645372.5 (0020, 0011) Series Number IS: '354'


(0004, 1400) Offset of the Next Directory Record UL: 1298 (0004, 1410) Record In-use Flag US: 65535 (0004, 1420) Offset of Referenced Lower-Level Di UL: 0 (0004, 1430) Directory Record Type CS: 'IMAGE' (0004, 1500) Referenced File ID CS: ['DICOM', 'PA1', 'ST1', 'SE1', 'IM1'] (0004, 1510) Referenced SOP Class UID in File UI: Secondary Capture Image Storage (0004, 1511) Referenced SOP Instance UID in File UI: 1.2.840.113619.2.144.44653394.14990.1318380911.86 (0004, 1512) Referenced Transfer Syntax UID in F UI: Explicit VR Little Endian (0008, 0005) Specific Character Set CS: 'ISO_IR 100' (0008, 0008) Image Type CS: 'DERIVED' (0008, 0023) Content Date DA: '20111011' (0008, 0033) Content Time TM: '151909.000000' (0020, 0013) Instance Number IS: '8844' (0028, 0010) Rows US: 2298 (0028, 0011) Columns US: 1980 (0028, 1052) Rescale Intercept DS: '0' (0028, 1053) Rescale Slope DS: '1'


The last part is repeated four times. I have 4 images in my DICOMDIR dataset.

Do you know how access to the field with the Numpy Array for each image?


Solution

  • I received an answer in Pydicom User Forum by Suever:

    DICOMDIR files do not contain any pixel information but rather basic header information along with the path to each file in the dataset. In order to get the pixel data from each of the images, you'll have to traverse the dicomdir dataset to get the filepaths to the individual dicom files (relative to the current directory) and then load the pixel data from each of the individual dicom files.

    A simple way to do this would be (there may be better ways)

        ds = dicom.read_file("dicomdir")
        pixel_data = list()
        for record in ds.DirectoryRecordSequence:
            if record.DirectoryRecordType == "IMAGE":
            # Extract the relative path to the DICOM file
                path = os.path.join(*record.ReferencedFileID)
                dcm = dicom.read_file(path)
    
                # Now get your image data
                pixel_data.append(dcm.pixel_array)
    

    It works ;)