CreateDicomImage#

class medicalimageanalysis.utils.creation.CreateDicomImage(output_dir, data, study=None, series=None, frame=None, origin=None, spacing=None, thickness=None, orientation=None)[source]

Bases: object

Writes a 3D array to disk as a DICOM series, one .dcm file per slice.

Parameters:
  • output_dir (str) – Folder the .dcm files are written to. Created if it does not exist.

  • data ((Z, Y, X) ndarray) – Voxel values. Written as signed 16-bit integers (floats are rounded), so values must fit in int16.

  • study (str, optional) – Study Instance UID. Generated in run() if None.

  • series (str, optional) – Series Instance UID. Generated in run() if None.

  • frame (str, optional) – Frame of Reference UID. Generated in run() if None.

  • origin ((3,) array_like, optional) – Patient position (mm) of the first voxel. Defaults to [0, 0, 0].

  • spacing ((2,) array_like, optional) – In-plane pixel spacing (x, y) in mm. Defaults to [1, 1].

  • thickness (float, optional) – Slice thickness in mm, also used as the distance between slices. Defaults to 1.

  • orientation ((6,) array_like, optional) – ImageOrientationPatient (row direction, column direction). Defaults to [1, 0, 0, 0, 1, 0].

Examples

>>> import tempfile
>>> import numpy as np
>>> creator = CreateDicomImage(tempfile.mkdtemp(), np.zeros((4, 16, 16), dtype=np.int16), thickness=2)
>>> len(creator.run())
4
set_study(study)[source]

Sets the Study Instance UID.

Parameters:

study (str) – Study Instance UID.

Examples

>>> import numpy as np
>>> creator = CreateDicomImage('C:/Data/output', np.zeros((4, 16, 16), dtype=np.int16))
>>> creator.set_study('1.2.3')
set_series(series)[source]

Sets the Series Instance UID.

Parameters:

series (str) – Series Instance UID.

Examples

>>> import numpy as np
>>> creator = CreateDicomImage('C:/Data/output', np.zeros((4, 16, 16), dtype=np.int16))
>>> creator.set_series('1.2.3.4')
set_frame(frame)[source]

Sets the Frame of Reference UID.

Parameters:

frame (str) – Frame of Reference UID.

Examples

>>> import numpy as np
>>> creator = CreateDicomImage('C:/Data/output', np.zeros((4, 16, 16), dtype=np.int16))
>>> creator.set_frame('1.2.3.4.5')
set_origin(origin)[source]

Sets the patient position (mm) of the first voxel.

Parameters:

origin ((3,) array_like) – Origin (x, y, z).

Examples

>>> import numpy as np
>>> creator = CreateDicomImage('C:/Data/output', np.zeros((4, 16, 16), dtype=np.int16))
>>> creator.set_origin([-250, -250, 100])
set_spacing(spacing)[source]

Sets the in-plane pixel spacing (mm).

Parameters:

spacing ((2,) array_like) – Spacing (x, y).

Examples

>>> import numpy as np
>>> creator = CreateDicomImage('C:/Data/output', np.zeros((4, 16, 16), dtype=np.int16))
>>> creator.set_spacing([0.98, 0.98])
set_thickness(thickness)[source]

Sets the slice thickness (mm), also used as the distance between slices.

Parameters:

thickness (float) – Slice thickness.

Examples

>>> import numpy as np
>>> creator = CreateDicomImage('C:/Data/output', np.zeros((4, 16, 16), dtype=np.int16))
>>> creator.set_thickness(2.5)
run(patient_name='Test', patient_id='Test', modality='CT', description='', sex='M')[source]

Writes one .dcm file per slice to output_dir.

Missing UIDs and geometry are filled with their defaults and stored on the instance, so the generated UIDs can be read back afterwards.

Parameters:
  • patient_name (str) – Patient Name.

  • patient_id (str) – Patient ID.

  • modality ({'CT', 'MR', 'PT'}) – Modality, which also sets the SOP Class UID.

  • description (str) – Series Description.

  • sex ({'M', 'F', 'O'}) – Patient Sex.

Returns:

Paths of the written files, in slice order.

Return type:

list of str

Raises:

ValueError – If the modality is not supported.

Examples

>>> import tempfile
>>> import numpy as np
>>> import pydicom
>>> voxels = np.zeros((10, 64, 32), dtype=np.int16)
>>> creator = CreateDicomImage(tempfile.mkdtemp(), voxels, spacing=[0.5, 0.8], thickness=2)
>>> paths = creator.run(patient_name='Doe^John', modality='MR')
>>> ds = pydicom.dcmread(paths[-1])
>>> ds.Rows, ds.Columns, [float(v) for v in ds.PixelSpacing], float(ds.ImagePositionPatient[2])
(64, 32, [0.8, 0.5], 18.0)