dbdicom.Series.set_affine#
- Series.set_affine(affine: np.eye())[source]#
Set the affine matrix of a series.
The affine is defined as a 4x4 numpy array with bottom row [0,0,0,1]. The final column represents the position of the top right hand corner of the first slice. The first three columns represent rotation and scaling with respect to the axes of the reference frame.
- Parameters:
affine (numpy.ndarray) – 4x4 numpy array
- Raises:
ValueError – if the series is empty. The information of the affine matrix is stored in the header and can not be stored in an empty series.
See also
Example
Create a series with unit affine array:
>>> zeros = db.zeros((128,128,10)) >>> print(zeros.affine()) [array([ [1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]], dtype=float32)]
Rotate the volume over 90 degrees in the xy-plane:
>>> affine = np.array([ ... [1., 0., 0., 0.], ... [0., 1., 0., 0.], ... [0., 0., 1., 0.], ... [0., 0., 0., 1.], ... ]) >>> zeros.set_affine(affine)
Apart from the rotation, also change the resolution to (3mm, 3mm, 1.5mm):
>>> affine = np.array([ ... [0., -3., 0., 0.], ... [3., 0., 0., 0.], ... [0., 0., 1.5, 0.], ... [0., 0., 0., 1.], ... ]) >>> zeros.set_affine(affine)
# Now rotate, change resolution, and shift the top right hand corner of the lowest slice to position (-30mm, 20mm, 120mm):
>>> affine = np.array([ ... [0., -3., 0., -30.], ... [3., 0., 0., 20.], ... [0., 0., 1.5, 120.], ... [0., 0., 0., 1.], ... ]) >>> zeros.set_affine(affine)
Note changing the affine will affect multiple DICOM tags, such as slice location and image positions:
>>> print(zeros.SliceLocation) [120.0, 121.5, 123.0, 124.5, 126.0, 127.5, 129.0, 130.5, 132.0, 133.5]
In this case, since the slices are stacked in parallel to the z-axis, the slice location starts at the lower z-coordinate of 120mm and then increments slice-by-slice with the slice thickness of 1.5mm.